forked from gioui/gio
/
pointer.go
404 lines (374 loc) · 10.7 KB
/
pointer.go
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// SPDX-License-Identifier: Unlicense OR MIT
package pointer
import (
"encoding/binary"
"fmt"
"image"
"strings"
"time"
"gioui.org/f32"
"gioui.org/io/event"
"gioui.org/io/key"
"gioui.org/op"
"github.com/MTGArenaChat/gio/internal/ops"
)
// Event is a pointer event.
type Event struct {
Kind Kind
Source Source
// PointerID is the id for the pointer and can be used
// to track a particular pointer from Press to
// Release or Cancel.
PointerID ID
// Priority is the priority of the receiving handler
// for this event.
Priority Priority
// Time is when the event was received. The
// timestamp is relative to an undefined base.
Time time.Duration
// Buttons are the set of pressed mouse buttons for this event.
Buttons Buttons
// Position is the position of the event, relative to
// the current transformation, as set by op.TransformOp.
Position f32.Point
// Scroll is the scroll amount, if any.
Scroll f32.Point
// Modifiers is the set of active modifiers when
// the mouse button was pressed.
Modifiers key.Modifiers
}
// PassOp sets the pass-through mode. InputOps added while the pass-through
// mode is set don't block events to siblings.
type PassOp struct {
}
// PassStack represents a PassOp on the pass stack.
type PassStack struct {
ops *ops.Ops
id ops.StackID
macroID int
}
// InputOp declares an input handler ready for pointer
// events.
type InputOp struct {
Tag event.Tag
// Grab, if set, request that the handler get
// Grabbed priority.
Grab bool
// Kinds is a bitwise-or of event types to receive.
Kinds Kind
// ScrollBounds describe the maximum scrollable distances in both
// axes. Specifically, any Event e delivered to Tag will satisfy
//
// ScrollBounds.Min.X <= e.Scroll.X <= ScrollBounds.Max.X (horizontal axis)
// ScrollBounds.Min.Y <= e.Scroll.Y <= ScrollBounds.Max.Y (vertical axis)
ScrollBounds image.Rectangle
}
type ID uint16
// Kind of an Event.
type Kind uint
// Priority of an Event.
type Priority uint8
// Source of an Event.
type Source uint8
// Buttons is a set of mouse buttons
type Buttons uint8
// Cursor denotes a pre-defined cursor shape. Its Add method adds an
// operation that sets the cursor shape for the current clip area.
type Cursor byte
// The cursors correspond to CSS pointer naming.
const (
// CursorDefault is the default cursor.
CursorDefault Cursor = iota
// CursorNone hides the cursor. To show it again, use any other cursor.
CursorNone
// CursorText is for selecting and inserting text.
CursorText
// CursorVerticalText is for selecting and inserting vertical text.
CursorVerticalText
// CursorPointer is for a link.
// Usually displayed as a pointing hand.
CursorPointer
// CursorCrosshair is for a precise location.
CursorCrosshair
// CursorAllScroll is for indicating scrolling in all directions.
// Usually displayed as arrows to all four directions.
CursorAllScroll
// CursorColResize is for vertical resize.
// Usually displayed as a vertical bar with arrows pointing east and west.
CursorColResize
// CursorRowResize is for horizontal resize.
// Usually displayed as a horizontal bar with arrows pointing north and south.
CursorRowResize
// CursorGrab is for content that can be grabbed (dragged to be moved).
// Usually displayed as an open hand.
CursorGrab
// CursorGrabbing is for content that is being grabbed (dragged to be moved).
// Usually displayed as a closed hand.
CursorGrabbing
// CursorNotAllowed is shown when the request action cannot be carried out.
// Usually displayed as a circle with a line through.
CursorNotAllowed
// CursorWait is shown when the program is busy and user cannot interact.
// Usually displayed as a hourglass or the system equivalent.
CursorWait
// CursorProgress is shown when the program is busy, but the user can still interact.
// Usually displayed as a default cursor with a hourglass.
CursorProgress
// CursorNorthWestResize is for top-left corner resizing.
// Usually displayed as an arrow towards north-west.
CursorNorthWestResize
// CursorNorthEastResize is for top-right corner resizing.
// Usually displayed as an arrow towards north-east.
CursorNorthEastResize
// CursorSouthWestResize is for bottom-left corner resizing.
// Usually displayed as an arrow towards south-west.
CursorSouthWestResize
// CursorSouthEastResize is for bottom-right corner resizing.
// Usually displayed as an arrow towards south-east.
CursorSouthEastResize
// CursorNorthSouth is for top-bottom resizing.
// Usually displayed as a bi-directional arrow towards north-south.
CursorNorthSouthResize
// CursorEastWestResize is for left-right resizing.
// Usually displayed as a bi-directional arrow towards east-west.
CursorEastWestResize
// CursorWestResize is for left resizing.
// Usually displayed as an arrow towards west.
CursorWestResize
// CursorEastResize is for right resizing.
// Usually displayed as an arrow towards east.
CursorEastResize
// CursorNorthResize is for top resizing.
// Usually displayed as an arrow towards north.
CursorNorthResize
// CursorSouthResize is for bottom resizing.
// Usually displayed as an arrow towards south.
CursorSouthResize
// CursorNorthEastSouthWestResize is for top-right to bottom-left diagonal resizing.
// Usually displayed as a double ended arrow on the corresponding diagonal.
CursorNorthEastSouthWestResize
// CursorNorthWestSouthEastResize is for top-left to bottom-right diagonal resizing.
// Usually displayed as a double ended arrow on the corresponding diagonal.
CursorNorthWestSouthEastResize
)
const (
// A Cancel event is generated when the current gesture is
// interrupted by other handlers or the system.
Cancel Kind = (1 << iota) >> 1
// Press of a pointer.
Press
// Release of a pointer.
Release
// Move of a pointer.
Move
// Drag of a pointer.
Drag
// Pointer enters an area watching for pointer input
Enter
// Pointer leaves an area watching for pointer input
Leave
// Scroll of a pointer.
Scroll
)
const (
// Mouse generated event.
Mouse Source = iota
// Touch generated event.
Touch
)
const (
// Shared priority is for handlers that
// are part of a matching set larger than 1.
Shared Priority = iota
// Foremost priority is like Shared, but the
// handler is the foremost of the matching set.
Foremost
// Grabbed is used for matching sets of size 1.
Grabbed
)
const (
// ButtonPrimary is the primary button, usually the left button for a
// right-handed user.
ButtonPrimary Buttons = 1 << iota
// ButtonSecondary is the secondary button, usually the right button for a
// right-handed user.
ButtonSecondary
// ButtonTertiary is the tertiary button, usually the middle button.
ButtonTertiary
)
// Push the current pass mode to the pass stack and set the pass mode.
func (p PassOp) Push(o *op.Ops) PassStack {
id, mid := ops.PushOp(&o.Internal, ops.PassStack)
data := ops.Write(&o.Internal, ops.TypePassLen)
data[0] = byte(ops.TypePass)
return PassStack{ops: &o.Internal, id: id, macroID: mid}
}
func (p PassStack) Pop() {
ops.PopOp(p.ops, ops.PassStack, p.id, p.macroID)
data := ops.Write(p.ops, ops.TypePopPassLen)
data[0] = byte(ops.TypePopPass)
}
func (op Cursor) Add(o *op.Ops) {
data := ops.Write(&o.Internal, ops.TypeCursorLen)
data[0] = byte(ops.TypeCursor)
data[1] = byte(op)
}
// Add panics if the scroll range does not contain zero.
func (op InputOp) Add(o *op.Ops) {
if op.Tag == nil {
panic("Tag must be non-nil")
}
if b := op.ScrollBounds; b.Min.X > 0 || b.Max.X < 0 || b.Min.Y > 0 || b.Max.Y < 0 {
panic(fmt.Errorf("invalid scroll range value %v", b))
}
if op.Kinds>>16 > 0 {
panic(fmt.Errorf("value in Types overflows uint16"))
}
data := ops.Write1(&o.Internal, ops.TypePointerInputLen, op.Tag)
data[0] = byte(ops.TypePointerInput)
if op.Grab {
data[1] = 1
}
bo := binary.LittleEndian
bo.PutUint16(data[2:], uint16(op.Kinds))
bo.PutUint32(data[4:], uint32(op.ScrollBounds.Min.X))
bo.PutUint32(data[8:], uint32(op.ScrollBounds.Min.Y))
bo.PutUint32(data[12:], uint32(op.ScrollBounds.Max.X))
bo.PutUint32(data[16:], uint32(op.ScrollBounds.Max.Y))
}
func (t Kind) String() string {
if t == Cancel {
return "Cancel"
}
var buf strings.Builder
for tt := Kind(1); tt > 0; tt <<= 1 {
if t&tt > 0 {
if buf.Len() > 0 {
buf.WriteByte('|')
}
buf.WriteString((t & tt).string())
}
}
return buf.String()
}
func (t Kind) string() string {
switch t {
case Press:
return "Press"
case Release:
return "Release"
case Cancel:
return "Cancel"
case Move:
return "Move"
case Drag:
return "Drag"
case Enter:
return "Enter"
case Leave:
return "Leave"
case Scroll:
return "Scroll"
default:
panic("unknown Type")
}
}
func (p Priority) String() string {
switch p {
case Shared:
return "Shared"
case Foremost:
return "Foremost"
case Grabbed:
return "Grabbed"
default:
panic("unknown priority")
}
}
func (s Source) String() string {
switch s {
case Mouse:
return "Mouse"
case Touch:
return "Touch"
default:
panic("unknown source")
}
}
// Contain reports whether the set b contains
// all of the buttons.
func (b Buttons) Contain(buttons Buttons) bool {
return b&buttons == buttons
}
func (b Buttons) String() string {
var strs []string
if b.Contain(ButtonPrimary) {
strs = append(strs, "ButtonPrimary")
}
if b.Contain(ButtonSecondary) {
strs = append(strs, "ButtonSecondary")
}
if b.Contain(ButtonTertiary) {
strs = append(strs, "ButtonTertiary")
}
return strings.Join(strs, "|")
}
func (c Cursor) String() string {
switch c {
case CursorDefault:
return "Default"
case CursorNone:
return "None"
case CursorText:
return "Text"
case CursorVerticalText:
return "VerticalText"
case CursorPointer:
return "Pointer"
case CursorCrosshair:
return "Crosshair"
case CursorAllScroll:
return "AllScroll"
case CursorColResize:
return "ColResize"
case CursorRowResize:
return "RowResize"
case CursorGrab:
return "Grab"
case CursorGrabbing:
return "Grabbing"
case CursorNotAllowed:
return "NotAllowed"
case CursorWait:
return "Wait"
case CursorProgress:
return "Progress"
case CursorNorthWestResize:
return "NorthWestResize"
case CursorNorthEastResize:
return "NorthEastResize"
case CursorSouthWestResize:
return "SouthWestResize"
case CursorSouthEastResize:
return "SouthEastResize"
case CursorNorthSouthResize:
return "NorthSouthResize"
case CursorEastWestResize:
return "EastWestResize"
case CursorWestResize:
return "WestResize"
case CursorEastResize:
return "EastResize"
case CursorNorthResize:
return "NorthResize"
case CursorSouthResize:
return "SouthResize"
case CursorNorthEastSouthWestResize:
return "NorthEastSouthWestResize"
case CursorNorthWestSouthEastResize:
return "NorthWestSouthEastResize"
default:
panic("unknown Type")
}
}
func (Event) ImplementsEvent() {}