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.view.stop(); w.stashView.stop() })
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 shares navigation rules across keys, scrolls and swipes.
367// Vertical moves stay in a selected stash pile; left stays, and right
368// deselects it, even when the row has no main card.
369func (m *Monitor) OverviewMove(dx, dy int) {
370 if dy != 0 {
371 m.overviewVertical(dy)
372 } else if dx != 0 {
373 m.overviewHorizontal(dx)
374 }
375}
376
377func (m *Monitor) overviewVertical(dy int) {
378 w := m.Current()
379 if at := m.cardAt(w); at >= 0 {
380 m.selectCard(w, min(max(at+dy, 0), len(w.Stash)-1))
381 return
382 }
383 if m.moveStack(-dy) {
384 return
385 }
386 rows := m.overviewWorkspaces()
387 i := indexOf(rows, w) + dy
388 if i >= 0 && i < len(rows) {
389 m.showOverview(rows[i])
390 }
391}
392
393func (m *Monitor) overviewHorizontal(dx int) {
394 w := m.Current()
395 if m.cardAt(w) >= 0 {
396 if dx > 0 {
397 m.ov.card, m.ov.cardOf = 0, nil
398 m.ov.selected = 0
399 item := m.stackFront(w)
400 if item.kind == stackColumns {
401 if w.overviewMaximized() {
402 i := m.hiddenColumn(w)
403 m.ov.selected, m.ov.selectedAt = w.Columns[i].Windows[w.Columns[i].Focus], i
404 } else {
405 w.selectOverviewColumn(0)
406 m.ov.selected = w.Columns[0].Windows[w.Columns[0].Focus]
407 }
408 }
409 }
410 return
411 }
412 front := m.stackFront(w)
413 if front.kind != stackColumns {
414 if dx < 0 && m.moveStack(1) {
415 return
416 }
417 if dx < 0 && len(w.Stash) > 0 {
418 m.selectCard(w, w.stashAt)
419 }
420 return
421 }
422 if len(w.Columns) == 0 {
423 return
424 }
425 i := w.columnOf(m.ov.selected)
426 if i < 0 || w.overviewMaximized() && i == w.Focus {
427 if w.overviewMaximized() {
428 i = m.hiddenColumn(w)
429 } else {
430 i = w.Focus
431 }
432 }
433 first := 0
434 if w.overviewMaximized() && w.Focus == 0 {
435 first = 1
436 }
437 if dx < 0 && i == first && len(w.Stash) > 0 {
438 m.selectCard(w, w.stashAt)
439 return
440 }
441 next := min(max(i+dx, 0), len(w.Columns)-1)
442 if w.overviewMaximized() && next == w.Focus {
443 next = min(max(next+dx, 0), len(w.Columns)-1)
444 if next == w.Focus {
445 return
446 }
447 }
448 m.ov.selected = w.Columns[next].Windows[w.Columns[next].Focus]
449 m.ov.selectedAt = next
450 if !w.overviewMaximized() {
451 // Ordinary group navigation retains its existing scroll-to-selection.
452 w.selectOverviewColumn(next)
453 }
454}
455
456// OverviewPick closes the overview on the clicked window.
457func (m *Monitor) OverviewPick(id WindowID) {
458 w, _ := m.find(id)
459 if w == nil {
460 return
461 }
462 if w.floatIndex(id) >= 0 && w.stashIndex(id) < 0 {
463 i := w.floatIndex(id)
464 if !w.coversFloat(w.Floats[i]) && !(id == w.cover() && w.pinned()) {
465 return
466 }
467 }
468 m.showOverview(w)
469 m.ov.card, m.ov.cardOf = 0, nil
470 if i := w.stashIndex(id); i >= 0 {
471 m.selectCard(w, i)
472 } else if item, ok := w.itemOf(id); ok {
473 m.setFront(w, item)
474 if i := w.columnOf(id); i >= 0 {
475 if item.kind == stackColumns && !w.overviewMaximized() {
476 w.selectOverviewColumn(i)
477 }
478 m.ov.selected = id
479 for j, v := range w.Columns[i].Windows {
480 if v == id {
481 w.Columns[i].Focus = j
482 break
483 }
484 }
485 }
486 }
487 m.closeOverview()
488}
489
490// overviewRows centers the selected row with its neighbors above and below.
491// The numbered/named boundary gets extra spacing and an inactive horizontal
492// rule. Row widths, stash piles and preview scales remain unchanged.
493func (m *Monitor) overviewRows() (map[*Workspace]int, []ports.Separator) {
494 cur := m.Current()
495 u := cur.overviewArea()
496 gap := max(1, u.H*3/100)
497 hc := cur.rowHeight()
498 y := u.Y + (u.H-hc)/2
499 rows := map[*Workspace]int{cur: y}
500 var dividers []ports.Separator
501 list := m.overviewWorkspaces()
502 i := indexOf(list, cur)
503 for _, next := range []int{i - 1, i + 1} {
504 if next < 0 || next >= len(list) {
505 continue
506 }
507 w := list[next]
508 spacing := gap
509 boundary := m.isHidden(w) != m.isHidden(cur)
510 if boundary {
511 spacing *= 2
512 }
513 top := y + hc
514 if next < i {
515 rows[w] = y - spacing - w.rowHeight()
516 top = y - spacing
517 } else {
518 rows[w] = y + hc + spacing
519 }
520 if boundary {
521 margin := u.W * 4 / 100
522 dividers = append(dividers, ports.Separator{Rect: Rect{X: u.X + margin, Y: top + spacing/2, W: u.W - 2*margin, H: 1}})
523 }
524 }
525 return rows, dividers
526}
527
528// overviewLayout places the current workspace and its overview neighbors,
529// each with its full-width stash pile and covering-float stack. Other rows
530// stay hidden; numbered and named workspaces share the same preview geometry.
531func (m *Monitor) overviewLayout(result []Placement) []Placement {
532 cur := m.Current()
533 rows, _ := m.overviewRows()
534 for w := range m.all() {
535 ry, shown := rows[w]
536 if shown {
537 at := m.cardAt(w)
538 result = append(result, m.stackRow(w, ry, w != cur, at < 0)...)
539 result = append(result, w.pile(ry, w != cur, at)...)
540 }
541 for _, f := range w.Floats {
542 if _, drawn := w.itemOf(f.ID); !shown || !drawn {
543 result = append(result, Placement{ID: f.ID, Hidden: true})
544 }
545 }
546 if !shown {
547 // Columns and stash only: the floats were listed above.
548 result = w.appendHidden(result, false)
549 }
550 }
551 return result
552}
553
554// fan places front and neighboring cards, farthest visible first and front
555// last. A card may contain several tiles; omitted cards stay in the layout
556// hidden so their clients are not left showing on screen.
557func fan(cards [][]Placement, front, maxBehind, maxBefore int, circular bool,
558 behind, before func(int) (int, int), dim, lit bool) []Placement {
559 n := len(cards)
560 if n == 0 {
561 return nil
562 }
563 front = min(max(front, 0), n-1)
564 out := make([]Placement, 0, n)
565 add := func(i, distance int, offset func(int) (int, int), peek bool) {
566 dx, dy := offset(distance)
567 for _, p := range cards[i] {
568 if !p.Hidden {
569 p.Rect.X += dx
570 p.Rect.Y += dy
571 if p.Peek || peek || dim {
572 p.peeking(1)
573 }
574 p.Focused = p.Focused && !peek && !dim && lit
575 }
576 out = append(out, p)
577 }
578 }
579 shown := make([]bool, n)
580 // Behind and before are in nearest-first order; paint farthest first.
581 for d := min(maxBehind, n-1); d >= 1; d-- {
582 i := front + d
583 if circular {
584 i = (front + d) % n
585 }
586 if i >= n {
587 continue
588 }
589 add(i, d, behind, true)
590 shown[i] = true
591 }
592 for d := min(maxBefore, n-1); d >= 1; d-- {
593 i := front - d
594 if circular {
595 i = (front - d + n) % n
596 }
597 if i < 0 {
598 continue
599 }
600 if shown[i] {
601 continue
602 }
603 add(i, d, before, true)
604 shown[i] = true
605 }
606 add(front, 0, behind, false)
607 shown[front] = true
608 for i, card := range cards {
609 if shown[i] {
610 continue
611 }
612 for _, p := range card {
613 p.Hidden = true
614 p.Preview = 0
615 p.Focused = false
616 out = append(out, p)
617 }
618 }
619 return out
620}
621
622// pileWidth is the room the stash pile of w takes on the left of its row,
623// its margin included; 0 without a stash.
624func (w *Workspace) pileWidth() int {
625 if len(w.Stash) == 0 {
626 return 0
627 }
628 card := int(math.Round(float64(w.stashRect().W) * overviewCardZoom))
629 // Never more than a third of the width: the columns keep the rest.
630 a := w.overviewArea()
631 return min(card+(min(len(w.Stash), overviewCards)-1)*w.cardStep()+a.W*2/100, a.W/3)
632}
633
634// cardStep is the offset between stacked cards, smaller on tiny outputs.
635func (w *Workspace) cardStep() int {
636 a := w.overviewArea()
637 return min(overviewCardStep, a.W/100, a.H/50)
638}
639
640// pile places the stash of w as cards on the left of the row whose top is
641// at y: entry front (the stash selection when -1) in front, the next ones
642// behind it, dimmed, the rest hidden. Only a front card of the current row
643// (dim false) with front set is selected.
644func (w *Workspace) pile(y int, dim bool, front int) []Placement {
645 n := len(w.Stash)
646 if n == 0 {
647 return nil
648 }
649 selected := front >= 0 && !dim
650 if front < 0 {
651 front = w.stashAt
652 }
653 front = min(max(front, 0), n-1)
654 r := w.stashRect()
655 u := w.overviewArea()
656 cw := int(math.Round(float64(r.W) * overviewCardZoom))
657 ch := int(math.Round(float64(r.H) * overviewCardZoom))
658 rowH := int(math.Round(float64(w.Usable.H) * w.overviewZoom()))
659 // Stack rows reserve two peek steps above the front card. Align the
660 // stash with that front band, not with the top of its envelope.
661 x0, y0 := u.X+u.W/100, w.frontRowY(y)+(rowH-ch)/2
662 cards := make([][]Placement, n)
663 for i := range w.Stash {
664 cards[i] = []Placement{{ID: w.Stash[i].ID, Rect: Rect{X: x0, Y: y0, W: cw, H: ch}, Preview: overviewCardZoom, Focused: selected}}
665 }
666 step := w.cardStep()
667 return fan(cards, front, min(n, overviewCards)-1, 0, true,
668 func(d int) (int, int) { return d * step, -d * step },
669 func(int) (int, int) { return 0, 0 }, dim, selected)
670}
671
672// overviewZoom is the scale that fits every column of w in the usable
673// width, within the overview bounds.
674func (w *Workspace) overviewZoom() float64 {
675 // Only the span is read: the tiles go in the zoom scratch, dead on return.
676 var span int
677 w.zoomBuf, span, _ = w.previewTilesInto(w.zoomBuf[:0])
678 if span <= 0 {
679 return overviewMaxZoom
680 }
681 room := float64(w.overviewArea().W-w.pileWidth()) * 0.96
682 z := min(max(room/float64(span), overviewMinZoom), overviewMaxZoom)
683 if len(w.stack()) > 1 {
684 // Reserve two clear peeks on either side of the front row.
685 z = min(z, overviewMaxZoom/(1+4*overviewPeekStep))
686 }
687 return z
688}
689
690// peekStep is the shared step for a stack row and its front-card inset.
691func (w *Workspace) peekStep() int {
692 return max(1, int(math.Round(float64(w.Usable.H)*w.overviewZoom()*overviewPeekStep)))
693}
694
695// frontRowY skips the space reserved for two cards peeking above the front.
696// A single-card row starts at y, as before.
697func (w *Workspace) frontRowY(y int) int {
698 if len(w.stack()) > 1 {
699 return y + 2*w.peekStep()
700 }
701 return y
702}
703
704// rowHeight includes the largest possible stack envelope (two peeks above
705// and two below). Neighbor rows use the same measure as the current row.
706func (w *Workspace) rowHeight() int {
707 h := int(math.Round(float64(w.Usable.H) * w.overviewZoom()))
708 if len(w.stack()) > 1 {
709 h += 4 * w.peekStep()
710 }
711 return h
712}
713
714// previewTiles lays tiles out unscrolled, relative to the usable area's
715// corner. Fixed overflow uses its on-screen geometry; scroll overflow uses
716// an unscrolled row. span is the row's width; sel is the focused column.
717func (w *Workspace) previewTiles() (tiles []Placement, span int, sel Rect) {
718 return w.previewTilesInto(make([]Placement, 0, w.columnWindows()))
719}
720
721// previewTilesInto is previewTiles appending to tiles (dst[:0] to reuse its
722// storage: the result then aliases it). The column rects use w.colBuf, dead
723// when it returns.
724func (w *Workspace) previewTilesInto(tiles []Placement) ([]Placement, int, Rect) {
725 var span int
726 var sel Rect
727 if w.pinned() {
728 c := w.cover()
729 // The row shows the columns a pinned fullscreen window hides,
730 // laid out as without it; its own tile stays marked fullscreen.
731 v := *w
732 v.fullscreen = 0
733 tiles, span, sel = v.previewTilesInto(tiles)
734 w.colBuf, w.rowBuf = v.colBuf, v.rowBuf
735 for i := range tiles {
736 tiles[i].Fullscreen = tiles[i].ID == c
737 }
738 return tiles, span, sel
739 }
740 g := w.gap()
741 h := max(w.Usable.H-2*g, 0)
742 var rects []Rect
743 if w.Overflow == OverflowFixed {
744 w.colBuf = w.columnRectsInto(w.colBuf, false)
745 rects = w.colBuf
746 span = w.Usable.W // Fixed overflow never scrolls, even with a stash pile.
747 }
748 maximized := w.Overflow == OverflowFixed && len(w.Columns) > 0 && w.Columns[w.Focus].FullWidth
749 for i, c := range w.Columns {
750 r := Rect{X: w.columnX(i) - w.Usable.X, Y: g, W: w.columnWidth(i), H: h}
751 if w.Overflow == OverflowFixed {
752 r = rects[i]
753 r.X, r.Y = r.X-w.Usable.X, r.Y-w.Usable.Y
754 } else if w.fullscreenColumn(i) {
755 // A fullscreen column shows at the width it had in the row.
756 r.W = max(w.Usable.W-2*g, 0)
757 }
758 if maximized && i != w.Focus {
759 // FullWidth hides other columns on screen. Their old buffers
760 // cannot fit their current spiral rects; show only the maximized
761 // column until navigation restores the normal layout.
762 for _, id := range c.Windows {
763 tiles = append(tiles, Placement{ID: id, Hidden: true})
764 }
765 continue
766 }
767 w.rowBuf = rowRectsInto(w.rowBuf[:0], r, c, g)
768 for j, t := range w.rowBuf {
769 tiles = append(tiles, Placement{ID: c.Windows[j], Rect: t, Focused: i == w.Focus && j == c.Focus})
770 }
771 if w.Overflow != OverflowFixed {
772 span = max(span, r.X+r.W+g)
773 }
774 if i == w.Focus {
775 sel = r
776 }
777 }
778 return tiles, span, sel
779}
780
781// previewRow scales the tiles of w into a row whose top is at y, centred
782// in the usable width, or scrolled so the focused column shows when the
783// row is wider, right of the stash pile. Dimmed rows (neighbor
784// workspaces) have no focus; neither has a row whose pile has it (lit
785// false).
786func (w *Workspace) previewRow(y int, dim, lit bool) []Placement {
787 tiles, span, sel := w.previewTiles()
788 return w.previewRowTiles(y, dim, lit, tiles, span, sel)
789}
790
791func (w *Workspace) previewRowTiles(y int, dim, lit bool, tiles []Placement, span int, sel Rect) []Placement {
792 u := w.overviewArea()
793 z := w.overviewZoom()
794 scale := func(v int) int { return int(math.Round(float64(v) * z)) }
795 width := scale(span)
796 // Rows stay centred on the output like rows without a stash; the pile
797 // only pushes a row right when they would overlap.
798 x := max(u.X+(u.W-width)/2, u.X+w.pileWidth())
799 left := w.pileWidth()
800 u.X, u.W = u.X+left, u.W-left
801 if width > u.W {
802 x = u.X + u.W/2 - scale(sel.X+sel.W/2)
803 x = min(max(x, u.X+u.W-width), u.X)
804 }
805 for i := range tiles {
806 if tiles[i].Hidden {
807 continue
808 }
809 r := tiles[i].Rect
810 x0, y0 := scale(r.X), scale(r.Y)
811 tiles[i].Rect = Rect{X: x + x0, Y: y + y0, W: scale(r.X+r.W) - x0, H: scale(r.Y+r.H) - y0}
812 tiles[i].Preview = z
813 if dim {
814 tiles[i].peeking(1)
815 }
816 tiles[i].Focused = tiles[i].Focused && !dim && lit
817 }
818 return tiles
819}
820
821// overviewOutline frames the selected preview with active lines of width
822// b, just outside it.
823func overviewOutline(layout []Placement, b int) []ports.Separator {
824 for _, p := range layout {
825 if p.Preview > 0 && p.Focused && !p.Hidden {
826 r := p.Rect
827 return []ports.Separator{
828 {Rect: Rect{X: r.X - b, Y: r.Y - b, W: r.W + 2*b, H: b}, Active: true},
829 {Rect: Rect{X: r.X - b, Y: r.Y + r.H, W: r.W + 2*b, H: b}, Active: true},
830 {Rect: Rect{X: r.X - b, Y: r.Y, W: b, H: r.H}, Active: true},
831 {Rect: Rect{X: r.X + r.W, Y: r.Y, W: b, H: r.H}, Active: true},
832 }
833 }
834 }
835 return nil
836}
837
838// overviewClickFade is the fade from which a card takes no click.
839const overviewClickFade = 0.5
840
841// overviewIn is the preview under the output-local point in layout, if
842// any. Front cards take priority over peeks, including peeks appended after
843// a front tile in another row's layout. The click path passes the shown
844// layout, so a card is hit where it is drawn while it moves. A card that is
845// nearly invisible (still fading in) is not clickable.
846func overviewIn(layout []Placement, x, y float64) WindowID {
847 for i := len(layout) - 1; i >= 0; i-- {
848 p := layout[i]
849 r := p.Rect
850 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) {
851 return p.ID
852 }
853 }
854 for i := len(layout) - 1; i >= 0; i-- {
855 p := layout[i]
856 r := p.Rect
857 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) {
858 return p.ID
859 }
860 }
861 return 0
862}
863
864// overviewClick closes the overview on the preview under the pointer, on
865// the output it shows on. picked is false when no preview is there.
866func (c *Core) overviewClick(ctx context.Context) (picked bool, err error) {
867 i := c.screenAt(c.cursorX, c.cursorY)
868 if i < 0 {
869 return false, nil
870 }
871 sc := c.screens[i]
872 if !sc.mon.ov.open {
873 return false, nil
874 }
875 // The cards are hit where they are drawn: a click during the open or a
876 // navigation animation picks what moves under the pointer.
877 id := overviewIn(sc.shownLayout(), c.cursorX-float64(sc.x), c.cursorY-float64(sc.y))
878 if id == 0 {
879 return false, nil
880 }
881 now := c.now()
882 shots := c.snapshot(now)
883 sc.mon.OverviewPick(id)
884 c.transition(shots, now)
885 c.focusScreen = i
886 c.keyboard.takeBack()
887 if err := c.workspaceVisible(ctx, true); err != nil {
888 return true, err
889 }
890 return true, c.publish(ctx)
891}
892
893// overviewAction runs a bind while the overview is open: focus binds move
894// the selection, close-window and move-to-workspace binds act on the
895// selected preview, other mutations are dropped. handled is false for binds
896// that run as usual.
897func (m *Monitor) overviewAction(a Action) (e Effect, handled bool) {
898 if !m.ov.open {
899 return Effect{}, false
900 }
901 if a == ActionCloseWindow {
902 return Effect{Close: m.overviewTarget()}, true
903 }
904 if movesToWorkspace(a) {
905 m.overviewMoveTo(a)
906 return Effect{}, true
907 }
908 return Effect{}, m.overviewFocus(a) || overviewBlocks(a)
909}
910
911// overviewFocus runs a focus bind as an overview move, so the focus binds
912// and the bare keys navigate the overview the same way: true when a was
913// one of them.
914func (m *Monitor) overviewFocus(a Action) bool {
915 switch a {
916 case ActionFocusColumnLeft:
917 m.OverviewMove(-1, 0)
918 case ActionFocusColumnRight:
919 m.OverviewMove(1, 0)
920 case ActionFocusWindowUp, ActionFocusWorkspaceUp:
921 m.OverviewMove(0, -1)
922 case ActionFocusWindowDown, ActionFocusWorkspaceDown:
923 m.OverviewMove(0, 1)
924 default:
925 return false
926 }
927 return true
928}
929
930const (
931 // overviewScrollStep is the scroll distance, in libinput's pointer
932 // units, that moves the selection one column or workspace.
933 overviewScrollStep = 60
934 // overviewScrollRepeat is the two-finger distance of each further
935 // column step in one scroll: longer than the first, so a short scroll
936 // reaches the next column exactly and a long one goes on.
937 overviewScrollRepeat = 150
938)
939
940// overviewScroll moves the overview selection with a scroll frame: a row
941// step when the vertical scroll reaches overviewScrollStep and leads, else
942// a column step. Two fingers step to the next column after
943// overviewScrollStep, then once per overviewScrollRepeat, and stay on
944// columns until they lift; up or down they step once per scroll, so a card
945// in a stack is easy to pick.
946// Continuous scrolling steps once per overviewScrollStep and a wheel once
947// per notch (high-resolution wheels add up their fractions of a notch).
948// Vertical steps select cards first, then rows; natural scroll
949// (touchpad.* or mouse.*) flips both axes as libinput reports. The end of
950// a scroll resets it (scrollStop).
951func (m *Monitor) overviewScroll(a ports.PointerAxis) (changed bool) {
952 if a.Vertical.Stop || a.Horizontal.Stop {
953 return false
954 }
955 finger := a.Source == ports.AxisFinger
956 if finger && m.ov.scrolled {
957 return false
958 }
959 // A sideways finger scroll stays on columns; other sources do not, and
960 // start from zero rather than from its leftover.
961 sideways := finger && m.ov.sideways
962 if !finger && m.ov.sideways {
963 m.ov.scrollX, m.ov.scrollY = 0, 0
964 }
965 stepX := float64(overviewScrollStep)
966 if sideways {
967 stepX = overviewScrollRepeat
968 }
969 add := func(acc *float64, ax ports.ScrollAxis) {
970 switch {
971 case !ax.Set:
972 case a.Source == ports.AxisWheel:
973 *acc += float64(ax.V120) / 120 * overviewScrollStep
974 default:
975 *acc += ax.Value
976 }
977 }
978 if !sideways {
979 add(&m.ov.scrollY, a.Vertical)
980 }
981 add(&m.ov.scrollX, a.Horizontal)
982 for {
983 switch {
984 case math.Abs(m.ov.scrollY) >= overviewScrollStep && math.Abs(m.ov.scrollY) >= math.Abs(m.ov.scrollX):
985 m.OverviewMove(0, sign(m.ov.scrollY))
986 m.ov.scrollY -= float64(sign(m.ov.scrollY)) * overviewScrollStep
987 m.ov.scrollX = 0
988 if finger {
989 m.ov.scrollX, m.ov.scrollY, m.ov.scrolled = 0, 0, true
990 return true
991 }
992 case math.Abs(m.ov.scrollX) >= stepX:
993 m.OverviewMove(sign(m.ov.scrollX), 0)
994 m.ov.scrollX -= float64(sign(m.ov.scrollX)) * stepX
995 m.ov.scrollY = 0
996 if finger {
997 m.ov.sideways, stepX = true, overviewScrollRepeat
998 }
999 default:
1000 return changed
1001 }
1002 changed = true
1003 }
1004}
1005
1006// scrollStop ends a scroll on every monitor: the fingers lifted, maybe
1007// after the pointer took the focus to another output, so the next scroll
1008// starts from zero wherever it goes.
1009func (c *Core) scrollStop(a ports.PointerAxis) {
1010 if !a.Vertical.Stop && !a.Horizontal.Stop {
1011 return
1012 }
1013 for _, sc := range c.screens {
1014 sc.mon.ov.scrollX, sc.mon.ov.scrollY, sc.mon.ov.scrolled, sc.mon.ov.sideways = 0, 0, false, false
1015 }
1016}
1017
1018func sign(v float64) int {
1019 if v < 0 {
1020 return -1
1021 }
1022 return 1
1023}
1024
1025// overviewKeyboardTaken reports whether a layer surface (a launcher) or a
1026// grabbing menu has the keyboard: it gets the keys, not the overview.
1027func (c *Core) overviewKeyboardTaken() bool {
1028 id := c.keyboardFocus()
1029 _, _, layer := c.layerOf(id)
1030 return layer || c.popups[id] != nil
1031}
1032
1033// overviewKey runs an overview key: true when the key was one.
1034func (m *Monitor) overviewKey(key ports.KeyEvent) bool {
1035 if key.Mods != 0 {
1036 return false
1037 }
1038 switch keyName(key.Keysym) {
1039 case "h", "Left":
1040 m.OverviewMove(-1, 0)
1041 case "l", "Right":
1042 m.OverviewMove(1, 0)
1043 case "k", "Up":
1044 m.OverviewMove(0, -1)
1045 case "j", "Down":
1046 m.OverviewMove(0, 1)
1047 case "Return", "KP_Enter":
1048 m.ToggleOverview()
1049 case "Escape":
1050 m.CancelOverview()
1051 default:
1052 return false
1053 }
1054 return true
1055}