Skip to content

Commit 79251a7

Browse files
author
Renzo Olivares
committed
Revert "Revert "add deadlineTimer to fix conflict with ForcePressGestureRecognizer""
This reverts commit 0a008f0.
1 parent a7722c3 commit 79251a7

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');
@@ -325,6 +354,24 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer with _Con
325354
_giveUpPointer(pointer);
326355
}
327356

357+
void _didExceedDeadlineWithEvent(PointerDownEvent event) {
358+
print('did exceed deadline with event $event');
359+
_checkTapDown(event);
360+
_didExceedDeadline();
361+
}
362+
363+
void _didExceedDeadline() {
364+
print('did exeeed deadline');
365+
// _checkTapDown(event);
366+
}
367+
368+
void _stopDeadlineTimer() {
369+
if (_deadlineTimer != null) {
370+
_deadlineTimer!.cancel();
371+
_deadlineTimer = null;
372+
}
373+
}
374+
328375
void _checkTapDown(PointerDownEvent event) {
329376
_initialButtons = event.buttons;
330377
_state = _DragState.possible;
@@ -450,6 +497,7 @@ class TapAndDragGestureRecognizer extends OneSequenceGestureRecognizer with _Con
450497

451498
@override
452499
void dispose() {
500+
_stopDeadlineTimer();
453501
consecutiveTapTimeout();
454502
super.dispose();
455503
}

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)