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.dropStashOver()
118 w.floatFocus, w.stashFocus = false, false
119 w.raiseColumns()
120 if back == nil {
121 w.addColumn(Column{Windows: []WindowID{id}})
122 return
123 }
124 at := min(back.col, len(w.Columns))
125 if i := slices.IndexFunc(w.Columns, back.holdsStack); i >= 0 {
126 c := &w.Columns[i]
127 row := 0
128 for _, v := range back.stacked[:back.row] {
129 if slices.Contains(c.Windows, v) {
130 row++
131 }
132 }
133 c.Windows = slices.Insert(c.Windows, row, id)
134 c.Focus = row
135 w.Focus = i
136 w.scroll()
137 return
138 }
139 // Its column is gone: it comes back expanded, unless another column
140 // was expanded meanwhile.
141 expanded := back.expanded && !slices.ContainsFunc(w.Columns, func(c Column) bool { return c.Expanded })
142 w.insertColumn(at, Column{Windows: []WindowID{id}, Width: back.width, Slot: back.slot, Expanded: expanded})
143 if back.fullWidth {
144 w.maximize(w.Focus)
145 }
146}
147
148// rehome is a stashed window moved to another workspace: its former column
149// is not there, so it returns as the last column, at its width.
150func (f Float) rehome() Float {
151 back := &origPlace{col: math.MaxInt}
152 if f.back != nil {
153 back.width = f.back.width
154 }
155 f.back = back
156 return f
157}
158
159// ToggleStashVisible hides the stash, or shows it and gives it the
160// focus. Native floats stay as they are. Over a covering fullscreen
161// window it is a user override: the stash shows over it, which stays
162// fullscreen behind (composed, no scanout) and returns when the stash
163// hides. A stashed fullscreen window is the stash on screen: no-op.
164func (w *Workspace) ToggleStashVisible() {
165 if len(w.Stash) == 0 {
166 return
167 }
168 if c := w.cover(); c != 0 {
169 if w.stashIndex(c) >= 0 {
170 return
171 }
172 if w.stashOverCover() {
173 w.dropStashOver()
174 return
175 }
176 // Not showStash: a pending stashed fullscreen must not replace
177 // the covering window.
178 w.stashOver, w.stashHidden, w.stashFocus, w.floatFocus = c, false, true, false
179 return
180 }
181 if w.stashHidden {
182 w.showStash()
183 return
184 }
185 w.stashHidden, w.stashFocus = true, false
186}
187
188// stashOverCover reports whether the stash shows over the covering
189// fullscreen window (ToggleStashVisible).
190func (w *Workspace) stashOverCover() bool {
191 return w.stashOver != 0 && w.stashOver == w.cover() && !w.stashHidden && len(w.Stash) > 0
192}
193
194// dropStashOver ends the stash override when the focus leaves it: the
195// stash hides again under the covering window, as it was.
196func (w *Workspace) dropStashOver() {
197 if w.stashOverCover() {
198 w.stashHidden, w.stashFocus = true, false
199 }
200 w.stashOver = 0
201}
202
203// Click focuses the clicked window. A click on a tile behind the shown
204// stash hides the stash, as a click outside a menu closes it.
205func (w *Workspace) Click(id WindowID) {
206 if !w.stashHidden && len(w.Stash) > 0 && !w.isFloat(id) && w.cover() == 0 {
207 w.stashHidden = true
208 }
209 w.FocusID(id)
210}
211
212// stashRect is where the selected stashed window is, border included.
213func (w *Workspace) stashRect() Rect {
214 u := w.Usable
215 fw := min(max(u.W*cmp.Or(w.stashWidth, stashWidth)/100-2*w.border, 0)+2*w.border, u.W)
216 fh := min(max(u.H*stashHeight/100-2*w.border, 0)+2*w.border, u.H)
217 return Rect{X: u.X + (u.W-fw)/2, Y: u.Y + (u.H-fh)/2, W: fw, H: fh}
218}
219
220// stashLayout places the stash: the selected window centred, its
221// neighbors gap% of the usable width away, showing what the margins
222// leave of them, the others hidden. cover is the covering fullscreen
223// window, if any.
224func (w *Workspace) stashLayout(focusedID, cover WindowID) []Placement {
225 u := w.Usable
226 center := w.stashRect()
227 fw, fh := center.W, center.H
228 // Peeks stay in the margins: they never overlap the selected window.
229 gap := u.W * w.stashGap / 100
230 peek := max((u.W-fw)/2-gap, 0)
231 out := make([]Placement, 0, len(w.Stash))
232 for i, f := range w.Stash {
233 p := Placement{ID: f.ID, Floating: true, Focused: f.ID == focusedID, Inset: ports.SideAll}
234 switch {
235 case f.ID == cover:
236 p.Rect, p.Fullscreen, p.Inset = w.Output, true, 0
237 case w.stashHidden || cover != 0 && !w.stashOverCover():
238 p.Hidden = true
239 case i == w.stashAt:
240 p.Rect = center
241 case peek > 0 && i == w.stashAt-1:
242 p.Rect, p.Peek = Rect{X: center.X - gap - fw, Y: center.Y, W: fw, H: fh}, true
243 case peek > 0 && i == w.stashAt+1:
244 p.Rect, p.Peek = Rect{X: center.X + fw + gap, Y: center.Y, W: fw, H: fh}, true
245 default:
246 p.Hidden = true
247 }
248 out = append(out, p)
249 }
250 return out
251}