layout.go RawHistoryFolder

1package core
2
3import (
4	"fmt"
5	"math"
6	"math/bits"
7	"slices"
8	"strconv"
9	"strings"
10	"time"
11
12	"github.com/bnema/neferwl/internal/ports"
13)
14
15type WindowID = ports.WindowID
16
17type Rect = ports.Rect
18
19type Width struct{ Num, Den, Pixels int }
20
21func ParseWidth(s string) (Width, error) {
22	if s == "1" {
23		return Width{Num: 1, Den: 1}, nil
24	}
25	if strings.HasSuffix(s, "px") {
26		n, e := strconv.Atoi(strings.TrimSuffix(s, "px"))
27		if e == nil && n > 0 {
28			return Width{Pixels: n}, nil
29		}
30	} else if strings.HasSuffix(s, "%") {
31		n, e := strconv.Atoi(strings.TrimSuffix(s, "%"))
32		if e == nil && n > 0 && n <= 100 {
33			return Width{Num: n, Den: 100}, nil
34		}
35	} else {
36		p := strings.Split(s, "/")
37		if len(p) == 2 {
38			a, e1 := strconv.Atoi(p[0])
39			b, e2 := strconv.Atoi(p[1])
40			if e1 == nil && e2 == nil && a > 0 && a <= b {
41				return Width{Num: a, Den: b}, nil
42			}
43		}
44	}
45	return Width{}, fmt.Errorf("invalid width %q", s)
46}
47
48// same reports whether v and o are the same width: 1/2 and 50/100 are.
49func (v Width) same(o Width) bool {
50	if v.Pixels > 0 || o.Pixels > 0 || v.Den == 0 || o.Den == 0 {
51		return v == o
52	}
53	return v.Num*o.Den == o.Num*v.Den
54}
55
56// Resolve returns column pixels: round(frac * (usableW - gaps)) - gaps.
57// Widths are clamped to the available area (zero if no space remains).
58func (v Width) Resolve(usableW, gaps int) int {
59	if usableW <= 0 {
60		return 0
61	}
62	gaps = min(max(gaps, 0), usableW/2)
63	limit := max(usableW-2*gaps, 0)
64	if limit == 0 {
65		return 0
66	}
67	if v.Pixels > 0 {
68		return min(v.Pixels, limit)
69	}
70	if v.Den <= 0 || v.Num <= 0 {
71		return 1
72	}
73	// Cap invalid fractions above one before multiplying; the result would
74	// be clamped to the available width anyway.
75	num, den := uint64(min(v.Num, v.Den)), uint64(v.Den)
76	avail := uint64(usableW - gaps)
77	q, r := avail/den, avail%den
78	hi, lo := bits.Mul64(r, num)
79	lo, carry := bits.Add64(lo, den/2, 0)
80	hi += carry
81	// hi < den: r and num are both smaller than den.
82	fraction, _ := bits.Div64(hi, lo, den)
83	rounded := q*num + fraction // no larger than avail (plus rounding)
84	if rounded <= uint64(gaps) {
85		return 1
86	}
87	return int(min(rounded-uint64(gaps), uint64(limit)))
88}
89
90// Column.Width is zero (auto) until the user picks a preset: auto columns share
91// the usable width equally, up to MaxColumns visible at once.
92type Column struct {
93	Windows   []WindowID
94	Width     Width
95	FullWidth bool
96	// Expanded is the fixed-overflow wide column: max-columns - 1 cells,
97	// the others share the last one. One column at most has it.
98	Expanded bool
99	Focus    int
100	// Slot is the declared column number (workspace.<name>.column.N) of a
101	// slot window; 0 for normal columns.
102	Slot int
103	// Shares are the row heights set by set-window-height, in percent, in
104	// Windows order. They apply only while there is one per window and
105	// they sum to 100; otherwise the rows share the height equally.
106	Shares []int
107}
108type Placement struct {
109	ID                          WindowID
110	Rect                        Rect
111	Fullscreen, Focused, Hidden bool
112	// Floating windows sit at their own size. Below marks a native float
113	// ordered behind the columns (never a stashed window).
114	Floating, Below bool
115	// Peek is a stashed window peeking in beside the selected one, or an
116	// overview preview of a neighbor workspace: dimmed.
117	Peek bool
118	// Preview is the scale of an overview preview: its buffer is drawn
119	// that much smaller in Rect, the client keeps its size. 0 otherwise.
120	Preview float64
121	// Neighbors are the sides touching a visible tile on this output.
122	// Inset reserves room for drawn lines: all sides of a float; for
123	// tiles without gaps, only the right and bottom shared sides.
124	Neighbors, Inset ports.Sides
125}
126
127// Overflow says what happens past MaxColumns columns.
128type Overflow string
129
130const (
131	// OverflowScroll opens further columns to the right; the view scrolls.
132	OverflowScroll Overflow = "scroll"
133	// OverflowFixed keeps every column on screen: past the max, each new
134	// column splits the newest one, alternating top/bottom and left/right
135	// (dwindle spiral).
136	OverflowFixed Overflow = "fixed"
137)
138
139type Workspace struct {
140	// ID stays with the workspace across reorder and monitor moves.
141	ID uint64
142	// Name is set for workspaces declared in config; empty for dynamic ones.
143	Name string
144	// overviewAfter is the numbered workspace from which this named workspace
145	// was invoked. It only orders overview rows; numbering stays unchanged.
146	overviewAfter *Workspace
147	Overflow      Overflow
148	Columns       []Column
149	// maximized remembers recently maximized columns by window ID, newest
150	// first; stack() filters removed windows at read time.
151	maximized []WindowID
152	Focus     int
153	ViewX     int
154	// shift slides the columns on screen past ViewX, in logical pixels,
155	// while a swipe follows the fingers or its spring (motion) lands.
156	shift  float64
157	motion *motion
158	// Output is the effective viewport in monitor coordinates: the whole
159	// monitor unless the workspace has a size override, then a centered
160	// rectangle no larger than the monitor. Fullscreen fills it.
161	Output Rect
162	// Usable is the layer reservations of the monitor intersected with Output.
163	Usable Rect
164	// monitor is the full logical monitor and reserved its area left by
165	// layer surfaces, both from the monitor; size is the override (0 inherits).
166	monitor, reserved Rect
167	size              [2]int
168	Gaps              int
169	MaxColumns        int
170	// border is drawn around floating windows, inside their rect.
171	border     int
172	presets    []Width
173	fullscreen WindowID
174	// left is the window whose fullscreen the user left for another
175	// window: its client requests are refused until it is focused again,
176	// so an app that re-asks at once (Wine) does not cover that window.
177	left WindowID
178	// Floats are native floating windows in user selection order;
179	// below/above and dialog groups determine their layout order.
180	Floats     []Float
181	floatFocus bool
182	// Stash holds the windows set aside by toggle-window-stash, left to
183	// right (stash.go); stashAt is the selected one. stashFocus is set
184	// while it has the focus, under a focused native float.
185	Stash       []Float
186	stashAt     int
187	stashFocus  bool
188	stashHidden bool
189	// stashWidth is the width of a stashed window (0: the default) and
190	// stashGap the space between it and its neighbors, in percent of the
191	// usable width.
192	stashWidth, stashGap int
193	// hiddenFullscreen holds a stashed window's fullscreen while the stash
194	// is hidden, restored when shown.
195	hiddenFullscreen WindowID
196	// home is the monitor (key or connector) the workspace belongs to; ""
197	// means the one it is on. On another monitor it is a guest; homePos is
198	// its position there, where it returns.
199	home    string
200	homePos int
201	// termAt is when core last spawned a terminal for this workspace.
202	termAt time.Time
203}
204
205// origPlace remembers where a stashed window was: its column, its row in a
206// stacked column, the column's width and slot.
207type origPlace struct {
208	col, row, slot int
209	// stacked holds the windows left in the column: the window returns to
210	// its row in the column that still holds one of them.
211	stacked  []WindowID
212	width    Width
213	expanded bool
214	// fullWidth is the column's maximize-column state.
215	fullWidth bool
216}
217
218// Float is a floating window and its client size, logical.
219type Float struct {
220	ID   WindowID
221	W, H int
222	// below places a covering native float behind columns and stash.
223	below bool
224	// back records the former column of a stashed window or a free float;
225	// nil for native floats.
226	back *origPlace
227	// free marks a window the user floated (toggle-floating) or dragged:
228	// core owns its size and its centre is at cx, cy, fractions of the
229	// usable area, so it keeps its place on any output.
230	free   bool
231	cx, cy float64
232}
233
234func (w *Workspace) empty() bool {
235	return len(w.Columns) == 0 && len(w.Floats) == 0 && len(w.Stash) == 0
236}
237
238// windows lists every window: columns first, then the stash, then native
239// floats.
240func (w *Workspace) windows() []WindowID {
241	var ids []WindowID
242	for _, c := range w.Columns {
243		ids = append(ids, c.Windows...)
244	}
245	for _, f := range w.Stash {
246		ids = append(ids, f.ID)
247	}
248	for _, f := range w.Floats {
249		ids = append(ids, f.ID)
250	}
251	return ids
252}
253
254func (w *Workspace) floatIndex(id WindowID) int {
255	return slices.IndexFunc(w.Floats, func(f Float) bool { return f.ID == id })
256}
257
258// coversFloat uses the placed rectangle, not a map-time size. Allow up to
259// two border widths plus two logical pixels per axis for scale rounding.
260func (w *Workspace) coversFloat(f Float) bool {
261	if w.fullscreen == f.ID || w.Usable.W <= 0 || w.Usable.H <= 0 {
262		return false
263	}
264	r := w.floatRect(f)
265	return w.Usable.W-r.W <= 2*w.border+2 && w.Usable.H-r.H <= 2*w.border+2
266}
267
268// reconcileFloats promotes floats that no longer cover, without raising
269// existing covering floats on automatic geometry changes. It also drops
270// float focus when no raised float remains on top.
271func (w *Workspace) reconcileFloats() {
272	for i := range w.Floats {
273		if w.Floats[i].below && !w.coversFloat(w.Floats[i]) && w.fullscreen != w.Floats[i].ID {
274			w.Floats[i].below = false
275		}
276	}
277	if len(w.Floats) == 0 || w.Floats[len(w.Floats)-1].below {
278		w.floatFocus = false
279	}
280}
281
282// canLeaveFloat reports whether columns or a shown stash can receive focus.
283func (w *Workspace) canLeaveFloat() bool {
284	return len(w.Columns) > 0 || !w.stashHidden && len(w.Stash) > 0
285}
286
287// leaveFloat moves focus to the columns or shown stash under the float.
288// Without either, it keeps focus and reports false.
289func (w *Workspace) leaveFloat() bool {
290	if !w.canLeaveFloat() {
291		return false
292	}
293	w.floatFocus = false
294	w.raiseColumns()
295	return true
296}
297
298// raiseColumns moves only covering floats above the columns behind them.
299// The user-selected column group rises as one; dialogs remain above it.
300func (w *Workspace) raiseColumns() {
301	for i := range w.Floats {
302		if w.coversFloat(w.Floats[i]) {
303			w.Floats[i].below = true
304		}
305	}
306}
307
308// AddFloating shows a native floating window over the columns and the
309// stash, and focuses it.
310func (w *Workspace) AddFloating(id WindowID, width, height int) {
311	if id == 0 || w.has(id) {
312		return
313	}
314	w.Floats = append(w.Floats, Float{ID: id, W: width, H: height})
315	w.floatFocus = true
316}
317
318// ResizeFloating records the size a floating window draws. Core sizes a
319// free float: its reports only fill a missing size.
320func (w *Workspace) ResizeFloating(id WindowID, width, height int) {
321	if i := w.floatIndex(id); i >= 0 && (!w.Floats[i].free || w.Floats[i].W <= 0) {
322		w.Floats[i].W, w.Floats[i].H = width, height
323		w.reconcileFloats()
324	}
325}
326
327// has reports whether the workspace holds the window.
328func (w *Workspace) has(id WindowID) bool {
329	if w.isFloat(id) {
330		return true
331	}
332	for _, c := range w.Columns {
333		for _, v := range c.Windows {
334			if v == id {
335				return true
336			}
337		}
338	}
339	return false
340}
341
342func (w *Workspace) Focused() (WindowID, bool) {
343	// Nothing else is drawn: the covering fullscreen window has the focus.
344	if full := w.cover(); full != 0 {
345		return full, true
346	}
347	if w.floatFocus && len(w.Floats) > 0 && !w.Floats[len(w.Floats)-1].below {
348		return w.Floats[len(w.Floats)-1].ID, true
349	}
350	if w.stashFocused() {
351		return w.Stash[w.stashAt].ID, true
352	}
353	if w.Focus < 0 || w.Focus >= len(w.Columns) {
354		return 0, false
355	}
356	c := w.Columns[w.Focus]
357	if c.Focus < 0 || c.Focus >= len(c.Windows) {
358		return 0, false
359	}
360	return c.Windows[c.Focus], true
361}
362func (w *Workspace) AddWindow(id WindowID) {
363	if id == 0 || w.has(id) {
364		return
365	}
366	if w.cover() != 0 {
367		// A new window never moves the view off a covering fullscreen
368		// window (ADR 011): it waits, hidden, after the focused column.
369		at := len(w.Columns)
370		if w.Overflow != OverflowFixed {
371			// A covering float or stashed window may have no column under it.
372			at = min(w.Focus+1, len(w.Columns))
373		}
374		w.Columns = slices.Insert(w.Columns, at, Column{Windows: []WindowID{id}})
375		return
376	}
377	w.addColumn(Column{Windows: []WindowID{id}})
378}
379
380// addColumn places a new column like a new window and focuses it.
381func (w *Workspace) addColumn(col Column) {
382	at := 0
383	if w.Overflow == OverflowFixed {
384		// The spiral depends only on window order: new windows go last.
385		at = len(w.Columns)
386	} else if len(w.Columns) > 0 {
387		at = w.Focus + 1
388	}
389	if w.Overflow == OverflowFixed && len(w.Columns) > 0 {
390		w.unmaximize()
391	}
392	w.Columns = append(w.Columns, Column{})
393	copy(w.Columns[at+1:], w.Columns[at:])
394	w.Columns[at] = col
395	w.Focus = at
396	w.scroll()
397}
398
399// AddSlotWindow places a slot window in its own column, ordered by slot
400// number before the other columns. Focus stays on the focused window: slot
401// windows are automatic events (ADR 011 golden rule).
402func (w *Workspace) AddSlotWindow(id WindowID, slot int, width Width) {
403	if id == 0 || w.has(id) {
404		return
405	}
406	// Right after the last slot column with a lower number, else first.
407	at := 0
408	for i, c := range w.Columns {
409		if c.Slot > 0 && c.Slot < slot {
410			at = i + 1
411		}
412	}
413	w.Columns = slices.Insert(w.Columns, at, Column{Windows: []WindowID{id}, Width: width, Slot: slot})
414	if len(w.Columns) > 1 && at <= w.Focus {
415		w.Focus++
416	}
417	w.scroll()
418}
419
420// setSlotWidth updates the width of slot column n, if present.
421func (w *Workspace) setSlotWidth(n int, width Width) {
422	for i := range w.Columns {
423		if w.Columns[i].Slot == n {
424			w.Columns[i].Width = width
425		}
426	}
427	w.scroll()
428}
429
430// inSlot reports whether window id is in slot column n.
431func (w *Workspace) inSlot(id WindowID, n int) bool {
432	for _, c := range w.Columns {
433		if c.Slot == n && slices.Contains(c.Windows, id) {
434			return true
435		}
436	}
437	return false
438}
439
440// unslot turns slot column n into a normal column.
441func (w *Workspace) unslot(n int) {
442	for i := range w.Columns {
443		if w.Columns[i].Slot == n {
444			w.Columns[i].Slot = 0
445		}
446	}
447}
448
449func (w *Workspace) RemoveWindow(id WindowID) {
450	w.maximized = slices.DeleteFunc(w.maximized, func(v WindowID) bool { return v == id })
451	if w.fullscreen == id {
452		w.fullscreen = 0
453	}
454	if w.hiddenFullscreen == id {
455		w.hiddenFullscreen = 0
456	}
457	if w.left == id {
458		w.left = 0
459	}
460	if i := w.floatIndex(id); i >= 0 {
461		w.Floats = slices.Delete(w.Floats, i, i+1)
462		if len(w.Floats) == 0 || w.Floats[len(w.Floats)-1].below {
463			w.floatFocus = false
464		}
465		return
466	}
467	if i := w.stashIndex(id); i >= 0 {
468		// The right neighbor takes the selection, else the left one.
469		w.Stash = slices.Delete(w.Stash, i, i+1)
470		if i < w.stashAt || w.stashAt == len(w.Stash) {
471			w.stashAt = max(w.stashAt-1, 0)
472		}
473		if len(w.Stash) == 0 {
474			w.stashFocus, w.stashHidden = false, false
475		}
476		return
477	}
478	for i := range w.Columns {
479		for j, v := range w.Columns[i].Windows {
480			if v != id {
481				continue
482			}
483			c := &w.Columns[i]
484			c.Windows = append(c.Windows[:j], c.Windows[j+1:]...)
485			c.Shares = nil
486			if len(c.Windows) == 0 {
487				w.Columns = append(w.Columns[:i], w.Columns[i+1:]...)
488				if len(w.Columns) == 0 {
489					w.Focus = 0
490					w.ViewX = 0
491					return
492				}
493				if i < w.Focus || (i == w.Focus && i > 0) {
494					w.Focus--
495				}
496			} else {
497				if j < c.Focus {
498					c.Focus--
499				} else if j == c.Focus && c.Focus > 0 {
500					c.Focus--
501				}
502			}
503			w.scroll()
504			return
505		}
506	}
507}
508
509// FocusID selects a window and scrolls its column into view; a native
510// float is raised, a stashed window is selected.
511func (w *Workspace) FocusID(id WindowID) bool {
512	if i := w.floatIndex(id); i >= 0 {
513		f := w.Floats[i]
514		f.below = false
515		w.Floats = append(slices.Delete(w.Floats, i, i+1), f)
516		w.floatFocus = true
517		return true
518	}
519	if i := w.stashIndex(id); i >= 0 {
520		if w.stashHidden {
521			return false
522		}
523		w.stashAt, w.stashFocus, w.floatFocus = i, true, false
524		return true
525	}
526	for i := range w.Columns {
527		for j, v := range w.Columns[i].Windows {
528			if v == id {
529				w.floatFocus, w.stashFocus = false, false
530				w.raiseColumns()
531				if w.Overflow == OverflowFixed && w.Focus != i && w.Focus < len(w.Columns) && !w.Columns[i].FullWidth {
532					w.unmaximize()
533				}
534				w.Focus = i
535				w.Columns[i].Focus = j
536				w.scroll()
537				return true
538			}
539		}
540	}
541	return false
542}
543
544// FocusColumn moves focus to the column on that side. From a covering
545// fullscreen window it leaves fullscreen for the window there (leaveCover);
546// false means it stays, with no window on that side.
547func (w *Workspace) FocusColumn(dir int) bool {
548	if w.pinned() {
549		return w.leaveCover(func() { w.focusColumn(dir) })
550	}
551	w.focusColumn(dir)
552	return true
553}
554
555func (w *Workspace) focusColumn(dir int) {
556	if w.floatFocus {
557		// The first move leaves the native float for the stash or columns,
558		// if there is one: otherwise the float keeps the focus.
559		w.leaveFloat()
560		return
561	}
562	if w.stashFocused() {
563		// Inside the stash, stopping at its ends: only hiding it leaves.
564		if (dir == -1 || dir == 1) && w.stashAt+dir >= 0 && w.stashAt+dir < len(w.Stash) {
565			w.stashAt += dir
566		}
567		return
568	}
569	if i := w.columnToward(dir); i >= 0 {
570		w.raiseColumns()
571		if w.Overflow == OverflowFixed {
572			w.unmaximize()
573		}
574		w.Focus = i
575		w.scroll()
576	}
577}
578
579// columnToward returns the column focus-column-left/right (dir -1/1) goes
580// to, or -1 at the edge. Scroll overflow follows the column order; fixed
581// overflow stacks columns (spiral, expanded strips), so it follows the
582// screen: the column on that side.
583func (w *Workspace) columnToward(dir int) int {
584	if len(w.Columns) == 0 || (dir != -1 && dir != 1) || w.pinned() {
585		return -1
586	}
587	if w.onScreenFocus() {
588		return w.screenNeighbor(dir, 0)
589	}
590	if i := w.Focus + dir; i >= 0 && i < len(w.Columns) {
591		return i
592	}
593	return -1
594}
595
596// onScreenFocus reports whether directional focus follows the screen:
597// fixed overflow with every column visible.
598func (w *Workspace) onScreenFocus() bool {
599	return w.Overflow == OverflowFixed && w.fullscreen == 0 && !w.Columns[w.Focus].FullWidth
600}
601
602// FocusWindow moves focus inside the column, then to a fixed-overflow column
603// above or below on screen. Up at the top raises the topmost demoted covering
604// float; false means no window is available in that direction. From a
605// covering fullscreen window it leaves fullscreen for that window.
606func (w *Workspace) FocusWindow(dir int) bool {
607	if w.pinned() {
608		return w.leaveCover(func() { w.focusWindow(dir) })
609	}
610	return w.focusWindow(dir)
611}
612
613func (w *Workspace) focusWindow(dir int) bool {
614	if w.floatFocus {
615		return w.leaveFloat()
616	}
617	if w.stashFocused() {
618		// The stash is one row: up and down do nothing there.
619		return true
620	}
621	if len(w.Columns) == 0 || (dir != -1 && dir != 1) {
622		return false
623	}
624	c := &w.Columns[w.Focus]
625	if c.Focus+dir >= 0 && c.Focus+dir < len(c.Windows) {
626		c.Focus += dir
627		w.raiseColumns()
628		w.scroll()
629		return true
630	}
631	if w.onScreenFocus() {
632		if i := w.screenNeighbor(0, dir); i >= 0 {
633			w.Focus = i
634			w.raiseColumns()
635			w.Columns[i].Focus = 0
636			if dir < 0 {
637				w.Columns[i].Focus = len(w.Columns[i].Windows) - 1
638			}
639			return true
640		}
641	}
642	if dir < 0 {
643		for i := len(w.Floats) - 1; i >= 0; i-- {
644			if f := w.Floats[i]; f.below && w.coversFloat(f) {
645				return w.FocusID(f.ID)
646			}
647		}
648	}
649	return false
650}
651
652// screenNeighbor returns the column on screen next to the focused one, left
653// or right (dx -1/1) or above or below (dy -1/1), or -1. The closest wins,
654// then the one sharing the longest edge, then the nearest in column order.
655func (w *Workspace) screenNeighbor(dx, dy int) int {
656	rects := w.columnRects()
657	cur := rects[w.Focus]
658	best, bestDist, bestOverlap := -1, 0, 0
659	for i, r := range rects {
660		var overlap, dist int
661		switch {
662		case dx < 0:
663			overlap, dist = min(cur.Y+cur.H, r.Y+r.H)-max(cur.Y, r.Y), cur.X-(r.X+r.W)
664		case dx > 0:
665			overlap, dist = min(cur.Y+cur.H, r.Y+r.H)-max(cur.Y, r.Y), r.X-(cur.X+cur.W)
666		case dy < 0:
667			overlap, dist = min(cur.X+cur.W, r.X+r.W)-max(cur.X, r.X), cur.Y-(r.Y+r.H)
668		default:
669			overlap, dist = min(cur.X+cur.W, r.X+r.W)-max(cur.X, r.X), r.Y-(cur.Y+cur.H)
670		}
671		if i == w.Focus || overlap <= 0 || dist < 0 {
672			continue
673		}
674		closer := best >= 0 && (dist < bestDist || dist == bestDist && (overlap > bestOverlap ||
675			overlap == bestOverlap && abs(i-w.Focus) < abs(best-w.Focus)))
676		if best < 0 || closer {
677			best, bestDist, bestOverlap = i, dist, overlap
678		}
679	}
680	return best
681}
682
683func abs(n int) int { return max(n, -n) }
684func (w *Workspace) MoveColumn(dir int) {
685	if w.onFloat() {
686		return
687	}
688	if (dir == -1 || dir == 1) && w.Focus+dir >= 0 && w.Focus+dir < len(w.Columns) {
689		i := w.Focus
690		w.Columns[i], w.Columns[i+dir] = w.Columns[i+dir], w.Columns[i]
691		w.Focus += dir
692		w.scroll()
693	}
694}
695
696// MoveWindow moves the focused tile up or down (dir -1/1) inside its
697// column; focus follows it. A slot window leaving row 0 releases the slot.
698func (w *Workspace) MoveWindow(dir int) {
699	if w.onFloat() || len(w.Columns) == 0 || (dir != -1 && dir != 1) || w.fullscreenHides() {
700		return
701	}
702	c := &w.Columns[w.Focus]
703	to := c.Focus + dir
704	if to < 0 || to >= len(c.Windows) {
705		return
706	}
707	if c.Focus == 0 || to == 0 {
708		w.dropSlotOf(c.Windows[0])
709	}
710	c.Windows[c.Focus], c.Windows[to] = c.Windows[to], c.Windows[c.Focus]
711	if validShares(*c) {
712		c.Shares[c.Focus], c.Shares[to] = c.Shares[to], c.Shares[c.Focus]
713	}
714	c.Focus = to
715	w.scroll()
716}
717
718// ResizeColumn changes the focused column's width by pct percent of the
719// usable width, clamped to 10-100%. An auto column becomes explicit. Fixed
720// overflow shares the width equally: nothing changes there.
721func (w *Workspace) ResizeColumn(pct int) {
722	if w.onFloat() || len(w.Columns) == 0 || w.Overflow == OverflowFixed || w.fullscreenHides() {
723		return
724	}
725	w.unmaximize()
726	g := w.gap()
727	avail := w.Usable.W - g
728	if avail <= 0 {
729		return
730	}
731	// Inverse of Width.Resolve: width = round(p/100 * avail) - g.
732	cur := (200*(w.columnWidth(w.Focus)+g) + avail) / (2 * avail)
733	w.Columns[w.Focus].Width = Width{Num: min(max(cur+pct, 10), 100), Den: 100}
734	w.scroll()
735}
736
737// minShare is the smallest row height set-window-height leaves, percent.
738const minShare = 10
739
740// ResizeRow changes the focused window's height share by pct percent; the
741// other rows of its column give or take it in proportion to their shares.
742// Every row keeps at least minShare percent.
743func (w *Workspace) ResizeRow(pct int) {
744	if w.onFloat() || len(w.Columns) == 0 || w.fullscreenHides() {
745		return
746	}
747	c := &w.Columns[w.Focus]
748	n := len(c.Windows)
749	if n < 2 || n*minShare > 100 {
750		return
751	}
752	shares := c.Shares
753	if !validShares(*c) {
754		shares = make([]int, n)
755		for i := range shares {
756			shares[i] = 100 / n
757		}
758		shares[n-1] += 100 % n
759	} else {
760		shares = slices.Clone(shares)
761	}
762	f := c.Focus
763	target := min(max(shares[f]+pct, minShare), 100-minShare*(n-1))
764	delta := target - shares[f]
765	if delta == 0 {
766		return
767	}
768	// The others share -delta in proportion to their room: what they have
769	// above minShare when shrinking, their share when growing.
770	weight := func(v int) int {
771		if delta > 0 {
772			return v - minShare
773		}
774		return v
775	}
776	total := 0
777	for i, v := range shares {
778		if i != f {
779			total += weight(v)
780		}
781	}
782	left := delta
783	for i, v := range shares {
784		if i == f {
785			continue
786		}
787		part := 0
788		if total > 0 {
789			part = delta * weight(v) / total
790		}
791		shares[i] = v - part
792		left -= part
793	}
794	shares[f] = target
795	// Rounding: the remainder goes to the rows that still have room, last
796	// first.
797	for i := n - 1; left != 0 && i >= 0; i-- {
798		if i == f {
799			continue
800		}
801		step := left
802		if left > 0 {
803			step = min(left, shares[i]-minShare)
804		}
805		shares[i] -= step
806		left -= step
807	}
808	c.Shares = shares
809}
810
811// takeColumn removes the focused column and returns it, as a normal column.
812func (w *Workspace) takeColumn() (Column, bool) {
813	if len(w.Columns) == 0 || w.onFloat() {
814		return Column{}, false
815	}
816	col := w.Columns[w.Focus]
817	col.Slot, col.Expanded = 0, false
818	if slices.Contains(col.Windows, w.fullscreen) {
819		w.fullscreen = 0
820	}
821	if slices.Contains(col.Windows, w.left) {
822		w.left = 0
823	}
824	w.Columns = slices.Delete(w.Columns, w.Focus, w.Focus+1)
825	w.Focus = min(w.Focus, max(len(w.Columns)-1, 0))
826	if len(w.Columns) == 0 {
827		w.ViewX = 0
828	} else {
829		w.scroll()
830	}
831	return col, true
832}
833
834// insertColumn adds a column at index at (clamped) and focuses it.
835func (w *Workspace) insertColumn(at int, col Column) {
836	at = min(max(at, 0), len(w.Columns))
837	if w.Overflow == OverflowFixed && len(w.Columns) > 0 {
838		w.unmaximize()
839	}
840	w.Columns = slices.Insert(w.Columns, at, col)
841	w.Focus = at
842	w.scroll()
843}
844
845// CycleWidth steps the focused column through the presets. Fixed overflow
846// ignores presets: it toggles the expanded column instead.
847func (w *Workspace) CycleWidth() {
848	if w.Overflow == OverflowFixed {
849		w.toggleExpanded()
850		return
851	}
852	if len(w.Columns) == 0 || len(w.presets) == 0 || w.onFloat() {
853		return
854	}
855	// auto → presets in order → auto; a width set by set-column-width
856	// restarts at the first preset.
857	c := &w.Columns[w.Focus]
858	next := Width{}
859	if c.Width == next || !slices.ContainsFunc(w.presets, c.Width.same) {
860		next = w.presets[0]
861	}
862	for i, v := range w.presets {
863		if v.same(c.Width) && i+1 < len(w.presets) {
864			next = w.presets[i+1]
865			break
866		}
867	}
868	c.Width = next
869	w.unmaximize()
870	w.scroll()
871}
872
873// toggleExpanded makes the focused column the expanded one, or shrinks it
874// back when it already is. One column at most is expanded.
875func (w *Workspace) toggleExpanded() {
876	if len(w.Columns) < 2 || max(w.MaxColumns, 1) < 2 || w.onFloat() {
877		return
878	}
879	on := !w.Columns[w.Focus].Expanded
880	for i := range w.Columns {
881		w.Columns[i].Expanded = false
882	}
883	w.Columns[w.Focus].Expanded = on
884	w.unmaximize()
885}
886
887// maximize sets FullWidth on column i; unlike ToggleFullWidth it never toggles.
888func (w *Workspace) maximize(i int) {
889	if i >= 0 && i < len(w.Columns) {
890		w.Columns[i].FullWidth = true
891		id := w.Columns[i].Windows[w.Columns[i].Focus]
892		w.maximized = slices.DeleteFunc(w.maximized, func(v WindowID) bool { return v == id })
893		w.maximized = slices.Insert(w.maximized, 0, id)
894	}
895}
896
897// unmaximize clears the focused column in scroll mode, or all maximized
898// columns in fixed mode (including after Escape changed focus).
899func (w *Workspace) unmaximize() {
900	for i := range w.Columns {
901		if w.Overflow == OverflowFixed || i == w.Focus {
902			w.Columns[i].FullWidth = false
903		}
904	}
905	if w.Overflow == OverflowFixed {
906		w.maximized = nil
907	}
908}
909
910// transferMaximization changes the maximized column without forgetting MRU.
911func (w *Workspace) transferMaximization(i int) {
912	if i == w.Focus || i < 0 || i >= len(w.Columns) {
913		return
914	}
915	w.Columns[w.Focus].FullWidth = false
916	w.maximize(i)
917}
918
919// ToggleFullWidth expands the focused tiled column without changing its saved width.
920func (w *Workspace) ToggleFullWidth() {
921	if len(w.Columns) == 0 || w.onFloat() {
922		return
923	}
924	if w.Columns[w.Focus].FullWidth {
925		w.unmaximize()
926	} else {
927		w.maximize(w.Focus)
928	}
929	w.scroll()
930}
931
932func (w *Workspace) ToggleFullscreen() {
933	id, ok := w.Focused()
934	if !ok {
935		return
936	}
937	if w.fullscreen == id {
938		// The user was on it: it keeps the focus, in front of floats that
939		// mapped meanwhile. Cleared first, so that floats it covers drop
940		// below the columns.
941		w.fullscreen = 0
942		w.FocusID(id)
943		w.endFullscreen()
944		return
945	}
946	w.fullscreen = id
947	w.scroll()
948}
949
950// endFullscreen ends the fullscreen: floats it kept demoted are promoted
951// again and the view follows the focus.
952func (w *Workspace) endFullscreen() {
953	w.fullscreen = 0
954	w.reconcileFloats()
955	w.scroll()
956}
957
958// leaveFullscreen ends the fullscreen for another window the user chose,
959// and refuses its client's requests until it is focused again (mayCover,
960// settleLeft).
961func (w *Workspace) leaveFullscreen() {
962	if w.fullscreen != 0 {
963		w.left = w.fullscreen
964	}
965	w.endFullscreen()
966}
967
968// Activate focuses a window and makes it visible: it leaves another
969// window's fullscreen, which hides everything else.
970func (w *Workspace) Activate(id WindowID) {
971	if i := w.stashIndex(id); i >= 0 && w.stashHidden {
972		// A hidden stashed window must be seen: show the stash with it,
973		// selected. Another window's fullscreen leaves first, so its own
974		// pending fullscreen comes back.
975		w.leaveFullscreen()
976		w.stashAt = i
977		w.showStash()
978	}
979	if w.fullscreen != 0 && w.fullscreen != id {
980		w.leaveFullscreen()
981	}
982	w.FocusID(id)
983}
984
985// SetFullscreen applies a client request. It never moves focus: client
986// requests are automatic events (ADR 011 golden rule).
987func (w *Workspace) SetFullscreen(id WindowID, on bool) {
988	if on && !w.mayCover(id) {
989		return
990	}
991	if w.isFloat(id) {
992		if w.stashHidden && w.stashIndex(id) >= 0 {
993			// Applied when the stash is shown again.
994			if on {
995				w.hiddenFullscreen = id
996			} else if w.hiddenFullscreen == id {
997				w.hiddenFullscreen = 0
998			}
999			return
1000		}
1001	}
1002	if !w.has(id) {
1003		return
1004	}
1005	if !on {
1006		if w.fullscreen == id {
1007			w.endFullscreen()
1008		}
1009		return
1010	}
1011	w.fullscreen = id
1012	if !w.isFloat(id) {
1013		w.scroll()
1014	}
1015}
1016
1017// SetOutput sets the logical size of the monitor. The workspace viewport
1018// follows: the whole monitor, or the centered size override.
1019func (w *Workspace) SetOutput(width, height int) {
1020	old := w.monitor
1021	w.monitor = Rect{W: max(width, 0), H: max(height, 0)}
1022	if w.reserved == old {
1023		w.reserved = w.monitor
1024	}
1025	w.SetUsable(w.reserved)
1026}
1027
1028// SetUsable takes the monitor area left by layer surfaces, clamps negative
1029// dimensions to zero and clips it to the monitor. The usable rectangle is
1030// its intersection with the viewport. Effective gaps shrink to fit it.
1031func (w *Workspace) SetUsable(r Rect) {
1032	r.X = min(max(r.X, 0), w.monitor.W)
1033	r.Y = min(max(r.Y, 0), w.monitor.H)
1034	r.W = min(max(r.W, 0), w.monitor.W-r.X)
1035	r.H = min(max(r.H, 0), w.monitor.H-r.Y)
1036	w.reserved = r
1037	w.fit()
1038}
1039
1040// SetSize overrides the logical size of the workspace; a zero side inherits
1041// the monitor, one larger than it is clamped to it.
1042func (w *Workspace) SetSize(width, height int) {
1043	w.size = [2]int{max(width, 0), max(height, 0)}
1044	w.fit()
1045}
1046
1047// fit derives the viewport and usable area from the monitor, its reservations
1048// and the size override.
1049func (w *Workspace) fit() {
1050	side := func(want, have int) int {
1051		if want <= 0 {
1052			return have
1053		}
1054		return min(want, have)
1055	}
1056	vw, vh := side(w.size[0], w.monitor.W), side(w.size[1], w.monitor.H)
1057	w.Output = Rect{X: (w.monitor.W - vw) / 2, Y: (w.monitor.H - vh) / 2, W: vw, H: vh}
1058	r, o := w.reserved, w.Output
1059	x0, y0 := max(r.X, o.X), max(r.Y, o.Y)
1060	x1, y1 := min(r.X+r.W, o.X+o.W), min(r.Y+r.H, o.Y+o.H)
1061	u := Rect{X: min(max(x0, o.X), o.X+o.W), Y: min(max(y0, o.Y), o.Y+o.H)}
1062	u.W, u.H = max(x1-u.X, 0), max(y1-u.Y, 0)
1063	w.Usable = u
1064	w.reconcileFloats()
1065	w.scroll()
1066}
1067func (w *Workspace) SetGaps(g int) {
1068	if g < 0 {
1069		g = 0
1070	}
1071	w.Gaps = g
1072	w.scroll()
1073}
1074func (w *Workspace) SetPresets(v []Width) { w.presets = append([]Width(nil), v...) }
1075func (w *Workspace) SetMaxColumns(n int) {
1076	w.MaxColumns = n
1077	w.scroll()
1078}
1079
1080// overviewArea is the monitor area the overview lays rows out in: the whole
1081// area left by layer surfaces, whatever the workspace's size. Tiles keep the
1082// workspace's own geometry, scaled uniformly.
1083func (w *Workspace) overviewArea() Rect { return w.reserved }
1084
1085func (w *Workspace) gap() int { return min(w.Gaps, w.Usable.W/2, w.Usable.H/2) }
1086
1087// settleLeft drops the latch once its window has the focus again, however
1088// it got it; core calls it after each event.
1089func (w *Workspace) settleLeft() {
1090	if id, ok := w.Focused(); ok && id == w.left {
1091		w.left = 0
1092	}
1093}
1094
1095// mayCover reports whether a client may make id fullscreen. In fixed
1096// overflow it would cover the output at once and take the keyboard from
1097// the focused window, so only the focused window may; the request of
1098// another is refused and the user can still toggle-fullscreen it. A
1099// hidden stash's request waits for the show (showStash). A window whose
1100// fullscreen the user left may only once focused again, in any overflow.
1101func (w *Workspace) mayCover(id WindowID) bool {
1102	if id == w.left {
1103		if focused, ok := w.Focused(); !ok || focused != id {
1104			return false
1105		}
1106		w.left = 0
1107	}
1108	if w.Overflow != OverflowFixed || w.stashHidden && w.stashIndex(id) >= 0 {
1109		return true
1110	}
1111	focused, ok := w.Focused()
1112	return ok && focused == id
1113}
1114
1115// pinned reports whether the covering fullscreen window hides every other
1116// window: focus moves leave it (leaveCover), and the overview shows it as
1117// the front card of its row. Only in scroll overflow does moving to a
1118// tiled neighbor scroll it off and show the target instead.
1119func (w *Workspace) pinned() bool {
1120	c := w.cover()
1121	return c != 0 && (w.Overflow == OverflowFixed || w.isFloat(c))
1122}
1123
1124// leaveCover runs a focus move from the covering fullscreen window as if it
1125// were not fullscreen. When the move reaches another window, fullscreen
1126// ends and that window shows, focused; otherwise (an edge) nothing changes
1127// and the caller goes on to the next workspace or monitor.
1128func (w *Workspace) leaveCover(move func()) bool {
1129	w.focusCover()
1130	full := w.fullscreen
1131	w.fullscreen = 0
1132	move()
1133	if id, ok := w.Focused(); ok && id != full {
1134		w.fullscreen = full
1135		w.leaveFullscreen()
1136		return true
1137	}
1138	w.fullscreen = full
1139	return false
1140}
1141
1142// focusCover points the focus state at the covering fullscreen window,
1143// which Focused already reports, so a user action acts on the window on
1144// screen, not on one it hides.
1145func (w *Workspace) focusCover() {
1146	c := w.cover()
1147	if c == 0 {
1148		return
1149	}
1150	if w.isFloat(c) {
1151		w.FocusID(c)
1152		return
1153	}
1154	w.floatFocus, w.stashFocus = false, false
1155	for i, col := range w.Columns {
1156		if j := slices.Index(col.Windows, c); j >= 0 {
1157			w.Focus, w.Columns[i].Focus = i, j
1158		}
1159	}
1160}
1161
1162// cover returns the fullscreen window that covers the output, or 0. The
1163// fullscreen is exclusive: while it covers, no other window of the
1164// workspace is drawn, so the output can scan it out.
1165func (w *Workspace) cover() WindowID {
1166	if w.fullscreen == 0 {
1167		return 0
1168	}
1169	if w.isFloat(w.fullscreen) || w.Overflow == OverflowFixed {
1170		return w.fullscreen
1171	}
1172	// Scroll overflow: only while its column is aligned on the output.
1173	for i, c := range w.Columns {
1174		if slices.Contains(c.Windows, w.fullscreen) && w.columnX(i)-w.ViewX == 0 {
1175			return w.fullscreen
1176		}
1177	}
1178	return 0
1179}
1180
1181// fullscreenHides reports whether a fullscreen window hides the focused
1182// column's layout: the others (fixed) or its own column (scroll). Column
1183// edits would change what the user cannot see.
1184func (w *Workspace) fullscreenHides() bool {
1185	return w.fullscreen != 0 && (w.Overflow == OverflowFixed || w.fullscreenColumn(w.Focus))
1186}
1187
1188func (w *Workspace) fullscreenColumn(i int) bool {
1189	for _, id := range w.Columns[i].Windows {
1190		if id == w.fullscreen && id != 0 {
1191			return true
1192		}
1193	}
1194	return false
1195}
1196func (w *Workspace) columnWidth(i int) int {
1197	return w.columnWidthFor(i, false)
1198}
1199
1200func (w *Workspace) columnWidthFor(i int, ignoreFullWidth bool) int {
1201	if w.Columns[i].FullWidth && !ignoreFullWidth {
1202		return max(w.Usable.W-2*w.gap(), 0)
1203	}
1204	if w.fullscreenColumn(i) {
1205		return w.Output.W
1206	}
1207	g := w.gap()
1208	// Fixed overflow never scrolls, so presets would push columns off screen.
1209	if w.Columns[i].Width == (Width{}) || w.Overflow == OverflowFixed {
1210		if len(w.Columns) == 1 {
1211			return max(w.Usable.W-2*g, 0)
1212		}
1213		// Equal shares of the width left after gaps; a remainder under k pixels stays empty.
1214		k := min(len(w.Columns), max(w.MaxColumns, 1))
1215		return max((w.Usable.W-g*(k+1))/k, 0)
1216	}
1217	return w.Columns[i].Width.Resolve(w.Usable.W, g)
1218}
1219func (w *Workspace) columnX(i int) int {
1220	x := w.Usable.X + w.gap()
1221	for j := 0; j < i; j++ {
1222		x += w.columnWidth(j) + w.gap()
1223	}
1224	return x
1225}
1226func (w *Workspace) scroll() {
1227	before := w.ViewX
1228	defer func() { w.retarget(before) }()
1229	if w.Overflow == OverflowFixed {
1230		w.ViewX = 0
1231		return
1232	}
1233	if len(w.Columns) == 0 {
1234		return
1235	}
1236	left := w.columnX(w.Focus) - w.ViewX
1237	width := w.columnWidth(w.Focus)
1238	if w.fullscreenColumn(w.Focus) {
1239		w.ViewX += left - w.Output.X
1240		return
1241	}
1242	minX := w.Usable.X + w.gap()
1243	maxX := w.Usable.X + w.Usable.W - w.gap()
1244	if width > maxX-minX || left < minX {
1245		w.ViewX += left - minX
1246	} else if left+width > maxX {
1247		w.ViewX += left + width - maxX
1248	}
1249}
1250
1251// columnRects returns each column's area on screen, before stacking windows.
1252func (w *Workspace) columnRects() []Rect {
1253	return w.columnRectsFor(false)
1254}
1255
1256// columnRectsFor shares the fixed layout geometry with the overview's hidden
1257// columns card, without changing FullWidth or the clients' saved buffers.
1258func (w *Workspace) columnRectsFor(ignoreFullWidth bool) []Rect {
1259	g := w.gap()
1260	y, h := w.Usable.Y+g, max(w.Usable.H-2*g, 0)
1261	rects := make([]Rect, len(w.Columns))
1262	view := w.ViewX + w.shiftPixels()
1263	x := w.Usable.X + g
1264	for i := range w.Columns {
1265		width := w.columnWidthFor(i, ignoreFullWidth)
1266		rects[i] = Rect{X: x - view, Y: y, W: width, H: h}
1267		x += width + g
1268	}
1269	if !ignoreFullWidth && w.Overflow == OverflowFixed && len(w.Columns) > 0 && w.Columns[w.Focus].FullWidth {
1270		rects[w.Focus].X = w.Usable.X + g
1271		return rects
1272	}
1273	k := max(w.MaxColumns, 1)
1274	if e := slices.IndexFunc(w.Columns, func(c Column) bool { return c.Expanded }); w.Overflow == OverflowFixed && e >= 0 && k > 1 && len(w.Columns) > 1 {
1275		return w.expandedRects(e, k, y, h)
1276	}
1277	if w.Overflow != OverflowFixed || len(w.Columns) <= k {
1278		return rects
1279	}
1280	// The last slot holds the spiral; the columns before it keep their place.
1281	area := rects[k-1]
1282	for i := k - 1; i < len(w.Columns)-1; i++ {
1283		rects[i], area = split(area, g, (i-k+1)%2 == 0)
1284	}
1285	rects[len(rects)-1] = area
1286	return rects
1287}
1288
1289// expandedRects places expanded column e over k-1 cells in its place. The
1290// columns before it stack in a strip on its left, those after it on its
1291// right; the two strips share the last cell.
1292func (w *Workspace) expandedRects(e, k, y, h int) []Rect {
1293	g := w.gap()
1294	cell := max((w.Usable.W-g*(k+1))/k, 0)
1295	wide := (k-1)*cell + (k-2)*g
1296	rest := max(w.Usable.W-2*g-wide-g, 0)
1297	before, after := w.Columns[:e], w.Columns[e+1:]
1298	leftW, rightW := rest, rest
1299	if len(before) > 0 && len(after) > 0 {
1300		leftW = max((rest-g)/2, 0)
1301		rightW = max(rest-g-leftW, 0)
1302	}
1303	rects := make([]Rect, 0, len(w.Columns))
1304	x := w.Usable.X + g
1305	if len(before) > 0 {
1306		rects = append(rects, stackRects(Rect{X: x, Y: y, W: leftW, H: h}, len(before), g)...)
1307		x += leftW + g
1308	}
1309	rects = append(rects, Rect{X: x, Y: y, W: wide, H: h})
1310	if len(after) > 0 {
1311		rects = append(rects, stackRects(Rect{X: x + wide + g, Y: y, W: rightW, H: h}, len(after), g)...)
1312	}
1313	// Gaps wider than a narrow output would push columns past its edge.
1314	right := w.Usable.X + w.Usable.W
1315	for i := range rects {
1316		rects[i].X = min(rects[i].X, right)
1317		rects[i].W = min(rects[i].W, right-rects[i].X)
1318	}
1319	return rects
1320}
1321
1322// stackRects cuts r in n rows with a gap between them; the last row takes
1323// the rounding remainder. Rows past the bottom (gaps taller than r) are
1324// clamped to it, like windows stacked in a column.
1325func stackRects(r Rect, n, gap int) []Rect {
1326	rows := make([]Rect, n)
1327	avail := max(r.H-(n-1)*gap, 0)
1328	height := avail / n
1329	yy, bottom := r.Y, r.Y+r.H
1330	for i := range rows {
1331		hh := height
1332		if i == n-1 {
1333			hh = avail - height*(n-1)
1334		}
1335		yy = min(yy, bottom)
1336		hh = min(hh, bottom-yy)
1337		rows[i] = Rect{X: r.X, Y: yy, W: r.W, H: hh}
1338		yy += hh + gap
1339	}
1340	return rows
1341}
1342
1343// rowRects splits column rect r into the rows of c: by c.Shares when they
1344// are valid, else equally (stackRects).
1345func rowRects(r Rect, c Column, gap int) []Rect {
1346	n := len(c.Windows)
1347	if !validShares(c) {
1348		return stackRects(r, n, gap)
1349	}
1350	rows := make([]Rect, n)
1351	avail := max(r.H-(n-1)*gap, 0)
1352	yy, bottom, used := r.Y, r.Y+r.H, 0
1353	for i, share := range c.Shares {
1354		hh := avail * share / 100
1355		if i == n-1 {
1356			hh = avail - used
1357		}
1358		used += hh
1359		yy = min(yy, bottom)
1360		hh = min(hh, bottom-yy)
1361		rows[i] = Rect{X: r.X, Y: yy, W: r.W, H: hh}
1362		yy += hh + gap
1363	}
1364	return rows
1365}
1366
1367// validShares reports whether c.Shares has one share per window, each at
1368// least 1, summing to 100.
1369func validShares(c Column) bool {
1370	if len(c.Shares) != len(c.Windows) || len(c.Shares) < 2 {
1371		return false
1372	}
1373	sum := 0
1374	for _, v := range c.Shares {
1375		if v < 1 {
1376			return false
1377		}
1378		sum += v
1379	}
1380	return sum == 100
1381}
1382
1383// split cuts r in two halves with a gap: top/bottom when vertical, else left/right.
1384func split(r Rect, gap int, vertical bool) (Rect, Rect) {
1385	if vertical {
1386		h := max((r.H-gap)/2, 0)
1387		return Rect{X: r.X, Y: r.Y, W: r.W, H: h}, Rect{X: r.X, Y: r.Y + h + gap, W: r.W, H: max(r.H-gap-h, 0)}
1388	}
1389	w := max((r.W-gap)/2, 0)
1390	return Rect{X: r.X, Y: r.Y, W: w, H: r.H}, Rect{X: r.X + w + gap, Y: r.Y, W: max(r.W-gap-w, 0), H: r.H}
1391}
1392
1393func (w *Workspace) Layout() []Placement {
1394	var result []Placement
1395	gap := w.gap()
1396	cols := w.columnRects()
1397	focusedID, _ := w.Focused()
1398	cover := w.cover()
1399	for i, c := range w.Columns {
1400		col := cols[i]
1401		n := len(c.Windows)
1402		if n == 0 {
1403			continue
1404		}
1405		fullColumn := w.fullscreenColumn(i)
1406		if fullColumn {
1407			col.W = w.Output.W
1408		}
1409		rows := rowRects(col, c, gap)
1410		for j, id := range c.Windows {
1411			r := rows[j]
1412			full := w.fullscreen == id && id != 0
1413			// Fixed overflow cannot scroll to other columns while one fills
1414			// the view. Keep them in place, but out of the scene.
1415			maximized := w.Overflow == OverflowFixed && w.Columns[w.Focus].FullWidth && i != w.Focus
1416			hidden := (cover != 0 && id != cover) || ((fullColumn || w.Overflow == OverflowFixed && w.fullscreen != 0) && !full) || maximized
1417			if full {
1418				// Scroll mode aligns the view on the column; fixed never scrolls.
1419				r = w.Output
1420				if w.Overflow != OverflowFixed {
1421					r.X = col.X
1422				}
1423			}
1424			if hidden {
1425				r = Rect{}
1426			}
1427			result = append(result, Placement{ID: id, Rect: r, Fullscreen: full, Focused: id == focusedID, Hidden: hidden})
1428		}
1429	}
1430	setVisibleNeighbors(result, gap, w.Output)
1431	// The column/stash group sits between demoted covering floats and
1432	// floats above it. Keep the order within each group stable.
1433	tiles := result
1434	result = nil
1435	const (
1436		belowTiles = iota
1437		coveringFloats
1438		dialogs
1439	)
1440	appendFloats := func(group int) {
1441		for _, f := range w.Floats {
1442			below := f.below && w.coversFloat(f)
1443			// Dialogs stay over covering floats even when the latter is
1444			// selected. Fullscreen floats remain in the upper group.
1445			order := dialogs
1446			if below {
1447				order = belowTiles
1448			} else if w.coversFloat(f) || w.fullscreen == f.ID {
1449				order = coveringFloats
1450			}
1451			if order != group {
1452				continue
1453			}
1454			// A covering fullscreen window hides every float, its own dialogs
1455			// too: they show again when it leaves fullscreen.
1456			p := Placement{ID: f.ID, Rect: w.floatRect(f), Floating: true, Below: below, Focused: f.ID == focusedID, Inset: ports.SideAll, Hidden: cover != 0 && f.ID != cover}
1457			if p.Hidden {
1458				p.Rect = Rect{}
1459				result = append(result, p)
1460				continue
1461			}
1462			if w.fullscreen == f.ID {
1463				p.Rect, p.Fullscreen, p.Inset = w.Output, true, 0
1464			}
1465			result = append(result, p)
1466		}
1467	}
1468	appendFloats(belowTiles)
1469	result = append(result, tiles...)
1470	result = append(result, w.stashLayout(focusedID, cover)...)
1471	appendFloats(coveringFloats)
1472	appendFloats(dialogs)
1473	return result
1474}
1475
1476// setVisibleNeighbors reserves client space only for shared lines that
1477// are actually on this output. A scrolled-off or hidden tile cannot give a
1478// lone visible tile a border (or shrink its client).
1479func setVisibleNeighbors(tiles []Placement, gap int, output Rect) {
1480	visible := func(p Placement) bool {
1481		return !p.Hidden && !p.Fullscreen && p.Rect.Overlaps(output)
1482	}
1483	overlap := func(a0, a1, b0, b1 int) bool { return a0 < b1 && b0 < a1 }
1484	for i := range tiles {
1485		a := &tiles[i]
1486		a.Neighbors, a.Inset = 0, 0
1487		if !visible(*a) {
1488			continue
1489		}
1490		for j := range tiles {
1491			if i == j || !visible(tiles[j]) {
1492				continue
1493			}
1494			b, r := tiles[j].Rect, a.Rect
1495			// Only count a shared edge strictly within this output.
1496			switch {
1497			case r.X+r.W+gap == b.X && overlap(r.Y, r.Y+r.H, b.Y, b.Y+b.H) && r.X+r.W > output.X && r.X+r.W < output.X+output.W:
1498				a.Neighbors |= ports.SideRight
1499			case b.X+b.W+gap == r.X && overlap(r.Y, r.Y+r.H, b.Y, b.Y+b.H) && r.X > output.X && r.X < output.X+output.W:
1500				a.Neighbors |= ports.SideLeft
1501			case r.Y+r.H+gap == b.Y && overlap(r.X, r.X+r.W, b.X, b.X+b.W) && r.Y+r.H > output.Y && r.Y+r.H < output.Y+output.H:
1502				a.Neighbors |= ports.SideBottom
1503			case b.Y+b.H+gap == r.Y && overlap(r.X, r.X+r.W, b.X, b.X+b.W) && r.Y > output.Y && r.Y < output.Y+output.H:
1504				a.Neighbors |= ports.SideTop
1505			}
1506		}
1507		a.Inset = a.Neighbors
1508		if gap == 0 {
1509			a.Inset &= ports.SideRight | ports.SideBottom
1510		}
1511	}
1512}
1513
1514// floatRect places a floating window, its border around its client size,
1515// clamped to the usable area: a native one centred, a free one at its
1516// centre, kept inside the area.
1517func (w *Workspace) floatRect(f Float) Rect {
1518	u := w.Usable
1519	fw, fh := f.W, f.H
1520	if fw <= 0 || fh <= 0 {
1521		fw, fh = u.W/2, u.H/2
1522	}
1523	fw, fh = min(fw+2*w.border, u.W), min(fh+2*w.border, u.H)
1524	if !f.free {
1525		return Rect{X: u.X + (u.W-fw)/2, Y: u.Y + (u.H-fh)/2, W: fw, H: fh}
1526	}
1527	x := u.X + int(math.Round(f.cx*float64(u.W))) - fw/2
1528	y := u.Y + int(math.Round(f.cy*float64(u.H))) - fh/2
1529	x = min(max(x, u.X), u.X+u.W-fw)
1530	y = min(max(y, u.Y), u.Y+u.H-fh)
1531	return Rect{X: x, Y: y, W: fw, H: fh}
1532}
1533
1534// imposedFloat reports whether core, rather than the client, owns the
1535// size of a floating window: a stashed one or a free float.
1536func (w *Workspace) imposedFloat(id WindowID) bool {
1537	if i := w.floatIndex(id); i >= 0 {
1538		return w.Floats[i].free
1539	}
1540	return w.stashIndex(id) >= 0
1541}
1542
1543// ToggleWindowStash stashes the focused tile, or returns the focused
1544// stashed window to its column. A native float becomes a new column.
1545func (w *Workspace) ToggleWindowStash() {
1546	id, ok := w.Focused()
1547	// A fullscreen window (a Wine game in scanout) keeps its place: it
1548	// leaves fullscreen first, then may float.
1549	if !ok || id == w.fullscreen {
1550		return
1551	}
1552	switch {
1553	case w.floatIndex(id) >= 0 && w.Floats[w.floatIndex(id)].free:
1554		// A free float keeps its place and size in the stash.
1555		f := w.Floats[w.floatIndex(id)]
1556		w.RemoveWindow(id)
1557		w.addStash(f)
1558	case w.floatIndex(id) >= 0:
1559		w.RemoveWindow(id)
1560		w.restore(id, nil)
1561	case w.stashIndex(id) >= 0:
1562		w.unstash(w.stashIndex(id))
1563	default:
1564		w.stashWindow(id)
1565	}
1566}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 25

Tags 10

↑↓ moveenter openesc close