1package core
2
3import "slices"
4
5func (w *Workspace) band(i int) int { return i / max(w.MaxColumns, 1) }
6func (w *Workspace) sameBand(a, b int) bool { return !w.policy().wraps || w.band(a) == w.band(b) }
7func (w *Workspace) cellX(i int) int {
8 return w.gap() + (i%max(w.MaxColumns, 1))*(w.cellWidth(i)+w.gap())
9}
10func (w *Workspace) cascadeBands() (n, spacing int) {
11 if len(w.Columns) == 0 || w.Usable.H <= 0 {
12 return 1, 0
13 }
14 return w.band(len(w.Columns)-1) + 1, w.Usable.H
15}
16
17// cellWidth is the width of the cell of column i: the columns of its band
18// share the usable width equally, as in fixed overflow, so a lone column
19// fills it.
20func (w *Workspace) cellWidth(i int) int {
21 first, last := w.laneBounds(w.band(i))
22 return w.equalShare(last - first + 1)
23}
24
25// equalShare is the width of each of k columns sharing the usable width
26// after gaps; a remainder under k pixels stays empty.
27func (w *Workspace) equalShare(k int) int {
28 k = max(k, 1)
29 return max((w.Usable.W-w.gap()*(k+1))/k, 0)
30}
31func (w *Workspace) cellInBand(b int) int {
32 k := max(w.MaxColumns, 1)
33 return min(b*k+w.Focus%k, len(w.Columns)-1)
34}
35func (w *Workspace) focusBand(dir int) bool {
36 i := w.laneNeighbor(dir)
37 if i < 0 {
38 return false
39 }
40 w.unmaximize()
41 w.Focus = i
42 w.Columns[i].Focus = 0
43 if dir < 0 {
44 w.Columns[i].Focus = len(w.Columns[i].Windows) - 1
45 }
46 w.raiseColumns()
47 w.scroll()
48 return true
49}
50func (w *Workspace) cascadeRectsInto(dst []Rect, ignoreFullWidth bool) []Rect {
51 g := w.gap()
52 rects := slices.Grow(dst[:0], len(w.Columns))[:len(w.Columns)]
53 view := w.View + w.shiftPixels()
54 for i := range w.Columns {
55 r := Rect{X: w.Usable.X + w.cellX(i), Y: w.Usable.Y + g + w.band(i)*w.Usable.H - view, W: w.cellWidth(i), H: max(w.Usable.H-2*g, 0)}
56 if w.Columns[i].FullWidth && !ignoreFullWidth {
57 r.X = w.Usable.X + g
58 r.W = max(w.Usable.W-2*g, 0)
59 }
60 rects[i] = r
61 }
62 return rects
63}
64
65func (w *Workspace) cascadePoints() []float64 {
66 n, spacing := w.cascadeBands()
67 points := make([]float64, n)
68 for i := range points {
69 points[i] = float64(i * spacing)
70 }
71 return points
72}
73
74func (w *Workspace) cascadeFocus(view int) int {
75 if len(w.Columns) == 0 {
76 return 0
77 }
78 b := min(max(view/max(w.Usable.H, 1), 0), w.band(len(w.Columns)-1))
79 return w.cellInBand(b)
80}
81
82// laneOf is the lane of column i in the overview: its band, or the only
83// lane of a layout that does not wrap.
84func (w *Workspace) laneOf(i int) int {
85 if w.policy().wraps {
86 return w.band(i)
87 }
88 return 0
89}
90
91// laneBounds is the range of columns of a lane, first > last when the lane
92// does not exist. A layout that does not wrap has the whole row as lane 0.
93func (w *Workspace) laneBounds(lane int) (first, last int) {
94 k := len(w.Columns)
95 if w.policy().wraps {
96 k = max(w.MaxColumns, 1)
97 }
98 first = lane * k
99 if lane < 0 || first >= len(w.Columns) {
100 return 0, -1
101 }
102 return first, min(first+k, len(w.Columns)) - 1
103}
104
105// laneNeighbor is the closest column to the focus in the lane d lanes away,
106// the focus's offset in its lane kept; -1 past the first or last lane.
107func (w *Workspace) laneNeighbor(d int) int {
108 lane := w.laneOf(w.Focus)
109 first, _ := w.laneBounds(lane)
110 nfirst, nlast := w.laneBounds(lane + d)
111 if nfirst > nlast {
112 return -1
113 }
114 return min(nfirst+w.Focus-first, nlast)
115}