~eliasnaur/gio

ref: 48b6a73753e7ee567154297987f24a4d10485999 gio/ui/pointer/queue.go -rw-r--r-- 4.8 KiB
48b6a737Elias Naur all: run goimports 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
238
239
240
241
242
243
244
245
246
247
// SPDX-License-Identifier: Unlicense OR MIT

package pointer

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

type Queue struct {
	// The root of the tree of ops relevant to pointer handling.
	root     ui.Op
	handlers map[Key]*handler
	pointers []pointerInfo
	scratch  []Key
}

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

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

type childOp interface {
	ChildOp() ui.Op
}

func (q *Queue) collectHandlers(op ui.Op, t ui.Transform) ui.Op {
	switch op := op.(type) {
	case ui.Ops:
		var all ui.Ops
		for _, op := range op {
			if op := q.collectHandlers(op, t); op != nil {
				if ops, ok := op.(ui.Ops); ok {
					all = append(all, ops...)
				} else {
					all = append(all, op)
				}
			}
		}
		return all
	case ui.OpLayer:
		child := q.collectHandlers(op.ChildOp(), t)
		if child == nil {
			return nil
		}
		return ui.OpLayer{Op: child}
	case ui.OpTransform:
		return q.collectHandlers(op.ChildOp(), t.Mul(op.Transform))
	case OpHandler:
		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
		return op
	case childOp:
		return q.collectHandlers(op.ChildOp(), t)
	default:
		return nil
	}
}

func (q *Queue) opHit(handlers *[]Key, op ui.Op, pos f32.Point) (HitResult, bool) {
	if op == nil {
		return HitNone, false
	}
	switch op := op.(type) {
	case ui.Ops:
		hitRes := HitNone
		var layer bool
		for i := len(op) - 1; i >= 0; i-- {
			op := op[i]
			if _, ok := op.(ui.OpLayer); layer && ok {
				continue
			}
			res, l := q.opHit(handlers, op, pos)
			if res > hitRes {
				hitRes = res
			}
			layer = layer || l
		}
		return hitRes, layer
	case ui.OpLayer:
		res, layer := q.opHit(handlers, op.Op, pos)
		return res, layer || res == HitOpaque
	case OpHandler:
		h, ok := q.handlers[op.Key]
		if !ok {
			return HitNone, false
		}
		tpos := h.transform.InvTransform(pos)
		res := h.area(tpos)
		if res != HitNone {
			*handlers = append(*handlers, op.Key)
		}
		return res, false
	default:
		panic("unexpected op")
	}
}

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

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

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, q.root, 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(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
			}
		}
	}
}