layout_policy.go RawHistoryFolder
1package core
2
3// layoutAxis is a logical navigation axis, independent of rendering.
4type layoutAxis uint8
5
6const (
7 verticalAxis layoutAxis = iota
8 horizontalAxis
9)
10
11func (a layoutAxis) span(r Rect) int {
12 if a == verticalAxis {
13 return r.H
14 }
15 return r.W
16}
17func (a layoutAxis) offset(r *Rect, pixels int) {
18 if a == verticalAxis {
19 r.Y += pixels
20 } else {
21 r.X += pixels
22 }
23}
24
25// layoutPolicy keeps geometry capabilities distinct from workspace navigation.
26// Window state remains owned by Workspace; policies contain no mutable state.
27type layoutPolicy struct {
28 content, workspace layoutAxis
29 equalCells, wraps bool
30}
31
32// maximizeHides reports whether a maximized column hides the others, so
33// it is a transient state that focus changes undo: fixed overflow. In
34// scroll overflow and cascade it is a lasting state of its column (a
35// cascade band packs a maximized column alone).
36func (p layoutPolicy) maximizeHides() bool { return p.equalCells && !p.wraps }
37
38// workspaceStep is the workspace step a directional focus past the content
39// edge takes along axis: dir when axis is the workspace axis, else 0.
40func (p layoutPolicy) workspaceStep(axis layoutAxis, dir int) int {
41 if axis == p.workspace {
42 return dir
43 }
44 return 0
45}
46
47func (w *Workspace) policy() layoutPolicy {
48 if w.Overflow == OverflowCascade {
49 return layoutPolicy{content: verticalAxis, workspace: horizontalAxis, equalCells: true, wraps: true}
50 }
51 if w.Overflow == OverflowFixed {
52 return layoutPolicy{content: horizontalAxis, workspace: verticalAxis, equalCells: true}
53 }
54 return layoutPolicy{content: horizontalAxis, workspace: verticalAxis}
55}
56
57// validSpans requires a complete, exclusive lane stamp: every column has
58// a positive span and the same nonzero identity, with no members elsewhere
59// in the workspace. Equal-cell lanes must also sum to 10000 basis points.
60func (w *Workspace) validSpans(first, last int) bool {
61 if first > last || w.Overflow == OverflowFixed && !w.laneRow() {
62 return false
63 }
64 stamp := w.Columns[first].spanStamp
65 if stamp == 0 {
66 return false
67 }
68 total := 0
69 for i := first; i <= last; i++ {
70 c := w.Columns[i]
71 if c.Span <= 0 || c.spanStamp != stamp {
72 return false
73 }
74 total += c.Span
75 }
76 count := 0
77 for _, c := range w.Columns {
78 if c.spanStamp == stamp {
79 count++
80 }
81 }
82 return count == last-first+1 && (!w.policy().equalCells || total == 10000)
83}
84
85// dropStaleSpans runs before scroll measures mutated layout geometry. Decide
86// validity before clearing any stamps, so shared identities cannot become
87// valid merely because an earlier invalid lane was cleared.
88func (w *Workspace) dropStaleSpans() {
89 stamped := false
90 for _, c := range w.Columns {
91 if c.Span != 0 || c.spanStamp != 0 {
92 stamped = true
93 break
94 }
95 }
96 if !stamped {
97 return
98 }
99 // A nil stale slice does not allocate, so valid lanes cost nothing here.
100 var stale []int
101 for first := 0; first < len(w.Columns); {
102 _, last := w.laneBounds(w.laneOf(first))
103 if !w.validSpans(first, last) {
104 for i := first; i <= last; i++ {
105 stale = append(stale, i)
106 }
107 }
108 first = last + 1
109 }
110 for _, i := range stale {
111 w.Columns[i].Span, w.Columns[i].spanStamp = 0, 0
112 }
113}
114
115func (w *Workspace) clearSpans(col int) {
116 if col < 0 || col >= len(w.Columns) {
117 return
118 }
119 first, last := w.laneBounds(w.laneOf(col))
120 for i := first; i <= last; i++ {
121 w.Columns[i].Span = 0
122 w.Columns[i].spanStamp = 0
123 }
124}