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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'arena.dart';
import 'binding.dart';
import 'constants.dart';
import 'drag.dart';
import 'drag_details.dart';
import 'events.dart';
import 'gesture_settings.dart';
import 'recognizer.dart';
import 'velocity_tracker.dart';
/// Signature for when [MultiDragGestureRecognizer] recognizes the start of a drag gesture.
typedef GestureMultiDragStartCallback = Drag? Function(Offset position);
/// Per-pointer state for a [MultiDragGestureRecognizer].
///
/// A [MultiDragGestureRecognizer] tracks each pointer separately. The state for
/// each pointer is a subclass of [MultiDragPointerState].
abstract class MultiDragPointerState {
/// Creates per-pointer state for a [MultiDragGestureRecognizer].
///
/// The [initialPosition] argument must not be null.
MultiDragPointerState(this.initialPosition, this.kind, this.gestureSettings)
: assert(initialPosition != null),
_velocityTracker = VelocityTracker.withKind(kind);
/// Device specific gesture configuration that should be preferred over
/// framework constants.
///
/// These settings are commonly retrieved from a [MediaQuery].
final DeviceGestureSettings? gestureSettings;
/// The global coordinates of the pointer when the pointer contacted the screen.
final Offset initialPosition;
final VelocityTracker _velocityTracker;
/// The kind of pointer performing the multi-drag gesture.
///
/// Used by subclasses to determine the appropriate hit slop, for example.
final PointerDeviceKind kind;
Drag? _client;
/// The offset of the pointer from the last position that was reported to the client.
///
/// After the pointer contacts the screen, the pointer might move some
/// distance before this movement will be recognized as a drag. This field
/// accumulates that movement so that we can report it to the client after
/// the drag starts.
Offset? get pendingDelta => _pendingDelta;
Offset? _pendingDelta = Offset.zero;
Duration? _lastPendingEventTimestamp;
GestureArenaEntry? _arenaEntry;
void _setArenaEntry(GestureArenaEntry entry) {
assert(_arenaEntry == null);
assert(pendingDelta != null);
assert(_client == null);
_arenaEntry = entry;
}
/// Resolve this pointer's entry in the [GestureArenaManager] with the given disposition.
@protected
@mustCallSuper
void resolve(GestureDisposition disposition) {
_arenaEntry!.resolve(disposition);
}
void _move(PointerMoveEvent event) {
assert(_arenaEntry != null);
if (!event.synthesized)
_velocityTracker.addPosition(event.timeStamp, event.position);
if (_client != null) {
assert(pendingDelta == null);
// Call client last to avoid reentrancy.
_client!.update(DragUpdateDetails(
sourceTimeStamp: event.timeStamp,
delta: event.delta,
globalPosition: event.position,
));
} else {
assert(pendingDelta != null);
_pendingDelta = _pendingDelta! + event.delta;
_lastPendingEventTimestamp = event.timeStamp;
checkForResolutionAfterMove();
}
}
/// Override this to call resolve() if the drag should be accepted or rejected.
/// This is called when a pointer movement is received, but only if the gesture
/// has not yet been resolved.
@protected
void checkForResolutionAfterMove() { }
/// Called when the gesture was accepted.
///
/// Either immediately or at some future point before the gesture is disposed,
/// call starter(), passing it initialPosition, to start the drag.
@protected
void accepted(GestureMultiDragStartCallback starter);
/// Called when the gesture was rejected.
///
/// The [dispose] method will be called immediately following this.
@protected
@mustCallSuper
void rejected() {
assert(_arenaEntry != null);
assert(_client == null);
assert(pendingDelta != null);
_pendingDelta = null;
_lastPendingEventTimestamp = null;
_arenaEntry = null;
}
void _startDrag(Drag client) {
assert(_arenaEntry != null);
assert(_client == null);
assert(client != null);
assert(pendingDelta != null);
_client = client;
final DragUpdateDetails details = DragUpdateDetails(
sourceTimeStamp: _lastPendingEventTimestamp,
delta: pendingDelta!,
globalPosition: initialPosition,
);
_pendingDelta = null;
_lastPendingEventTimestamp = null;
// Call client last to avoid reentrancy.
_client!.update(details);
}
void _up() {
assert(_arenaEntry != null);
if (_client != null) {
assert(pendingDelta == null);
final DragEndDetails details = DragEndDetails(velocity: _velocityTracker.getVelocity());
final Drag client = _client!;
_client = null;
// Call client last to avoid reentrancy.
client.end(details);
} else {
assert(pendingDelta != null);
_pendingDelta = null;
_lastPendingEventTimestamp = null;
}
}
void _cancel() {
assert(_arenaEntry != null);
if (_client != null) {
assert(pendingDelta == null);
final Drag client = _client!;
_client = null;
// Call client last to avoid reentrancy.
client.cancel();
} else {
assert(pendingDelta != null);
_pendingDelta = null;
_lastPendingEventTimestamp = null;
}
}
/// Releases any resources used by the object.
@protected
@mustCallSuper
void dispose() {
_arenaEntry?.resolve(GestureDisposition.rejected);
_arenaEntry = null;
assert(() {
_pendingDelta = null;
return true;
}());
}
}
/// Recognizes movement on a per-pointer basis.
///
/// In contrast to [DragGestureRecognizer], [MultiDragGestureRecognizer] watches
/// each pointer separately, which means multiple drags can be recognized
/// concurrently if multiple pointers are in contact with the screen.
///
/// [MultiDragGestureRecognizer] is not intended to be used directly. Instead,
/// consider using one of its subclasses to recognize specific types for drag
/// gestures.
///
/// See also:
///
/// * [ImmediateMultiDragGestureRecognizer], the most straight-forward variant
/// of multi-pointer drag gesture recognizer.
/// * [HorizontalMultiDragGestureRecognizer], which only recognizes drags that
/// start horizontally.
/// * [VerticalMultiDragGestureRecognizer], which only recognizes drags that
/// start vertically.
/// * [DelayedMultiDragGestureRecognizer], which only recognizes drags that
/// start after a long-press gesture.
abstract class MultiDragGestureRecognizer extends GestureRecognizer {
/// Initialize the object.
///
/// {@macro flutter.gestures.GestureRecognizer.supportedDevices}
MultiDragGestureRecognizer({
required Object? debugOwner,
@Deprecated(
'Migrate to supportedDevices. '
'This feature was deprecated after v2.3.0-1.0.pre.',
)
PointerDeviceKind? kind,
Set<PointerDeviceKind>? supportedDevices,
}) : super(
debugOwner: debugOwner,
kind: kind,
supportedDevices: supportedDevices,
);
/// Called when this class recognizes the start of a drag gesture.
///
/// The remaining notifications for this drag gesture are delivered to the
/// [Drag] object returned by this callback.
GestureMultiDragStartCallback? onStart;
Map<int, MultiDragPointerState>? _pointers = <int, MultiDragPointerState>{};
@override
void addAllowedPointer(PointerDownEvent event) {
assert(_pointers != null);
assert(event.pointer != null);
assert(event.position != null);
assert(!_pointers!.containsKey(event.pointer));
final MultiDragPointerState state = createNewPointerState(event);
_pointers![event.pointer] = state;
GestureBinding.instance.pointerRouter.addRoute(event.pointer, _handleEvent);
state._setArenaEntry(GestureBinding.instance.gestureArena.add(event.pointer, this));
}
/// Subclasses should override this method to create per-pointer state
/// objects to track the pointer associated with the given event.
@protected
@factory
MultiDragPointerState createNewPointerState(PointerDownEvent event);
void _handleEvent(PointerEvent event) {
assert(_pointers != null);
assert(event.pointer != null);
assert(event.timeStamp != null);
assert(event.position != null);
assert(_pointers!.containsKey(event.pointer));
final MultiDragPointerState state = _pointers![event.pointer]!;
if (event is PointerMoveEvent) {
state._move(event);
// We might be disposed here.
} else if (event is PointerUpEvent) {
assert(event.delta == Offset.zero);
state._up();
// We might be disposed here.
_removeState(event.pointer);
} else if (event is PointerCancelEvent) {
assert(event.delta == Offset.zero);
state._cancel();
// We might be disposed here.
_removeState(event.pointer);
} else if (event is! PointerDownEvent) {
// we get the PointerDownEvent that resulted in our addPointer getting called since we
// add ourselves to the pointer router then (before the pointer router has heard of
// the event).
assert(false);
}
}
@override
void acceptGesture(int pointer) {
assert(_pointers != null);
final MultiDragPointerState? state = _pointers![pointer];
if (state == null)
return; // We might already have canceled this drag if the up comes before the accept.
state.accepted((Offset initialPosition) => _startDrag(initialPosition, pointer));
}
Drag? _startDrag(Offset initialPosition, int pointer) {
assert(_pointers != null);
final MultiDragPointerState state = _pointers![pointer]!;
assert(state != null);
assert(state._pendingDelta != null);
Drag? drag;
if (onStart != null)
drag = invokeCallback<Drag?>('onStart', () => onStart!(initialPosition));
if (drag != null) {
state._startDrag(drag);
} else {
_removeState(pointer);
}
return drag;
}
@override
void rejectGesture(int pointer) {
assert(_pointers != null);
if (_pointers!.containsKey(pointer)) {
final MultiDragPointerState state = _pointers![pointer]!;
assert(state != null);
state.rejected();
_removeState(pointer);
} // else we already preemptively forgot about it (e.g. we got an up event)
}
void _removeState(int pointer) {
if (_pointers == null) {
// We've already been disposed. It's harmless to skip removing the state
// for the given pointer because dispose() has already removed it.
return;
}
assert(_pointers!.containsKey(pointer));
GestureBinding.instance.pointerRouter.removeRoute(pointer, _handleEvent);
_pointers!.remove(pointer)!.dispose();
}
@override
void dispose() {
_pointers!.keys.toList().forEach(_removeState);
assert(_pointers!.isEmpty);
_pointers = null;
super.dispose();
}
}
class _ImmediatePointerState extends MultiDragPointerState {
_ImmediatePointerState(Offset initialPosition, PointerDeviceKind kind, DeviceGestureSettings? deviceGestureSettings) : super(initialPosition, kind, deviceGestureSettings);
@override
void checkForResolutionAfterMove() {
assert(pendingDelta != null);
if (pendingDelta!.distance > computeHitSlop(kind, gestureSettings))
resolve(GestureDisposition.accepted);
}
@override
void accepted(GestureMultiDragStartCallback starter) {
starter(initialPosition);
}
}
/// Recognizes movement both horizontally and vertically on a per-pointer basis.
///
/// In contrast to [PanGestureRecognizer], [ImmediateMultiDragGestureRecognizer]
/// watches each pointer separately, which means multiple drags can be
/// recognized concurrently if multiple pointers are in contact with the screen.
///
/// See also:
///
/// * [PanGestureRecognizer], which recognizes only one drag gesture at a time,
/// regardless of how many fingers are involved.
/// * [HorizontalMultiDragGestureRecognizer], which only recognizes drags that
/// start horizontally.
/// * [VerticalMultiDragGestureRecognizer], which only recognizes drags that
/// start vertically.
/// * [DelayedMultiDragGestureRecognizer], which only recognizes drags that
/// start after a long-press gesture.
class ImmediateMultiDragGestureRecognizer extends MultiDragGestureRecognizer {
/// Create a gesture recognizer for tracking multiple pointers at once.
///
/// {@macro flutter.gestures.GestureRecognizer.supportedDevices}
ImmediateMultiDragGestureRecognizer({
Object? debugOwner,
@Deprecated(
'Migrate to supportedDevices. '
'This feature was deprecated after v2.3.0-1.0.pre.',
)
PointerDeviceKind? kind,
Set<PointerDeviceKind>? supportedDevices,
}) : super(
debugOwner: debugOwner,
kind: kind,
supportedDevices: supportedDevices,
);
@override
MultiDragPointerState createNewPointerState(PointerDownEvent event) {
return _ImmediatePointerState(event.position, event.kind, gestureSettings);
}
@override
String get debugDescription => 'multidrag';
}
class _HorizontalPointerState extends MultiDragPointerState {
_HorizontalPointerState(Offset initialPosition, PointerDeviceKind kind, DeviceGestureSettings? deviceGestureSettings): super(initialPosition, kind, deviceGestureSettings);
@override
void checkForResolutionAfterMove() {
assert(pendingDelta != null);
if (pendingDelta!.dx.abs() > computeHitSlop(kind, gestureSettings))
resolve(GestureDisposition.accepted);
}
@override
void accepted(GestureMultiDragStartCallback starter) {
starter(initialPosition);
}
}
/// Recognizes movement in the horizontal direction on a per-pointer basis.
///
/// In contrast to [HorizontalDragGestureRecognizer],
/// [HorizontalMultiDragGestureRecognizer] watches each pointer separately,
/// which means multiple drags can be recognized concurrently if multiple
/// pointers are in contact with the screen.
///
/// See also:
///
/// * [HorizontalDragGestureRecognizer], a gesture recognizer that just
/// looks at horizontal movement.
/// * [ImmediateMultiDragGestureRecognizer], a similar recognizer, but without
/// the limitation that the drag must start horizontally.
/// * [VerticalMultiDragGestureRecognizer], which only recognizes drags that
/// start vertically.
class HorizontalMultiDragGestureRecognizer extends MultiDragGestureRecognizer {
/// Create a gesture recognizer for tracking multiple pointers at once
/// but only if they first move horizontally.
///
/// {@macro flutter.gestures.GestureRecognizer.supportedDevices}
HorizontalMultiDragGestureRecognizer({
Object? debugOwner,
@Deprecated(
'Migrate to supportedDevices. '
'This feature was deprecated after v2.3.0-1.0.pre.',
)
PointerDeviceKind? kind,
Set<PointerDeviceKind>? supportedDevices,
}) : super(
debugOwner: debugOwner,
kind: kind,
supportedDevices: supportedDevices,
);
@override
MultiDragPointerState createNewPointerState(PointerDownEvent event) {
return _HorizontalPointerState(event.position, event.kind, gestureSettings);
}
@override
String get debugDescription => 'horizontal multidrag';
}
class _VerticalPointerState extends MultiDragPointerState {
_VerticalPointerState(Offset initialPosition, PointerDeviceKind kind, DeviceGestureSettings? deviceGestureSettings): super(initialPosition, kind, deviceGestureSettings);
@override
void checkForResolutionAfterMove() {
assert(pendingDelta != null);
if (pendingDelta!.dy.abs() > computeHitSlop(kind, gestureSettings))
resolve(GestureDisposition.accepted);
}
@override
void accepted(GestureMultiDragStartCallback starter) {
starter(initialPosition);
}
}
/// Recognizes movement in the vertical direction on a per-pointer basis.
///
/// In contrast to [VerticalDragGestureRecognizer],
/// [VerticalMultiDragGestureRecognizer] watches each pointer separately,
/// which means multiple drags can be recognized concurrently if multiple
/// pointers are in contact with the screen.
///
/// See also:
///
/// * [VerticalDragGestureRecognizer], a gesture recognizer that just
/// looks at vertical movement.
/// * [ImmediateMultiDragGestureRecognizer], a similar recognizer, but without
/// the limitation that the drag must start vertically.
/// * [HorizontalMultiDragGestureRecognizer], which only recognizes drags that
/// start horizontally.
class VerticalMultiDragGestureRecognizer extends MultiDragGestureRecognizer {
/// Create a gesture recognizer for tracking multiple pointers at once
/// but only if they first move vertically.
///
/// {@macro flutter.gestures.GestureRecognizer.supportedDevices}
VerticalMultiDragGestureRecognizer({
Object? debugOwner,
@Deprecated(
'Migrate to supportedDevices. '
'This feature was deprecated after v2.3.0-1.0.pre.',
)
PointerDeviceKind? kind,
Set<PointerDeviceKind>? supportedDevices,
}) : super(
debugOwner: debugOwner,
kind: kind,
supportedDevices: supportedDevices,
);
@override
MultiDragPointerState createNewPointerState(PointerDownEvent event) {
return _VerticalPointerState(event.position, event.kind, gestureSettings);
}
@override
String get debugDescription => 'vertical multidrag';
}
class _DelayedPointerState extends MultiDragPointerState {
_DelayedPointerState(Offset initialPosition, Duration delay, PointerDeviceKind kind, DeviceGestureSettings? deviceGestureSettings)
: assert(delay != null),
super(initialPosition, kind, deviceGestureSettings) {
_timer = Timer(delay, _delayPassed);
}
Timer? _timer;
GestureMultiDragStartCallback? _starter;
void _delayPassed() {
assert(_timer != null);
assert(pendingDelta != null);
assert(pendingDelta!.distance <= computeHitSlop(kind, gestureSettings));
_timer = null;
if (_starter != null) {
_starter!(initialPosition);
_starter = null;
} else {
resolve(GestureDisposition.accepted);
}
assert(_starter == null);
}
void _ensureTimerStopped() {
_timer?.cancel();
_timer = null;
}
@override
void accepted(GestureMultiDragStartCallback starter) {
assert(_starter == null);
if (_timer == null)
starter(initialPosition);
else
_starter = starter;
}
@override
void checkForResolutionAfterMove() {
if (_timer == null) {
// If we've been accepted by the gesture arena but the pointer moves too
// much before the timer fires, we end up a state where the timer is
// stopped but we keep getting calls to this function because we never
// actually started the drag. In this case, _starter will be non-null
// because we're essentially waiting forever to start the drag.
assert(_starter != null);
return;
}
assert(pendingDelta != null);
if (pendingDelta!.distance > computeHitSlop(kind, gestureSettings)) {
resolve(GestureDisposition.rejected);
_ensureTimerStopped();
}
}
@override
void dispose() {
_ensureTimerStopped();
super.dispose();
}
}
/// Recognizes movement both horizontally and vertically on a per-pointer basis
/// after a delay.
///
/// In contrast to [ImmediateMultiDragGestureRecognizer],
/// [DelayedMultiDragGestureRecognizer] waits for a [delay] before recognizing
/// the drag. If the pointer moves more than [kTouchSlop] before the delay
/// expires, the gesture is not recognized.
///
/// In contrast to [PanGestureRecognizer], [DelayedMultiDragGestureRecognizer]
/// watches each pointer separately, which means multiple drags can be
/// recognized concurrently if multiple pointers are in contact with the screen.
///
/// See also:
///
/// * [ImmediateMultiDragGestureRecognizer], a similar recognizer but without
/// the delay.
/// * [PanGestureRecognizer], which recognizes only one drag gesture at a time,
/// regardless of how many fingers are involved.
class DelayedMultiDragGestureRecognizer extends MultiDragGestureRecognizer {
/// Creates a drag recognizer that works on a per-pointer basis after a delay.
///
/// In order for a drag to be recognized by this recognizer, the pointer must
/// remain in the same place for [delay] (up to [kTouchSlop]). The [delay]
/// defaults to [kLongPressTimeout] to match [LongPressGestureRecognizer] but
/// can be changed for specific behaviors.
///
/// {@macro flutter.gestures.GestureRecognizer.supportedDevices}
DelayedMultiDragGestureRecognizer({
this.delay = kLongPressTimeout,
Object? debugOwner,
@Deprecated(
'Migrate to supportedDevices. '
'This feature was deprecated after v2.3.0-1.0.pre.',
)
PointerDeviceKind? kind,
Set<PointerDeviceKind>? supportedDevices,
}) : assert(delay != null),
super(
debugOwner: debugOwner,
kind: kind,
supportedDevices: supportedDevices,
);
/// The amount of time the pointer must remain in the same place for the drag
/// to be recognized.
final Duration delay;
@override
MultiDragPointerState createNewPointerState(PointerDownEvent event) {
return _DelayedPointerState(event.position, delay, event.kind, gestureSettings);
}
@override
String get debugDescription => 'long multidrag';
}