Repository navigation
Expand file tree
/
Copy pathapp_hover.go
More file actions
246 lines (226 loc) · 6.97 KB
/
Copy pathapp_hover.go
File metadata and controls
246 lines (226 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package tui
import (
"fmt"
"slices"
)
// Re-renders rebuild every element, so hover cannot remember element pointers.
// Instead it remembers, for each element it entered, the element's path in the
// tree and the onMouseLeave it had at that moment. Paths use an element's key
// when it has one and its sibling index otherwise. On each mouse event the
// chain under the pointer is compared by path: a re-render that keeps the
// structure keeps the paths, so nothing fires and a handler that sets state
// cannot loop. Anything that drops out (pointer moved, modal opened, list
// changed) gets its saved onMouseLeave, so every enter gets exactly one leave.
//
// Content can also change under a still pointer (wheel scroll, a key press, a
// background update). After a render that followed any dispatched event, hover
// is re-checked once at the last pointer position. Renders caused only by
// hover callbacks do not re-check, which bounds a handler that moves its own
// element to one extra round instead of a loop.
// hoverEntry is an element the pointer entered.
type hoverEntry struct {
path []pathSeg
leave func()
}
// pathSeg is one step from a parent to a child: the child's key, or its
// index when it has none.
type pathSeg struct {
key string
keyed bool
index int
}
// dispatchMouseHover updates hover for a mouse event at (x, y) in buffer
// coordinates. inside is false when the pointer left the app's region
// (inline mode), which leaves everything currently hovered.
func (a *App) dispatchMouseHover(x, y int, inside bool) {
if !a.mouseMotion {
return
}
a.pointerX, a.pointerY, a.pointerInside = x, y, inside
a.updateHover()
}
// enableHoverMotion turns on motion reporting the first time a rendered tree
// uses hover, so hover works without WithMouseMotion. It stays on afterwards:
// toggling per page would churn terminal modes for no real saving.
func (a *App) enableHoverMotion() {
if !a.hoverUsed || a.mouseMotion || !a.mouseEnabled {
return
}
a.mouseMotion = true
a.setMouseMotion(true)
}
// recheckHover re-runs hover at the last pointer position after a render, if
// an event was dispatched since the last check.
func (a *App) recheckHover() {
if !a.hoverStale {
return
}
a.hoverStale = false
if a.mouseMotion && a.pointerInside {
a.updateHover()
}
}
// updateHover fires onMouseLeave/onMouseEnter for the pointer at its last
// known position against the current tree, then syncs hover: styles.
func (a *App) updateHover() {
var chain []*Element
if a.pointerInside {
chain = a.hoverChain(a.pointerX, a.pointerY)
}
paths := make([][]pathSeg, len(chain))
for i, el := range chain {
paths[i] = elementPath(el)
}
hovered := func(path []pathSeg) bool {
return slices.ContainsFunc(paths, func(p []pathSeg) bool { return slices.Equal(p, path) })
}
// Leave deepest first, enter outermost first (matches DOM mouseleave/mouseenter).
var kept []hoverEntry
var left []func()
for _, h := range a.hovered {
if hovered(h.path) {
kept = append(kept, h)
} else if h.leave != nil {
left = append(left, h.leave)
}
}
var entered []func()
var next []hoverEntry
for i, el := range chain {
if !hasHover(el) || slices.ContainsFunc(kept, func(h hoverEntry) bool { return slices.Equal(h.path, paths[i]) }) {
continue
}
if el.onMouseEnter != nil {
entered = append(entered, el.onMouseEnter)
}
next = append(next, hoverEntry{path: paths[i], leave: el.onMouseLeave})
}
a.hovered = append(kept, next...)
slices.SortStableFunc(a.hovered, func(x, y hoverEntry) int { return len(y.path) - len(x.path) })
a.syncHoverStyles(true)
for _, fn := range left {
fn()
}
for _, fn := range slices.Backward(entered) {
fn()
}
}
// syncHoverStyles makes the elements at hovered paths in the current tree,
// and only those, show their hover: classes. Called after hover changes
// (markDirty) and after a re-render builds fresh elements, before layout
// (no markDirty, or every frame would schedule another).
func (a *App) syncHoverStyles(markDirty bool) {
var styled []*Element
for _, h := range a.hovered {
if el := a.elementAtPath(h.path); el != nil && el.hoverClass != "" {
styled = append(styled, el)
}
}
for _, el := range a.hoverStyled {
if el.hoverActive && !slices.Contains(styled, el) {
el.setHoverStyle(false)
if markDirty {
el.MarkDirty()
}
}
}
for _, el := range styled {
if !el.hoverActive {
el.setHoverStyle(true)
if markDirty {
el.MarkDirty()
}
}
}
a.hoverStyled = styled
}
func hasHover(el *Element) bool {
return el.onMouseEnter != nil || el.onMouseLeave != nil || el.hoverClass != ""
}
// hoverChain returns the element under (x, y) and its ancestors, deepest
// first. While an overlay is open only the topmost overlay is hit-tested, and
// the chain stops at the overlay so content behind the backdrop never hovers.
func (a *App) hoverChain(x, y int) []*Element {
scope := a.hitScope()
if scope == nil {
return nil
}
var chain []*Element
for el := scope.ElementAt(x, y); el != nil; el = el.parent {
chain = append(chain, el)
if el == scope {
break
}
}
return chain
}
// hitScope is the subtree mouse hit testing searches: the topmost open
// overlay while one is open, so content behind its backdrop never reacts,
// and the root otherwise.
func (a *App) hitScope() *Element {
if n := len(a.overlays); n > 0 {
return a.overlays[n-1].element
}
return a.root
}
// elementPath is the element's path from the tree root, which survives a
// re-render that keeps the same structure (and, for keyed elements, a reorder).
func elementPath(el *Element) []pathSeg {
var path []pathSeg
for ; el.parent != nil; el = el.parent {
path = append(path, segmentOf(el))
}
slices.Reverse(path)
return path
}
func segmentOf(el *Element) pathSeg {
if el.hasKey {
return pathSeg{key: fmt.Sprint(el.key), keyed: true}
}
return pathSeg{index: slices.Index(el.parent.children, el)}
}
// elementAtPath resolves a path in the current tree, or nil if nothing is
// there any more.
func (a *App) elementAtPath(path []pathSeg) *Element {
el := a.root
for _, seg := range path {
if el == nil {
return nil
}
var next *Element
if seg.keyed {
for _, c := range el.children {
if c.hasKey && fmt.Sprint(c.key) == seg.key {
next = c
break
}
}
} else if seg.index < len(el.children) && !el.children[seg.index].hasKey {
next = el.children[seg.index]
}
el = next
}
return el
}
// moveCoalescer holds back MouseMove events while a batch of events drains,
// so a burst of motion dispatches only its last position.
type moveCoalescer struct {
ev MouseEvent
held bool
}
// dispatchCoalescing dispatches ev, holding back MouseMove events until the
// next other event (dispatched after the held move, preserving order) or flush.
func (a *App) dispatchCoalescing(ev Event, c *moveCoalescer) {
if me, ok := ev.(MouseEvent); ok && me.Action == MouseMove {
c.ev, c.held = me, true
return
}
a.flushMove(c)
a.Dispatch(ev)
}
func (a *App) flushMove(c *moveCoalescer) {
if c.held {
c.held = false
a.Dispatch(c.ev)
}
}