1package core
2
3import (
4 "context"
5 "math"
6 "slices"
7
8 "github.com/bnema/neferwl/internal/ports"
9)
10
11// overviewState owns the provisional selection and the snapshot of each row
12// it showed. row is the workspace whose stack has a provisional front; nil
13// uses its real front.
14type overviewState struct {
15 open bool
16 from *Workspace
17 back *Workspace
18 rows map[*Workspace]rowSnapshot
19 row *Workspace
20 front stackItem
21 frontAt int
22 cardOf *Workspace
23 card WindowID
24 selected WindowID
25 selectedAt int
26 scrollX, scrollY float64
27 // scrolled is set once a two-finger scroll stepped up or down: it
28 // waits for the fingers to lift before the next step. sideways is set
29 // once it stepped to a column: it stays on columns and steps once per
30 // overviewScrollRepeat.
31 scrolled, sideways bool
32}
33
34// rowSnapshot is a row as the overview first showed it: browsing moves its
35// focus and scroll, closing puts them back.
36type rowSnapshot struct {
37 focus WindowID
38 floats []Float
39 fullWidth WindowID
40 // FullWidth history is restored when active binds changed it.
41 maximized []WindowID
42}
43
44// The overview scales workspace rows without resizing clients. Covering
45// floats and the columns form a linear stack of cards; small floats stay
46// hidden. Up/down traverse the stack before changing workspace. A pinned
47// fullscreen window is the front card of its row, the windows it hides
48// behind it; choosing one of them leaves fullscreen.
49
50// A workspace's stash shows as a pile of cards pinned to the left edge of
51// its row: the selected stashed window in front at overviewCardZoom, up to
52// overviewCards-1 others behind it, each overviewCardStep further up and
53// right, dimmed. The row takes the width the pile leaves.
54const (
55 overviewCardZoom = 0.2
56 overviewCards = 4
57 overviewCardStep = 8
58 overviewPeekStep = 0.07
59)
60
61// overviewMaxZoom caps the current row so its neighbors show above and
62// below; overviewMinZoom floors the shrink of wide workspaces, which then
63// scroll to keep the selected column in view.
64const overviewMaxZoom, overviewMinZoom = 0.6, 0.25
65
66// ToggleOverview opens the overview, or closes it on the selection. Over
67// a covering fullscreen window (a game) too: its row shows it, composed
68// rather than scanned out while the overview is open.
69func (m *Monitor) ToggleOverview() {
70 if m.ov.open {
71 m.closeOverview()
72 return
73 }
74 w := m.Current()
75 // A slide in progress lands at once: the overview lays out the
76 // settled state.
77 m.stopSwitch()
78 m.each(func(w *Workspace) { w.stopSlide() })
79 m.ov.open, m.ov.from, m.ov.back = true, w, m.back
80 m.ov.scrollX, m.ov.scrollY, m.ov.scrolled, m.ov.sideways = 0, 0, false, false
81 m.overviewOpens++
82 m.selectRow()
83}
84
85// remember snapshots w the first time the overview shows it.
86func (m *Monitor) remember(w *Workspace) {
87 if _, ok := m.ov.rows[w]; ok {
88 return
89 }
90 if m.ov.rows == nil {
91 m.ov.rows = make(map[*Workspace]rowSnapshot)
92 }
93 s := rowSnapshot{
94 floats: append([]Float(nil), w.Floats...),
95 maximized: slices.Clone(w.maximized),
96 }
97 s.focus, _ = w.Focused()
98 if len(w.Columns) > 0 && w.Columns[w.Focus].FullWidth {
99 c := w.Columns[w.Focus]
100 s.fullWidth = c.Windows[c.Focus]
101 }
102 m.ov.rows[w] = s
103}
104
105// selectRow resets provisional stack and stash selection for a new row.
106func (m *Monitor) selectRow() {
107 m.remember(m.Current())
108 m.ov.card, m.ov.cardOf = 0, nil
109 m.ov.front, m.ov.row, m.ov.frontAt = stackItem{}, nil, 0
110 m.ov.selected, m.ov.selectedAt = 0, -1
111 w := m.Current()
112 // A fullscreen window has the focus: its card is in front.
113 w.focusCover()
114 if w.floatFocus && len(w.Columns) > 0 && (len(w.stack()) < 2 || m.stackFront(w).kind == stackColumns) {
115 w.floatFocus = false
116 }
117 if len(w.Stash) > 0 && (len(w.stack()) == 0 || w.stashFocused() && !w.floatFocus) {
118 m.selectCard(w, w.stashAt)
119 }
120}
121
122// selectCard selects stash entry i of w.
123func (m *Monitor) selectCard(w *Workspace, i int) {
124 m.ov.card, m.ov.cardOf = w.Stash[i].ID, w
125}
126
127// card is the selected stash card, 0 when there is none: the overview is
128// closed, the selection is on a column, or its window left the stash on
129// screen (unmapped, unstashed, or another workspace shown by a bind).
130func (m *Monitor) card() WindowID {
131 w := m.ov.cardOf
132 if !m.ov.open || w == nil || w != m.Current() || w.stashIndex(m.ov.card) < 0 {
133 return 0
134 }
135 return m.ov.card
136}
137
138// cardAt is the pile entry of w that is selected, -1 when none.
139func (m *Monitor) cardAt(w *Workspace) int {
140 if id := m.card(); id != 0 && w == m.ov.cardOf {
141 return w.stashIndex(id)
142 }
143 return -1
144}
145
146// overviewTarget resolves the active preview to one window for validation
147// and window actions. Presentation state never falls back to a covering float.
148func (m *Monitor) overviewTarget() WindowID {
149 if id := m.card(); id != 0 {
150 return id
151 }
152 w := m.Current()
153 item := m.stackFront(w)
154 switch item.kind {
155 case stackFloat:
156 return item.id
157 case stackColumn:
158 i := w.columnOf(item.id)
159 if i >= 0 {
160 c := w.Columns[i]
161 return c.Windows[c.Focus]
162 }
163 case stackColumns:
164 i := w.Focus
165 if len(w.Columns) == 0 {
166 return 0
167 }
168 if w.overviewMaximized() {
169 i = w.columnOf(m.ov.selected)
170 if i < 0 || i == w.Focus {
171 i = m.hiddenColumn(w)
172 }
173 }
174 if i >= 0 {
175 c := w.Columns[i]
176 return c.Windows[c.Focus]
177 }
178 }
179 return 0
180}
181
182// overviewBlocks makes window mutations explicit: they are disabled while
183// selection is provisional. Workspace and monitor navigation and moves to
184// another workspace remain active.
185func overviewBlocks(a Action) bool {
186 if _, _, ok := ResizeArg(a); ok {
187 return true
188 }
189 switch a {
190 case ActionMoveColumnLeft, ActionMoveColumnRight, ActionCycleColumnWidth,
191 ActionMaximizeColumn, ActionToggleFullscreen, ActionToggleWindowStash,
192 ActionToggleStashVisible, ActionConsumeOrExpelLeft,
193 ActionConsumeOrExpelRight, ActionMoveWorkspaceLeft, ActionMoveWorkspaceRight,
194 ActionMoveWorkspaceToMonitorUp, ActionMoveWorkspaceToMonitorDown,
195 ActionMoveWindowUp, ActionMoveWindowDown, ActionMoveWorkspaceUp,
196 ActionMoveWorkspaceDown, ActionToggleFloating:
197 return true
198 }
199 return false
200}
201
202// overviewMoveTo moves the selected preview at once, like close-window: the
203// overview stays open and closing it does not undo the move. Nothing moves
204// without a selected preview, from a stash card, or when the bind has
205// nowhere to go.
206func (m *Monitor) overviewMoveTo(a Action) {
207 cur, id := m.Current(), m.overviewTarget()
208 i, column, ok := m.moveDest(a)
209 if m.card() != 0 || id == 0 || !ok || m.Workspaces[i] == cur {
210 return
211 }
212 if cur.pinned() && id != cur.cover() {
213 // Focused reports the covering window while it covers.
214 cur.leaveFullscreen()
215 }
216 cur.FocusID(id)
217 m.MoveToWorkspace(i, column)
218 // The row that received the window keeps it focused on close.
219 if to, _ := m.find(id); to != nil && to != cur {
220 delete(m.ov.rows, to)
221 }
222 m.selectRow()
223}
224
225// closeOverview closes the overview on the selection: a card shows the
226// stash on it. A fullscreen request another stashed window made while
227// the stash was hidden is dropped: the picked card stays in front. A
228// window a pinned fullscreen window hides leaves that fullscreen.
229func (m *Monitor) closeOverview() {
230 w := m.Current()
231 front, target := m.stackFront(w), m.overviewTarget()
232 if w.pinned() && target != w.cover() {
233 w.leaveFullscreen()
234 }
235 if i := m.cardAt(w); i >= 0 {
236 if w.hiddenFullscreen != w.Stash[i].ID {
237 w.hiddenFullscreen = 0
238 }
239 w.stashAt = i
240 w.showStash()
241 } else if front != (stackItem{}) {
242 w.apply(front, target)
243 }
244 for r, s := range m.ov.rows {
245 if r != w && m.has(r) {
246 restoreRow(r, s)
247 }
248 }
249 m.ov = overviewState{}
250}
251
252// CancelOverview closes the overview and returns to the workspace and
253// window it opened on, if they are still there. Every row it showed gets
254// its focus back.
255func (m *Monitor) CancelOverview() {
256 if from := m.ov.from; from != nil && m.has(from) {
257 m.show(from)
258 }
259 for r, s := range m.ov.rows {
260 if m.has(r) {
261 restoreRow(r, s)
262 }
263 }
264 m.back = m.ov.back
265 if m.back != nil && !m.has(m.back) {
266 m.back = nil
267 }
268 m.ov = overviewState{}
269}
270
271// restoreRow puts w back as snapshot s found it.
272func restoreRow(w *Workspace, s rowSnapshot) {
273 if s.focus != 0 {
274 w.FocusID(s.focus)
275 } else {
276 w.floatFocus, w.stashFocus = false, false
277 }
278 // FocusID on a float raises it; restore the initial order and below
279 // flags for floats still present, leaving new floats in their order.
280 current := w.Floats
281 w.Floats = nil
282 for _, f := range s.floats {
283 for i, now := range current {
284 if now.ID == f.ID {
285 now.below = f.below
286 w.Floats = append(w.Floats, now)
287 current = append(current[:i], current[i+1:]...)
288 break
289 }
290 }
291 }
292 w.Floats = append(w.Floats, current...)
293 // Fixed overflow maximizes at most one column: only the one holding
294 // the anchor window, wherever active binds moved it. Scroll overflow
295 // keeps each column's own width.
296 if w.Overflow == OverflowFixed {
297 w.unmaximize()
298 for i := range w.Columns {
299 if s.fullWidth != 0 && slices.Contains(w.Columns[i].Windows, s.fullWidth) {
300 w.maximize(i)
301 break
302 }
303 }
304 }
305 // Windows that left the row since leave its maximize history too.
306 w.maximized = slices.DeleteFunc(slices.Clone(s.maximized), func(id WindowID) bool { return !w.has(id) })
307 w.scroll()
308}
309
310// overviewWorkspaces inserts named rows below their invocation workspace,
311// in configured order within each group. Unattached rows follow the last
312// occupied numbered row (or the first). Empty invocation anchors stay visible;
313// other empty rows are skipped unless current.
314func (m *Monitor) overviewWorkspaces() []*Workspace {
315 fallback := m.Workspaces[0]
316 for _, w := range m.Workspaces {
317 if !w.empty() {
318 fallback = w
319 }
320 }
321 groups := make(map[*Workspace][]*Workspace)
322 named := slices.Clone(m.hidden)
323 rank := func(w *Workspace) int {
324 for i, spec := range m.named {
325 if spec.Name == w.Name {
326 return i
327 }
328 }
329 return len(m.named)
330 }
331 slices.SortStableFunc(named, func(a, b *Workspace) int { return rank(a) - rank(b) })
332 for _, w := range named {
333 if w.empty() && w != m.Current() {
334 continue
335 }
336 anchor := w.overviewAfter
337 if indexOf(m.Workspaces, anchor) < 0 {
338 anchor = fallback
339 }
340 groups[anchor] = append(groups[anchor], w)
341 }
342 var rows []*Workspace
343 for _, w := range m.Workspaces {
344 if !w.empty() || w == m.Current() || len(groups[w]) > 0 {
345 rows = append(rows, w)
346 rows = append(rows, groups[w]...)
347 }
348 }
349 return rows
350}
351
352// showOverview changes rows without committing the provisional stack. Entering
353// a named group uses its invocation anchor as the toggle return target, not
354// rows merely browsed in the overview.
355func (m *Monitor) showOverview(w *Workspace) {
356 if w != m.Current() && m.isHidden(w) && !m.isHidden(m.Current()) {
357 m.back = m.Current()
358 if indexOf(m.Workspaces, w.overviewAfter) >= 0 {
359 m.back = w.overviewAfter
360 }
361 }
362 m.show(w)
363 m.selectRow()
364}
365
366// OverviewMove routes every vertical input through moveStack, then falls
367// through to workspace navigation at the stack boundary. Horizontal moves
368// stay inside a column group; left on a single card sends it behind.
369func (m *Monitor) OverviewMove(dx, dy int) {
370 w := m.Current()
371 if dy != 0 {
372 if m.moveStack(-dy) {
373 return
374 }
375 rows := m.overviewWorkspaces()
376 i := indexOf(rows, w) + dy
377 if i < 0 || i >= len(rows) {
378 return
379 }
380 m.showOverview(rows[i])
381 return
382 }
383 if at := m.cardAt(w); at >= 0 {
384 switch {
385 case at+dx >= 0 && at+dx < len(w.Stash):
386 m.selectCard(w, at+dx)
387 case dx > 0 && len(w.stack()) > 0:
388 m.ov.card, m.ov.cardOf = 0, nil
389 item := m.stackFront(w)
390 if item.kind == stackColumns {
391 if w.overviewMaximized() {
392 i := m.hiddenColumn(w)
393 m.ov.selected, m.ov.selectedAt = w.Columns[i].Windows[w.Columns[i].Focus], i
394 } else {
395 w.selectOverviewColumn(0)
396 m.ov.selected = w.Columns[0].Windows[w.Columns[0].Focus]
397 }
398 }
399 }
400 return
401 }
402 front := m.stackFront(w)
403 if front.kind != stackColumns {
404 if dx < 0 && m.moveStack(1) {
405 return
406 }
407 if dx < 0 && len(w.Stash) > 0 {
408 m.selectCard(w, w.stashAt)
409 }
410 return
411 }
412 if len(w.Columns) == 0 {
413 return
414 }
415 i := w.columnOf(m.ov.selected)
416 if i < 0 || w.overviewMaximized() && i == w.Focus {
417 if w.overviewMaximized() {
418 i = m.hiddenColumn(w)
419 } else {
420 i = w.Focus
421 }
422 }
423 first := 0
424 if w.overviewMaximized() && w.Focus == 0 {
425 first = 1
426 }
427 if dx < 0 && i == first && len(w.Stash) > 0 {
428 m.selectCard(w, w.stashAt)
429 return
430 }
431 next := min(max(i+dx, 0), len(w.Columns)-1)
432 if w.overviewMaximized() && next == w.Focus {
433 next = min(max(next+dx, 0), len(w.Columns)-1)
434 if next == w.Focus {
435 return
436 }
437 }
438 m.ov.selected = w.Columns[next].Windows[w.Columns[next].Focus]
439 m.ov.selectedAt = next
440 if !w.overviewMaximized() {
441 // Ordinary group navigation retains its existing scroll-to-selection.
442 w.selectOverviewColumn(next)
443 }
444}
445
446// OverviewPick closes the overview on the clicked window.
447func (m *Monitor) OverviewPick(id WindowID) {
448 w, _ := m.find(id)
449 if w == nil {
450 return
451 }
452 if w.floatIndex(id) >= 0 && w.stashIndex(id) < 0 {
453 i := w.floatIndex(id)
454 if !w.coversFloat(w.Floats[i]) && !(id == w.cover() && w.pinned()) {
455 return
456 }
457 }
458 m.showOverview(w)
459 m.ov.card, m.ov.cardOf = 0, nil
460 if i := w.stashIndex(id); i >= 0 {
461 m.selectCard(w, i)
462 } else if item, ok := w.itemOf(id); ok {
463 m.setFront(w, item)
464 if i := w.columnOf(id); i >= 0 {
465 if item.kind == stackColumns && !w.overviewMaximized() {
466 w.selectOverviewColumn(i)
467 }
468 m.ov.selected = id
469 for j, v := range w.Columns[i].Windows {
470 if v == id {
471 w.Columns[i].Focus = j
472 break
473 }
474 }
475 }
476 }
477 m.closeOverview()
478}
479
480// overviewRows centers the selected row with its neighbors above and below.
481// The numbered/named boundary gets extra spacing and an inactive horizontal
482// rule. Row widths, stash piles and preview scales remain unchanged.
483func (m *Monitor) overviewRows() (map[*Workspace]int, []ports.Separator) {
484 cur := m.Current()
485 u := cur.overviewArea()
486 gap := max(1, u.H*3/100)
487 hc := cur.rowHeight()
488 y := u.Y + (u.H-hc)/2
489 rows := map[*Workspace]int{cur: y}
490 var dividers []ports.Separator
491 list := m.overviewWorkspaces()
492 i := indexOf(list, cur)
493 for _, next := range []int{i - 1, i + 1} {
494 if next < 0 || next >= len(list) {
495 continue
496 }
497 w := list[next]
498 spacing := gap
499 boundary := m.isHidden(w) != m.isHidden(cur)
500 if boundary {
501 spacing *= 2
502 }
503 top := y + hc
504 if next < i {
505 rows[w] = y - spacing - w.rowHeight()
506 top = y - spacing
507 } else {
508 rows[w] = y + hc + spacing
509 }
510 if boundary {
511 margin := u.W * 4 / 100
512 dividers = append(dividers, ports.Separator{Rect: Rect{X: u.X + margin, Y: top + spacing/2, W: u.W - 2*margin, H: 1}})
513 }
514 }
515 return rows, dividers
516}
517
518// overviewLayout places the current workspace and its overview neighbors,
519// each with its full-width stash pile and covering-float stack. Other rows
520// stay hidden; numbered and named workspaces share the same preview geometry.
521func (m *Monitor) overviewLayout(result []Placement) []Placement {
522 cur := m.Current()
523 rows, _ := m.overviewRows()
524 for w := range m.all() {
525 ry, shown := rows[w]
526 if shown {
527 at := m.cardAt(w)
528 result = append(result, m.stackRow(w, ry, w != cur, at < 0)...)
529 result = append(result, w.pile(ry, w != cur, at)...)
530 }
531 for _, f := range w.Floats {
532 if _, drawn := w.itemOf(f.ID); !shown || !drawn {
533 result = append(result, Placement{ID: f.ID, Hidden: true})
534 }
535 }
536 if !shown {
537 // Columns and stash only: the floats were listed above.
538 result = w.appendHidden(result, false)
539 }
540 }
541 return result
542}
543
544// fan places front and neighboring cards, farthest visible first and front
545// last. A card may contain several tiles; omitted cards stay in the layout
546// hidden so their clients are not left showing on screen.
547func fan(cards [][]Placement, front, maxBehind, maxBefore int, circular bool,
548 behind, before func(int) (int, int), dim, lit bool) []Placement {
549 n := len(cards)
550 if n == 0 {
551 return nil
552 }
553 front = min(max(front, 0), n-1)
554 out := make([]Placement, 0, n)
555 add := func(i, distance int, offset func(int) (int, int), peek bool) {
556 dx, dy := offset(distance)
557 for _, p := range cards[i] {
558 if !p.Hidden {
559 p.Rect.X += dx
560 p.Rect.Y += dy
561 p.Peek = p.Peek || peek || dim
562 p.Focused = p.Focused && !peek && !dim && lit
563 }
564 out = append(out, p)
565 }
566 }
567 shown := make([]bool, n)
568 // Behind and before are in nearest-first order; paint farthest first.
569 for d := min(maxBehind, n-1); d >= 1; d-- {
570 i := front + d
571 if circular {
572 i = (front + d) % n
573 }
574 if i >= n {
575 continue
576 }
577 add(i, d, behind, true)
578 shown[i] = true
579 }
580 for d := min(maxBefore, n-1); d >= 1; d-- {
581 i := front - d
582 if circular {
583 i = (front - d + n) % n
584 }
585 if i < 0 {
586 continue
587 }
588 if shown[i] {
589 continue
590 }
591 add(i, d, before, true)
592 shown[i] = true
593 }
594 add(front, 0, behind, false)
595 shown[front] = true
596 for i, card := range cards {
597 if shown[i] {
598 continue
599 }
600 for _, p := range card {
601 p.Hidden = true
602 p.Preview = 0
603 p.Focused = false
604 out = append(out, p)
605 }
606 }
607 return out
608}
609
610// pileWidth is the room the stash pile of w takes on the left of its row,
611// its margin included; 0 without a stash.
612func (w *Workspace) pileWidth() int {
613 if len(w.Stash) == 0 {
614 return 0
615 }
616 card := int(math.Round(float64(w.stashRect().W) * overviewCardZoom))
617 // Never more than a third of the width: the columns keep the rest.
618 a := w.overviewArea()
619 return min(card+(min(len(w.Stash), overviewCards)-1)*w.cardStep()+a.W*2/100, a.W/3)
620}
621
622// cardStep is the offset between stacked cards, smaller on tiny outputs.
623func (w *Workspace) cardStep() int {
624 a := w.overviewArea()
625 return min(overviewCardStep, a.W/100, a.H/50)
626}
627
628// pile places the stash of w as cards on the left of the row whose top is
629// at y: entry front (the stash selection when -1) in front, the next ones
630// behind it, dimmed, the rest hidden. Only a front card of the current row
631// (dim false) with front set is selected.
632func (w *Workspace) pile(y int, dim bool, front int) []Placement {
633 n := len(w.Stash)
634 if n == 0 {
635 return nil
636 }
637 selected := front >= 0 && !dim
638 if front < 0 {
639 front = w.stashAt
640 }
641 front = min(max(front, 0), n-1)
642 r := w.stashRect()
643 u := w.overviewArea()
644 cw := int(math.Round(float64(r.W) * overviewCardZoom))
645 ch := int(math.Round(float64(r.H) * overviewCardZoom))
646 rowH := int(math.Round(float64(w.Usable.H) * w.overviewZoom()))
647 // Stack rows reserve two peek steps above the front card. Align the
648 // stash with that front band, not with the top of its envelope.
649 x0, y0 := u.X+u.W/100, w.frontRowY(y)+(rowH-ch)/2
650 cards := make([][]Placement, n)
651 for i := range w.Stash {
652 cards[i] = []Placement{{ID: w.Stash[i].ID, Rect: Rect{X: x0, Y: y0, W: cw, H: ch}, Preview: overviewCardZoom, Focused: selected}}
653 }
654 step := w.cardStep()
655 return fan(cards, front, min(n, overviewCards)-1, 0, true,
656 func(d int) (int, int) { return d * step, -d * step },
657 func(int) (int, int) { return 0, 0 }, dim, selected)
658}
659
660// overviewZoom is the scale that fits every column of w in the usable
661// width, within the overview bounds.
662func (w *Workspace) overviewZoom() float64 {
663 // Only the span is read: the tiles go in the zoom scratch, dead on return.
664 var span int
665 w.zoomBuf, span, _ = w.previewTilesInto(w.zoomBuf[:0])
666 if span <= 0 {
667 return overviewMaxZoom
668 }
669 room := float64(w.overviewArea().W-w.pileWidth()) * 0.96
670 z := min(max(room/float64(span), overviewMinZoom), overviewMaxZoom)
671 if len(w.stack()) > 1 {
672 // Reserve two clear peeks on either side of the front row.
673 z = min(z, overviewMaxZoom/(1+4*overviewPeekStep))
674 }
675 return z
676}
677
678// peekStep is the shared step for a stack row and its front-card inset.
679func (w *Workspace) peekStep() int {
680 return max(1, int(math.Round(float64(w.Usable.H)*w.overviewZoom()*overviewPeekStep)))
681}
682
683// frontRowY skips the space reserved for two cards peeking above the front.
684// A single-card row starts at y, as before.
685func (w *Workspace) frontRowY(y int) int {
686 if len(w.stack()) > 1 {
687 return y + 2*w.peekStep()
688 }
689 return y
690}
691
692// rowHeight includes the largest possible stack envelope (two peeks above
693// and two below). Neighbor rows use the same measure as the current row.
694func (w *Workspace) rowHeight() int {
695 h := int(math.Round(float64(w.Usable.H) * w.overviewZoom()))
696 if len(w.stack()) > 1 {
697 h += 4 * w.peekStep()
698 }
699 return h
700}
701
702// previewTiles lays tiles out unscrolled, relative to the usable area's
703// corner. Fixed overflow uses its on-screen geometry; scroll overflow uses
704// an unscrolled row. span is the row's width; sel is the focused column.
705func (w *Workspace) previewTiles() (tiles []Placement, span int, sel Rect) {
706 return w.previewTilesInto(make([]Placement, 0, w.columnWindows()))
707}
708
709// previewTilesInto is previewTiles appending to tiles (dst[:0] to reuse its
710// storage: the result then aliases it). The column rects use w.colBuf, dead
711// when it returns.
712func (w *Workspace) previewTilesInto(tiles []Placement) ([]Placement, int, Rect) {
713 var span int
714 var sel Rect
715 if w.pinned() {
716 c := w.cover()
717 // The row shows the columns a pinned fullscreen window hides,
718 // laid out as without it; its own tile stays marked fullscreen.
719 v := *w
720 v.fullscreen = 0
721 tiles, span, sel = v.previewTilesInto(tiles)
722 w.colBuf, w.rowBuf = v.colBuf, v.rowBuf
723 for i := range tiles {
724 tiles[i].Fullscreen = tiles[i].ID == c
725 }
726 return tiles, span, sel
727 }
728 g := w.gap()
729 h := max(w.Usable.H-2*g, 0)
730 var rects []Rect
731 if w.Overflow == OverflowFixed {
732 w.colBuf = w.columnRectsInto(w.colBuf, false)
733 rects = w.colBuf
734 span = w.Usable.W // Fixed overflow never scrolls, even with a stash pile.
735 }
736 maximized := w.Overflow == OverflowFixed && len(w.Columns) > 0 && w.Columns[w.Focus].FullWidth
737 for i, c := range w.Columns {
738 r := Rect{X: w.columnX(i) - w.Usable.X, Y: g, W: w.columnWidth(i), H: h}
739 if w.Overflow == OverflowFixed {
740 r = rects[i]
741 r.X, r.Y = r.X-w.Usable.X, r.Y-w.Usable.Y
742 } else if w.fullscreenColumn(i) {
743 // A fullscreen column shows at the width it had in the row.
744 r.W = max(w.Usable.W-2*g, 0)
745 }
746 if maximized && i != w.Focus {
747 // FullWidth hides other columns on screen. Their old buffers
748 // cannot fit their current spiral rects; show only the maximized
749 // column until navigation restores the normal layout.
750 for _, id := range c.Windows {
751 tiles = append(tiles, Placement{ID: id, Hidden: true})
752 }
753 continue
754 }
755 w.rowBuf = rowRectsInto(w.rowBuf[:0], r, c, g)
756 for j, t := range w.rowBuf {
757 tiles = append(tiles, Placement{ID: c.Windows[j], Rect: t, Focused: i == w.Focus && j == c.Focus})
758 }
759 if w.Overflow != OverflowFixed {
760 span = max(span, r.X+r.W+g)
761 }
762 if i == w.Focus {
763 sel = r
764 }
765 }
766 return tiles, span, sel
767}
768
769// previewRow scales the tiles of w into a row whose top is at y, centred
770// in the usable width, or scrolled so the focused column shows when the
771// row is wider, right of the stash pile. Dimmed rows (neighbor
772// workspaces) have no focus; neither has a row whose pile has it (lit
773// false).
774func (w *Workspace) previewRow(y int, dim, lit bool) []Placement {
775 tiles, span, sel := w.previewTiles()
776 return w.previewRowTiles(y, dim, lit, tiles, span, sel)
777}
778
779func (w *Workspace) previewRowTiles(y int, dim, lit bool, tiles []Placement, span int, sel Rect) []Placement {
780 u := w.overviewArea()
781 z := w.overviewZoom()
782 scale := func(v int) int { return int(math.Round(float64(v) * z)) }
783 width := scale(span)
784 // Rows stay centred on the output like rows without a stash; the pile
785 // only pushes a row right when they would overlap.
786 x := max(u.X+(u.W-width)/2, u.X+w.pileWidth())
787 left := w.pileWidth()
788 u.X, u.W = u.X+left, u.W-left
789 if width > u.W {
790 x = u.X + u.W/2 - scale(sel.X+sel.W/2)
791 x = min(max(x, u.X+u.W-width), u.X)
792 }
793 for i := range tiles {
794 if tiles[i].Hidden {
795 continue
796 }
797 r := tiles[i].Rect
798 x0, y0 := scale(r.X), scale(r.Y)
799 tiles[i].Rect = Rect{X: x + x0, Y: y + y0, W: scale(r.X+r.W) - x0, H: scale(r.Y+r.H) - y0}
800 tiles[i].Preview, tiles[i].Peek = z, dim
801 tiles[i].Focused = tiles[i].Focused && !dim && lit
802 }
803 return tiles
804}
805
806// overviewOutline frames the selected preview with active lines of width
807// b, just outside it.
808func overviewOutline(layout []Placement, b int) []ports.Separator {
809 for _, p := range layout {
810 if p.Preview > 0 && p.Focused && !p.Hidden {
811 r := p.Rect
812 return []ports.Separator{
813 {Rect: Rect{X: r.X - b, Y: r.Y - b, W: r.W + 2*b, H: b}, Active: true},
814 {Rect: Rect{X: r.X - b, Y: r.Y + r.H, W: r.W + 2*b, H: b}, Active: true},
815 {Rect: Rect{X: r.X - b, Y: r.Y, W: b, H: r.H}, Active: true},
816 {Rect: Rect{X: r.X + r.W, Y: r.Y, W: b, H: r.H}, Active: true},
817 }
818 }
819 }
820 return nil
821}
822
823// overviewClickFade is the fade from which a card takes no click.
824const overviewClickFade = 0.5
825
826// overviewIn is the preview under the output-local point in layout, if
827// any. Front cards take priority over peeks, including peeks appended after
828// a front tile in another row's layout. The click path passes the shown
829// layout, so a card is hit where it is drawn while it moves. A card that is
830// nearly invisible (still fading in) is not clickable.
831func overviewIn(layout []Placement, x, y float64) WindowID {
832 for i := len(layout) - 1; i >= 0; i-- {
833 p := layout[i]
834 r := p.Rect
835 if p.Preview > 0 && !p.Hidden && !p.Peek && p.Fade < overviewClickFade && x >= float64(r.X) && x < float64(r.X+r.W) && y >= float64(r.Y) && y < float64(r.Y+r.H) {
836 return p.ID
837 }
838 }
839 for i := len(layout) - 1; i >= 0; i-- {
840 p := layout[i]
841 r := p.Rect
842 if p.Preview > 0 && !p.Hidden && p.Fade < overviewClickFade && x >= float64(r.X) && x < float64(r.X+r.W) && y >= float64(r.Y) && y < float64(r.Y+r.H) {
843 return p.ID
844 }
845 }
846 return 0
847}
848
849// overviewClick closes the overview on the preview under the pointer, on
850// the output it shows on. picked is false when no preview is there.
851func (c *Core) overviewClick(ctx context.Context) (picked bool, err error) {
852 i := c.screenAt(c.cursorX, c.cursorY)
853 if i < 0 {
854 return false, nil
855 }
856 sc := c.screens[i]
857 if !sc.mon.ov.open {
858 return false, nil
859 }
860 // The cards are hit where they are drawn: a click during the open or a
861 // navigation animation picks what moves under the pointer.
862 id := overviewIn(sc.shownLayout(), c.cursorX-float64(sc.x), c.cursorY-float64(sc.y))
863 if id == 0 {
864 return false, nil
865 }
866 now := c.now()
867 shots := c.snapshot(now)
868 sc.mon.OverviewPick(id)
869 c.transition(shots, now)
870 c.focusScreen = i
871 c.keyboard.takeBack()
872 if err := c.workspaceVisible(ctx, true); err != nil {
873 return true, err
874 }
875 return true, c.publish(ctx)
876}
877
878// overviewAction runs a bind while the overview is open: focus binds move
879// the selection, close-window and move-to-workspace binds act on the
880// selected preview, other mutations are dropped. handled is false for binds
881// that run as usual.
882func (m *Monitor) overviewAction(a Action) (e Effect, handled bool) {
883 if !m.ov.open {
884 return Effect{}, false
885 }
886 if a == ActionCloseWindow {
887 return Effect{Close: m.overviewTarget()}, true
888 }
889 if movesToWorkspace(a) {
890 m.overviewMoveTo(a)
891 return Effect{}, true
892 }
893 return Effect{}, m.overviewFocus(a) || overviewBlocks(a)
894}
895
896// overviewFocus runs a focus bind as an overview move, so the focus binds
897// and the bare keys navigate the overview the same way: true when a was
898// one of them.
899func (m *Monitor) overviewFocus(a Action) bool {
900 switch a {
901 case ActionFocusColumnLeft:
902 m.OverviewMove(-1, 0)
903 case ActionFocusColumnRight:
904 m.OverviewMove(1, 0)
905 case ActionFocusWindowUp, ActionFocusWorkspaceUp:
906 m.OverviewMove(0, -1)
907 case ActionFocusWindowDown, ActionFocusWorkspaceDown:
908 m.OverviewMove(0, 1)
909 default:
910 return false
911 }
912 return true
913}
914
915const (
916 // overviewScrollStep is the scroll distance, in libinput's pointer
917 // units, that moves the selection one column or workspace.
918 overviewScrollStep = 60
919 // overviewScrollRepeat is the two-finger distance of each further
920 // column step in one scroll: longer than the first, so a short scroll
921 // reaches the next column exactly and a long one goes on.
922 overviewScrollRepeat = 150
923)
924
925// overviewScroll moves the overview selection with a scroll frame: a row
926// step when the vertical scroll reaches overviewScrollStep and leads, else
927// a column step. Two fingers step to the next column after
928// overviewScrollStep, then once per overviewScrollRepeat, and stay on
929// columns until they lift; up or down they step once per scroll, so a card
930// in a stack is easy to pick.
931// Continuous scrolling steps once per overviewScrollStep and a wheel once
932// per notch (high-resolution wheels add up their fractions of a notch).
933// Vertical steps select cards first, then rows; natural scroll
934// (touchpad.* or mouse.*) flips both axes as libinput reports. The end of
935// a scroll resets it (scrollStop).
936func (m *Monitor) overviewScroll(a ports.PointerAxis) (changed bool) {
937 if a.Vertical.Stop || a.Horizontal.Stop {
938 return false
939 }
940 finger := a.Source == ports.AxisFinger
941 if finger && m.ov.scrolled {
942 return false
943 }
944 // A sideways finger scroll stays on columns; other sources do not, and
945 // start from zero rather than from its leftover.
946 sideways := finger && m.ov.sideways
947 if !finger && m.ov.sideways {
948 m.ov.scrollX, m.ov.scrollY = 0, 0
949 }
950 stepX := float64(overviewScrollStep)
951 if sideways {
952 stepX = overviewScrollRepeat
953 }
954 add := func(acc *float64, ax ports.ScrollAxis) {
955 switch {
956 case !ax.Set:
957 case a.Source == ports.AxisWheel:
958 *acc += float64(ax.V120) / 120 * overviewScrollStep
959 default:
960 *acc += ax.Value
961 }
962 }
963 if !sideways {
964 add(&m.ov.scrollY, a.Vertical)
965 }
966 add(&m.ov.scrollX, a.Horizontal)
967 for {
968 switch {
969 case math.Abs(m.ov.scrollY) >= overviewScrollStep && math.Abs(m.ov.scrollY) >= math.Abs(m.ov.scrollX):
970 m.OverviewMove(0, sign(m.ov.scrollY))
971 m.ov.scrollY -= float64(sign(m.ov.scrollY)) * overviewScrollStep
972 m.ov.scrollX = 0
973 if finger {
974 m.ov.scrollX, m.ov.scrollY, m.ov.scrolled = 0, 0, true
975 return true
976 }
977 case math.Abs(m.ov.scrollX) >= stepX:
978 m.OverviewMove(sign(m.ov.scrollX), 0)
979 m.ov.scrollX -= float64(sign(m.ov.scrollX)) * stepX
980 m.ov.scrollY = 0
981 if finger {
982 m.ov.sideways, stepX = true, overviewScrollRepeat
983 }
984 default:
985 return changed
986 }
987 changed = true
988 }
989}
990
991// scrollStop ends a scroll on every monitor: the fingers lifted, maybe
992// after the pointer took the focus to another output, so the next scroll
993// starts from zero wherever it goes.
994func (c *Core) scrollStop(a ports.PointerAxis) {
995 if !a.Vertical.Stop && !a.Horizontal.Stop {
996 return
997 }
998 for _, sc := range c.screens {
999 sc.mon.ov.scrollX, sc.mon.ov.scrollY, sc.mon.ov.scrolled, sc.mon.ov.sideways = 0, 0, false, false
1000 }
1001}
1002
1003func sign(v float64) int {
1004 if v < 0 {
1005 return -1
1006 }
1007 return 1
1008}
1009
1010// overviewKeyboardTaken reports whether a layer surface (a launcher) or a
1011// grabbing menu has the keyboard: it gets the keys, not the overview.
1012func (c *Core) overviewKeyboardTaken() bool {
1013 id := c.keyboardFocus()
1014 _, _, layer := c.layerOf(id)
1015 return layer || c.popups[id] != nil
1016}
1017
1018// overviewKey runs an overview key: true when the key was one.
1019func (m *Monitor) overviewKey(key ports.KeyEvent) bool {
1020 if key.Mods != 0 {
1021 return false
1022 }
1023 switch keyName(key.Keysym) {
1024 case "h", "Left":
1025 m.OverviewMove(-1, 0)
1026 case "l", "Right":
1027 m.OverviewMove(1, 0)
1028 case "k", "Up":
1029 m.OverviewMove(0, -1)
1030 case "j", "Down":
1031 m.OverviewMove(0, 1)
1032 case "Return", "KP_Enter":
1033 m.ToggleOverview()
1034 case "Escape":
1035 m.CancelOverview()
1036 default:
1037 return false
1038 }
1039 return true
1040}