Skip to content

Commit 3b29ddf

Browse files
author
Renzo Olivares
committed
add deadlineTimer to fix conflict with ForcePressGestureRecognizer
1 parent 68e0c81 commit 3b29ddf

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

packages/flutter/lib/src/gestures/selection_recognizers.dart

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,20 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer with _Con
108108
///
109109
/// {@macro flutter.gestures.GestureRecognizer.supportedDevices}
110110
TapAndDragGestureRecognizer({
111+
this.deadline,
111112
super.debugOwner,
112113
this.dragStartBehavior = DragStartBehavior.start,
113114
super.kind,
114115
super.supportedDevices,
115116
});
116117

118+
/// If non-null, the recognizer will call [didExceedDeadline] after this
119+
/// amount of time has elapsed since starting to track the primary pointer.
120+
///
121+
/// The [didExceedDeadline] will not be called if the primary pointer is
122+
/// accepted, rejected, or all pointers are up or canceled before [deadline].
123+
final Duration? deadline;
124+
117125
/// {@macro flutter.gestures.monodrag.DragGestureRecognizer.dragStartBehavior}
118126
DragStartBehavior dragStartBehavior;
119127

@@ -154,7 +162,10 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer with _Con
154162

155163
// Tap related state
156164
PointerUpEvent? _up;
157-
165+
PointerDownEvent? _down;
166+
Timer? _deadlineTimer;
167+
int? _primaryPointer;
168+
158169
// Drag related state
159170
OffsetPair? _correctedPosition;
160171
late OffsetPair _initialPosition;
@@ -212,6 +223,12 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer with _Con
212223
void addAllowedPointer(PointerDownEvent event) {
213224
print('addAllowedPointer $event ${event.pointer}');
214225
super.addAllowedPointer(event);
226+
if (_state == _DragState.ready) {
227+
_primaryPointer = event.pointer;
228+
if (deadline != null) {
229+
_deadlineTimer = Timer(deadline!, () => _didExceedDeadlineWithEvent(event));
230+
}
231+
}
215232
}
216233

217234
@override
@@ -223,6 +240,15 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer with _Con
223240
@override
224241
void acceptGesture(int pointer) {
225242
assert(!_acceptedActivePointers.contains(pointer));
243+
if (pointer == _primaryPointer) {
244+
print('is primary pointer');
245+
// _checkTapDown(event);
246+
if (_down != null) {
247+
_checkTapDown(_down!);
248+
// _checkTapUp(_up!);
249+
}
250+
_stopDeadlineTimer();
251+
}
226252
_acceptedActivePointers.add(pointer);
227253
print('accept gesture $pointer');
228254
}
@@ -251,7 +277,9 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer with _Con
251277
_checkEnd();
252278
break;
253279
}
280+
_stopDeadlineTimer();
254281
_up = null;
282+
_down = null;
255283
_initialButtons = null;
256284
_state = _DragState.ready;
257285
_consecutiveTapCountWhileDragging = null;
@@ -264,7 +292,8 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer with _Con
264292
print('handle PointerDownEvent $event');
265293
if (_state == _DragState.ready) {
266294
_globalDistanceMoved = 0.0;
267-
_checkTapDown(event);
295+
_down = event;
296+
// _checkTapDown(event);
268297
}
269298
} else if (event is PointerMoveEvent) {
270299
print('handle PointerMoveEvent $event');
@@ -321,6 +350,24 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer with _Con
321350
_giveUpPointer(pointer);
322351
}
323352

353+
void _didExceedDeadlineWithEvent(PointerDownEvent event) {
354+
print('did exceed deadline with event $event');
355+
_checkTapDown(event);
356+
_didExceedDeadline();
357+
}
358+
359+
void _didExceedDeadline() {
360+
print('did exeeed deadline');
361+
// _checkTapDown(event);
362+
}
363+
364+
void _stopDeadlineTimer() {
365+
if (_deadlineTimer != null) {
366+
_deadlineTimer!.cancel();
367+
_deadlineTimer = null;
368+
}
369+
}
370+
324371
void _checkTapDown(PointerDownEvent event) {
325372
_initialButtons = event.buttons;
326373
_state = _DragState.possible;
@@ -446,6 +493,7 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer with _Con
446493

447494
@override
448495
void dispose() {
496+
_stopDeadlineTimer();
449497
consecutiveTapTimeout();
450498
super.dispose();
451499
}

packages/flutter/lib/src/widgets/text_selection.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,7 @@ class _TextSelectionGestureDetectorState extends State<TextSelectionGestureDetec
25222522
widget.onDragSelectionUpdate != null ||
25232523
widget.onDragSelectionEnd != null) {
25242524
gestures[TapAndDragGestureRecognizer] = GestureRecognizerFactoryWithHandlers<TapAndDragGestureRecognizer>(
2525-
() => TapAndDragGestureRecognizer(debugOwner: this, supportedDevices: <PointerDeviceKind>{ PointerDeviceKind.mouse, PointerDeviceKind.touch }),
2525+
() => TapAndDragGestureRecognizer(deadline: kPressTimeout, debugOwner: this, supportedDevices: <PointerDeviceKind>{ PointerDeviceKind.mouse, PointerDeviceKind.touch }),
25262526
(TapAndDragGestureRecognizer instance) {
25272527
instance
25282528
// Text selection should start from the position of the first pointer

0 commit comments

Comments
 (0)