Debian Codegraph @

shiny/gesture/gesture.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package gesture provides gesture events such as long presses and drags.
6 // These are higher level than underlying mouse and touch events.
7 package gesture
8
9 import (
10 "fmt"
11 "time"
12
13 "golang.org/x/exp/shiny/screen"
14 "golang.org/x/mobile/event/mouse"
15 )
16
17 // TODO: handle touch events, not just mouse events.
18 //
19 // TODO: multi-button / multi-touch gestures such as pinch, rotate and tilt?
20
21 const (
22 // TODO: use a resolution-independent unit such as DIPs or Millimetres?
23 dragThreshold = 10 // Pixels.
24
25 doublePressThreshold = 300 * time.Millisecond
26 longPressThreshold = 500 * time.Millisecond
27 )
28
29 // Type describes the type of a touch event.
30 type Type uint8
31
32 const (
33 // TypeStart and TypeEnd are the start and end of a gesture. A gesture
34 // spans multiple events.
35 TypeStart Type = 0
36 TypeEnd Type = 1
37
38 // TypeIsXxx is when the gesture is recognized as a long press, double
39 // press or drag. For example, a mouse button press won't generate a
40 // TypeIsLongPress immediately, but if a threshold duration passes without
41 // the corresponding mouse button release, a TypeIsLongPress event is sent.
42 //
43 // Once a TypeIsXxx event is sent, the corresponding Event.Xxx bool field
44 // is set for this and subsequent events. For example, a TypeTap event by
45 // itself doesn't say whether or not it is a single tap or the first tap of
46 // a double tap. If the app needs to distinguish these two sorts of taps,
47 // it can wait until a TypeEnd or TypeIsDoublePress event is seen. If a
48 // TypeEnd is seen before TypeIsDoublePress, or equivalently, if the
49 // TypeEnd event's DoublePress field is false, the gesture is a single tap.
50 //
51 // These attributes aren't exclusive. A long press drag is perfectly valid.
52 //
53 // The uncommon "double press" instead of "double tap" terminology is
54 // because, in this package, taps are associated with button releases, not
55 // button presses. Note also that "double" really means "at least two".
56 TypeIsLongPress Type = 10
57 TypeIsDoublePress Type = 11
58 TypeIsDrag Type = 12
59
60 // TypeTap and TypeDrag are tap and drag events.
61 //
62 // For 'flinging' drags, to simulate inertia, look to the Velocity field of
63 // the TypeEnd event.
64 //
65 // TODO: implement velocity.
66 TypeTap Type = 20
67 TypeDrag Type = 21
68
69 // All internal types are >= typeInternal.
70 typeInternal Type = 100
71
72 // The typeXxxSchedule and typeXxxResolve constants are used for the two
73 // step process for sending an event after a timeout, in a separate
74 // goroutine. There are two steps so that the spawned goroutine is
75 // guaranteed to execute only after any other EventDeque.SendFirst calls
76 // are made for the one underlying mouse or touch event.
77
78 typeDoublePressSchedule Type = 100
79 typeDoublePressResolve Type = 101
80
81 typeLongPressSchedule Type = 110
82 typeLongPressResolve Type = 111
83 )
84
85 func (t Type) String() string {
86 switch t {
87 case TypeStart:
88 return "Start"
89 case TypeEnd:
90 return "End"
91 case TypeIsLongPress:
92 return "IsLongPress"
93 case TypeIsDoublePress:
94 return "IsDoublePress"
95 case TypeIsDrag:
96 return "IsDrag"
97 case TypeTap:
98 return "Tap"
99 case TypeDrag:
100 return "Drag"
101 default:
102 return fmt.Sprintf("gesture.Type(%d)", t)
103 }
104 }
105
106 // Point is a mouse or touch location, in pixels.
107 type Point struct {
108 X, Y float32
109 }
110
111 // Event is a gesture event.
112 type Event struct {
113 // Type is the gesture type.
114 Type Type
115
116 // Drag, LongPress and DoublePress are set when the gesture is recognized as
117 // a drag, etc.
118 //
119 // Note that these status fields can be lost during a gesture's events over
120 // time: LongPress can be set for the first press of a double press, but
121 // unset on the second press.
122 Drag bool
123 LongPress bool
124 DoublePress bool
125
126 // InitialPos is the initial position of the button press or touch that
127 // started this gesture.
128 InitialPos Point
129
130 // CurrentPos is the current position of the button or touch event.
131 CurrentPos Point
132
133 // TODO: a "Velocity Point" field. See
134 // - frameworks/native/libs/input/VelocityTracker.cpp in AOSP, or
135 // - https://chromium.googlesource.com/chromium/src/+/master/ui/events/gesture_detection/velocity_tracker.cc in Chromium,
136 // for some velocity tracking implementations.
137
138 // Time is the event's time.
139 Time time.Time
140
141 // TODO: include the mouse Button and key Modifiers?
142 }
143
144 type internalEvent struct {
145 eventFilter *EventFilter
146
147 typ Type
148 x, y float32
149
150 // pressCounter is the EventFilter.pressCounter value at the time this
151 // internal event was scheduled to be delivered after a timeout. It detects
152 // whether there have been other button presses and releases during that
153 // timeout, and hence whether this internalEvent is obsolete.
154 pressCounter uint32
155 }
156
157 // EventFilter generates gesture events from lower level mouse and touch
158 // events.
159 type EventFilter struct {
160 EventDeque screen.EventDeque
161
162 inProgress bool
163 drag bool
164 longPress bool
165 doublePress bool
166
167 // initialPos is the initial position of the button press or touch that
168 // started this gesture.
169 initialPos Point
170
171 // pressButton is the initial button that started this gesture. If
172 // button.None, no gesture is in progress.
173 pressButton mouse.Button
174
175 // pressCounter is incremented on every button press and release.
176 pressCounter uint32
177 }
178
179 func (f *EventFilter) sendFirst(t Type, x, y float32, now time.Time) {
180 if t >= typeInternal {
181 f.EventDeque.SendFirst(internalEvent{
182 eventFilter: f,
183 typ: t,
184 x: x,
185 y: y,
186 pressCounter: f.pressCounter,
187 })
188 return
189 }
190 f.EventDeque.SendFirst(Event{
191 Type: t,
192 Drag: f.drag,
193 LongPress: f.longPress,
194 DoublePress: f.doublePress,
195 InitialPos: f.initialPos,
196 CurrentPos: Point{
197 X: x,
198 Y: y,
199 },
200 // TODO: Velocity.
201 Time: now,
202 })
203 }
204
205 func (f *EventFilter) sendAfter(e internalEvent, sleep time.Duration) {
206 time.Sleep(sleep)
207 f.EventDeque.SendFirst(e)
208 }
209
210 func (f *EventFilter) end(x, y float32, now time.Time) {
211 f.sendFirst(TypeEnd, x, y, now)
212 f.inProgress = false
213 f.drag = false
214 f.longPress = false
215 f.doublePress = false
216 f.initialPos = Point{}
217 f.pressButton = mouse.ButtonNone
218 }
219
220 // Filter filters the event. It can return e, a different event, or nil to
221 // consume the event. It can also trigger side effects such as pushing new
222 // events onto its EventDeque.
223 func (f *EventFilter) Filter(e interface{}) interface{} {
224 switch e := e.(type) {
225 case internalEvent:
226 if e.eventFilter != f {
227 break
228 }
229
230 now := time.Now()
231
232 switch e.typ {
233 case typeDoublePressSchedule:
234 e.typ = typeDoublePressResolve
235 go f.sendAfter(e, doublePressThreshold)
236
237 case typeDoublePressResolve:
238 if e.pressCounter == f.pressCounter {
239 // It's a single press only.
240 f.end(e.x, e.y, now)
241 }
242
243 case typeLongPressSchedule:
244 e.typ = typeLongPressResolve
245 go f.sendAfter(e, longPressThreshold)
246
247 case typeLongPressResolve:
248 if e.pressCounter == f.pressCounter && !f.drag {
249 f.longPress = true
250 f.sendFirst(TypeIsLongPress, e.x, e.y, now)
251 }
252 }
253 return nil
254
255 case mouse.Event:
256 now := time.Now()
257
258 switch e.Direction {
259 case mouse.DirNone:
260 if f.pressButton == mouse.ButtonNone {
261 break
262 }
263 startDrag := false
264 if !f.drag &&
265 (abs(e.X-f.initialPos.X) > dragThreshold || abs(e.Y-f.initialPos.Y) > dragThreshold) {
266 f.drag = true
267 startDrag = true
268 }
269 if f.drag {
270 f.sendFirst(TypeDrag, e.X, e.Y, now)
271 }
272 if startDrag {
273 f.sendFirst(TypeIsDrag, e.X, e.Y, now)
274 }
275
276 case mouse.DirPress:
277 if f.pressButton != mouse.ButtonNone {
278 break
279 }
280
281 oldInProgress := f.inProgress
282 oldDoublePress := f.doublePress
283
284 f.drag = false
285 f.longPress = false
286 f.doublePress = f.inProgress
287 f.initialPos = Point{e.X, e.Y}
288 f.pressButton = e.Button
289 f.pressCounter++
290
291 f.inProgress = true
292
293 f.sendFirst(typeLongPressSchedule, e.X, e.Y, now)
294 if !oldDoublePress && f.doublePress {
295 f.sendFirst(TypeIsDoublePress, e.X, e.Y, now)
296 }
297 if !oldInProgress {
298 f.sendFirst(TypeStart, e.X, e.Y, now)
299 }
300
301 case mouse.DirRelease:
302 if f.pressButton != e.Button {
303 break
304 }
305 f.pressButton = mouse.ButtonNone
306 f.pressCounter++
307
308 if f.drag {
309 f.end(e.X, e.Y, now)
310 break
311 }
312 f.sendFirst(typeDoublePressSchedule, e.X, e.Y, now)
313 f.sendFirst(TypeTap, e.X, e.Y, now)
314 }
315 }
316 return e
317 }
318
319 func abs(x float32) float32 {
320 if x < 0 {
321 return -x
322 } else if x == 0 {
323 return 0 // Handle floating point negative zero.
324 }
325 return x
326 }