1package core
2
3import (
4 "cmp"
5 "math"
6 "slices"
7
8 "github.com/bnema/neferwl/internal/ports"
9)
10
11// The stash is a workspace's strip of windows set aside with
12// toggle-window-stash, left to right in arrival order. The selected one
13// is centred, stash.width% of the usable width and stashHeight% of its
14// height; its neighbors fill the margins beside it, stash.gap% away,
15// the sides, dimmed; the others wait off screen. toggle-stash-visible
16// hides and shows the whole strip. Native floats (dialogs) are not in it.
17
18// stashWidth is the default share of the usable width, in percent, a
19// stashed window takes (stash.width); stashHeight is its share of the
20// usable height.
21const stashWidth, stashHeight = 80, 80
22
23func (w *Workspace) stashIndex(id WindowID) int {
24 return slices.IndexFunc(w.Stash, func(f Float) bool { return f.ID == id })
25}
26
27// isFloat reports whether the window floats: native or stashed.
28func (w *Workspace) isFloat(id WindowID) bool {
29 return w.floatIndex(id) >= 0 || w.stashIndex(id) >= 0
30}
31
32// stashFocused reports whether the stash has the focus, under any native
33// float that has it. A shown stash without tiles always has it.
34func (w *Workspace) stashFocused() bool {
35 return (w.stashFocus || len(w.Columns) == 0) && !w.stashHidden && len(w.Stash) > 0
36}
37
38// onFloat reports whether the focus is on a floating window: column
39// actions do nothing then.
40func (w *Workspace) onFloat() bool { return w.floatFocus || w.stashFocused() }
41
42// stashWindow moves the focused tile id to the end of the stash,
43// remembering its place in its column to return there.
44func (w *Workspace) stashWindow(id WindowID) {
45 back := w.tilePlace(id)
46 w.RemoveWindow(id)
47 w.addStash(Float{ID: id, back: back})
48}
49
50// tilePlace is where the focused tile id is in its column, for restore.
51func (w *Workspace) tilePlace(id WindowID) *origPlace {
52 c := w.Columns[w.Focus]
53 lone := len(c.Windows) == 1
54 back := &origPlace{col: w.Focus, row: c.Focus, slot: c.Slot, width: c.Width, fullWidth: c.FullWidth && lone, expanded: c.Expanded && lone}
55 for _, v := range c.Windows {
56 if v != id {
57 back.stacked = append(back.stacked, v)
58 }
59 }
60 return back
61}
62
63// addStash appends a window to the stash, shows the stash and selects it.
64func (w *Workspace) addStash(f Float) {
65 w.Stash = append(w.Stash, f)
66 w.stashAt = len(w.Stash) - 1
67 w.showStash()
68}
69
70// captureStash puts a new window id in the shown stash, selected, and
71// reports whether it did. Like a new tile, it leaves a focused native
72// float focused. Under a covering fullscreen window the stash is not on
73// screen: the window tiles as usual.
74func (w *Workspace) captureStash(id WindowID) bool {
75 if id == 0 || w.stashHidden || len(w.Stash) == 0 || w.cover() != 0 {
76 return false
77 }
78 // No former column: toggle-window-stash tiles it as a new column.
79 w.Stash = append(w.Stash, Float{ID: id})
80 w.stashAt, w.stashFocus = len(w.Stash)-1, true
81 return true
82}
83
84// showStash shows the stash and gives it the focus; a member whose
85// fullscreen waited while hidden is fullscreen again, selected, as if it
86// asked now.
87func (w *Workspace) showStash() {
88 w.stashHidden, w.stashFocus, w.floatFocus = false, true, false
89 if i := w.stashIndex(w.hiddenFullscreen); i >= 0 {
90 w.fullscreen, w.stashAt = w.hiddenFullscreen, i
91 }
92 w.hiddenFullscreen = 0
93}
94
95// unstash returns stash entry i to its former column, focused; a free
96// float floats again where it was.
97func (w *Workspace) unstash(i int) {
98 f := w.Stash[i]
99 w.RemoveWindow(f.ID)
100 if f.free {
101 w.Floats = append(w.Floats, f)
102 w.floatFocus, w.stashFocus = true, false
103 return
104 }
105 w.restore(f.ID, f.back)
106}
107
108// holdsStack reports whether c holds a window of the stack left behind.
109func (p origPlace) holdsStack(c Column) bool {
110 return slices.ContainsFunc(c.Windows, func(v WindowID) bool { return slices.Contains(p.stacked, v) })
111}
112
113// restore tiles id back where back says: its row in the column that still
114// holds its former neighbors, else a column of its own at its former
115// index. It takes the focus.
116func (w *Workspace) restore(id WindowID, back *origPlace) {
117 w.floatFocus, w.stashFocus = false, false
118 w.raiseColumns()
119 if back == nil {
120 w.addColumn(Column{Windows: []WindowID{id}})
121 return
122 }
123 at := min(back.col, len(w.Columns))
124 if i := slices.IndexFunc(w.Columns, back.holdsStack); i >= 0 {
125 c := &w.Columns[i]
126 row := 0
127 for _, v := range back.stacked[:back.row] {
128 if slices.Contains(c.Windows, v) {
129 row++
130 }
131 }
132 c.Windows = slices.Insert(c.Windows, row, id)
133 c.Focus = row
134 w.Focus = i
135 w.scroll()
136 return
137 }
138 // Its column is gone: it comes back expanded, unless another column
139 // was expanded meanwhile.
140 expanded := back.expanded && !slices.ContainsFunc(w.Columns, func(c Column) bool { return c.Expanded })
141 w.insertColumn(at, Column{Windows: []WindowID{id}, Width: back.width, Slot: back.slot, Expanded: expanded})
142 if back.fullWidth {
143 w.maximize(w.Focus)
144 }
145}
146
147// rehome is a stashed window moved to another workspace: its former column
148// is not there, so it returns as the last column, at its width.
149func (f Float) rehome() Float {
150 back := &origPlace{col: math.MaxInt}
151 if f.back != nil {
152 back.width = f.back.width
153 }
154 f.back = back
155 return f
156}
157
158// ToggleStashVisible hides the stash, or shows it and gives it the
159// focus. Native floats stay as they are. It does nothing under a covering
160// fullscreen window (a game in scanout): hiding it would drop scanout and
161// VRR.
162func (w *Workspace) ToggleStashVisible() {
163 if len(w.Stash) == 0 || w.cover() != 0 {
164 return
165 }
166 if w.stashHidden {
167 w.showStash()
168 return
169 }
170 w.stashHidden, w.stashFocus = true, false
171}
172
173// Click focuses the clicked window. A click on a tile behind the shown
174// stash hides the stash, as a click outside a menu closes it.
175func (w *Workspace) Click(id WindowID) {
176 if !w.stashHidden && len(w.Stash) > 0 && !w.isFloat(id) && w.cover() == 0 {
177 w.stashHidden = true
178 }
179 w.FocusID(id)
180}
181
182// stashRect is where the selected stashed window is, border included.
183func (w *Workspace) stashRect() Rect {
184 u := w.Usable
185 fw := min(max(u.W*cmp.Or(w.stashWidth, stashWidth)/100-2*w.border, 0)+2*w.border, u.W)
186 fh := min(max(u.H*stashHeight/100-2*w.border, 0)+2*w.border, u.H)
187 return Rect{X: u.X + (u.W-fw)/2, Y: u.Y + (u.H-fh)/2, W: fw, H: fh}
188}
189
190// stashLayout places the stash: the selected window centred, its
191// neighbors gap% of the usable width away, showing what the margins
192// leave of them, the others hidden. cover is the covering fullscreen
193// window, if any.
194func (w *Workspace) stashLayout(focusedID, cover WindowID) []Placement {
195 u := w.Usable
196 center := w.stashRect()
197 fw, fh := center.W, center.H
198 // Peeks stay in the margins: they never overlap the selected window.
199 gap := u.W * w.stashGap / 100
200 peek := max((u.W-fw)/2-gap, 0)
201 out := make([]Placement, 0, len(w.Stash))
202 for i, f := range w.Stash {
203 p := Placement{ID: f.ID, Floating: true, Focused: f.ID == focusedID, Inset: ports.SideAll}
204 switch {
205 case f.ID == cover:
206 p.Rect, p.Fullscreen, p.Inset = w.Output, true, 0
207 case w.stashHidden || cover != 0:
208 p.Hidden = true
209 case i == w.stashAt:
210 p.Rect = center
211 case peek > 0 && i == w.stashAt-1:
212 p.Rect, p.Peek = Rect{X: center.X - gap - fw, Y: center.Y, W: fw, H: fh}, true
213 case peek > 0 && i == w.stashAt+1:
214 p.Rect, p.Peek = Rect{X: center.X + fw + gap, Y: center.Y, W: fw, H: fh}, true
215 default:
216 p.Hidden = true
217 }
218 out = append(out, p)
219 }
220 return out
221}