~eliasnaur/gio

ref: 9f58ed0fea4e7b1f8f69846d8f72bec19390d7ed gio/ui/pointer/queue.go -rw-r--r-- 4.6 KiB
9f58ed0fElias Naur ui: add version to OpBlock to track invalidated blocks 2 years ago
                                                                                
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
// SPDX-License-Identifier: Unlicense OR MIT

package pointer

import (
	"gioui.org/ui"
	"gioui.org/ui/f32"
	"gioui.org/ui/internal/ops"
)

type Queue struct {
	hitTree  []hitNode
	handlers map[Key]*handler
	pointers []pointerInfo
	reader   ui.OpsReader
	scratch  []Key
}

type hitNode struct {
	// The layer depth.
	level int
	// The handler, or nil for a layer.
	key Key
}

type pointerInfo struct {
	id       ID
	pressed  bool
	handlers []Key
}

type handler struct {
	area      Area
	active    bool
	transform ui.Transform
	events    []Event
	wantsGrab bool
}

func (q *Queue) collectHandlers(r *ui.OpsReader, t ui.Transform, layer int) {
	for {
		data, refs, ok := r.Decode()
		if !ok {
			return
		}
		switch ops.OpType(data[0]) {
		case ops.TypePush:
			q.collectHandlers(r, t, layer)
		case ops.TypePop:
			return
		case ops.TypeLayer:
			layer++
			q.hitTree = append(q.hitTree, hitNode{level: layer})
		case ops.TypeTransform:
			var op ui.OpTransform
			op.Decode(data)
			t = t.Mul(op.Transform)
		case ops.TypePointerHandler:
			var op OpHandler
			op.Decode(data, refs)
			q.hitTree = append(q.hitTree, hitNode{level: layer, key: op.Key})
			h, ok := q.handlers[op.Key]
			if !ok {
				h = new(handler)
				q.handlers[op.Key] = h
			}
			h.area = op.Area
			h.transform = t
			h.wantsGrab = h.wantsGrab || op.Grab
		}
	}
}

func (q *Queue) opHit(handlers *[]Key, pos f32.Point) {
	level := 1 << 30
	opaque := false
	for i := len(q.hitTree) - 1; i >= 0; i-- {
		n := q.hitTree[i]
		if n.key == nil {
			// Layer
			if opaque {
				opaque = false
				// Skip sibling handlers.
				level = n.level - 1
			}
		} else if n.level <= level {
			// Handler
			h, ok := q.handlers[n.key]
			if !ok {
				continue
			}
			tpos := h.transform.InvTransform(pos)
			res := h.area.Hit(tpos)
			opaque = opaque || res == HitOpaque
			if res != HitNone {
				*handlers = append(*handlers, n.key)
			}
		}
	}
}

func (q *Queue) init() {
	if q.handlers == nil {
		q.handlers = make(map[Key]*handler)
	}
}

func (q *Queue) Frame(root *ui.Ops) {
	q.init()
	for k, h := range q.handlers {
		if !h.active {
			q.dropHandler(k)
		} else {
			// Reset handler.
			h.events = h.events[:0]
		}
	}
	q.hitTree = q.hitTree[:0]
	q.reader.Reset(root)
	q.collectHandlers(&q.reader, ui.Transform{}, 0)
}

func (q *Queue) For(k Key) []Event {
	if k == nil {
		panic("nil handler")
	}
	q.init()
	h, ok := q.handlers[k]
	if !ok {
		h = new(handler)
		q.handlers[k] = h
	}
	if !h.active {
		h.active = true
		// Prepend a Cancel.
		h.events = append(h.events, Event{})
		copy(h.events[1:], h.events)
		h.events[0] = Event{Type: Cancel}
	}
	return h.events
}

func (q *Queue) dropHandler(k Key) {
	delete(q.handlers, k)
	for i := range q.pointers {
		p := &q.pointers[i]
		for i := len(p.handlers) - 1; i >= 0; i-- {
			if p.handlers[i] == k {
				p.handlers = append(p.handlers[:i], p.handlers[i+1:]...)
			}
		}
	}
}

func (q *Queue) Push(e Event) {
	q.init()
	if e.Type == Cancel {
		q.pointers = q.pointers[:0]
		for k := range q.handlers {
			q.dropHandler(k)
		}
		return
	}
	pidx := -1
	for i, p := range q.pointers {
		if p.id == e.PointerID {
			pidx = i
			break
		}
	}
	if pidx == -1 {
		q.pointers = append(q.pointers, pointerInfo{id: e.PointerID})
		pidx = len(q.pointers) - 1
	}
	p := &q.pointers[pidx]
	if !p.pressed && (e.Type == Move || e.Type == Press) {
		p.handlers, q.scratch = q.scratch[:0], p.handlers
		q.opHit(&p.handlers, e.Position)
		// Drop handlers no longer hit.
	loop:
		for _, h := range q.scratch {
			for _, h2 := range p.handlers {
				if h == h2 {
					continue loop
				}
			}
			q.dropHandler(h)
		}
		if e.Type == Press {
			p.pressed = true
		}
	}
	if p.pressed {
		// Resolve grabs.
		q.scratch = q.scratch[:0]
		for i, k := range p.handlers {
			h := q.handlers[k]
			if h.wantsGrab {
				q.scratch = append(q.scratch, p.handlers[:i]...)
				q.scratch = append(q.scratch, p.handlers[i+1:]...)
				break
			}
		}
		// Drop handlers that lost their grab.
		for _, k := range q.scratch {
			q.dropHandler(k)
		}
	}
	if e.Type == Release {
		q.pointers = append(q.pointers[:pidx], q.pointers[pidx+1:]...)
	}
	for i, k := range p.handlers {
		h := q.handlers[k]
		e := e
		switch {
		case p.pressed && len(p.handlers) == 1:
			e.Priority = Grabbed
		case i == 0:
			e.Priority = Foremost
		}
		e.Position = h.transform.InvTransform(e.Position)
		e.Hit = h.area.Hit(e.Position) != HitNone
		h.events = append(h.events, e)
		if e.Type == Release {
			// Release grab when the number of grabs reaches zero.
			grabs := 0
			for _, p := range q.pointers {
				if p.pressed && len(p.handlers) == 1 && p.handlers[0] == k {
					grabs++
				}
			}
			if grabs == 0 {
				h.wantsGrab = false
			}
		}
	}
}