Skip to content

Commit

Permalink
Pointer bidding
Browse files Browse the repository at this point in the history
  • Loading branch information
moffatman committed Aug 13, 2022
1 parent 2794a64 commit b896d91
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions lib/widgets/weak_gesture_recognizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abstract class WeakDragGestureRecognizer extends OneSequenceGestureRecognizer {

Offset _getDeltaForDetails(Offset delta);
double? _getPrimaryValueFromOffset(Offset value);
bool _hasSufficientGlobalDistanceToAccept(PointerEvent event, double? deviceTouchSlop);
double _calculateAcceptFactor(PointerEvent event, double? deviceTouchSlop);

final Map<int, VelocityTracker> _velocityTrackers = <int, VelocityTracker>{};
final Map<int, Duration> _pointerDownTimes = <int, Duration>{};
Expand Down Expand Up @@ -184,9 +184,7 @@ abstract class WeakDragGestureRecognizer extends OneSequenceGestureRecognizer {
untransformedDelta: movedLocally,
untransformedEndPosition: event.localPosition,
).distance * (_getPrimaryValueFromOffset(movedLocally) ?? 1).sign;
if (_hasSufficientGlobalDistanceToAccept(event, gestureSettings?.touchSlop)) {
resolve(GestureDisposition.accepted);
}
resolve(GestureDisposition.accepted, bid: _calculateAcceptFactor(event, gestureSettings?.touchSlop));
}
}
if (event is PointerPanZoomUpdateEvent) {
Expand All @@ -210,9 +208,7 @@ abstract class WeakDragGestureRecognizer extends OneSequenceGestureRecognizer {
untransformedDelta: movedLocally,
untransformedEndPosition: event.localPosition + event.pan
).distance * (_getPrimaryValueFromOffset(movedLocally) ?? 1).sign;
if (_hasSufficientGlobalDistanceToAccept(event, gestureSettings?.touchSlop)) {
resolve(GestureDisposition.accepted);
}
resolve(GestureDisposition.accepted, bid: _calculateAcceptFactor(event, gestureSettings?.touchSlop));
}
}
if (event is PointerUpEvent || event is PointerCancelEvent || event is PointerPanZoomEndEvent) {
Expand All @@ -224,6 +220,7 @@ abstract class WeakDragGestureRecognizer extends OneSequenceGestureRecognizer {

@override
void acceptGesture(int pointer) {
super.acceptGesture(pointer);
assert(!_acceptedActivePointers.contains(pointer));
_acceptedActivePointers.add(pointer);
if (_state != _DragState.accepted) {
Expand Down Expand Up @@ -268,6 +265,7 @@ abstract class WeakDragGestureRecognizer extends OneSequenceGestureRecognizer {

@override
void rejectGesture(int pointer) {
super.rejectGesture(pointer);
_giveUpPointer(pointer);
}

Expand Down Expand Up @@ -420,11 +418,14 @@ class WeakVerticalDragGestureRecognizer extends WeakDragGestureRecognizer {
}

@override
bool _hasSufficientGlobalDistanceToAccept(PointerEvent event, double? deviceTouchSlop) {
return (sign != null && _globalDistanceMoved.sign == sign!.sign) && _globalDistanceMoved.abs() > (weakness * computeHitSlop(event.kind, gestureSettings)) || (
(_globalDistanceMoved.abs() > computeHitSlop(event.kind, gestureSettings)) &&
_hasSufficientDurationToAccept(event)
);
double _calculateAcceptFactor(PointerEvent event, double? deviceTouchSlop) {
if (_globalDistanceMoved.sign != sign?.sign) {
return 0;
}
if (_hasSufficientDurationToAccept(event)) {
return _globalDistanceMoved.abs() / computeHitSlop(event.kind, gestureSettings);
}
return _globalDistanceMoved.abs() / (weakness * computeHitSlop(event.kind, gestureSettings));
}

@override
Expand Down Expand Up @@ -456,11 +457,14 @@ class WeakHorizontalDragGestureRecognizer extends WeakDragGestureRecognizer {
}

@override
bool _hasSufficientGlobalDistanceToAccept(PointerEvent event, double? deviceTouchSlop) {
return (sign != null && _globalDistanceMoved.sign == sign!.sign) && _globalDistanceMoved.abs() > (weakness * computeHitSlop(event.kind, gestureSettings)) || (
(_globalDistanceMoved.abs() > computeHitSlop(event.kind, gestureSettings)) &&
_hasSufficientDurationToAccept(event)
);
double _calculateAcceptFactor(PointerEvent event, double? deviceTouchSlop) {
if (_globalDistanceMoved.sign != sign?.sign) {
return 0;
}
if (_hasSufficientDurationToAccept(event)) {
return _globalDistanceMoved.abs() / computeHitSlop(event.kind, gestureSettings);
}
return _globalDistanceMoved.abs() / (weakness * computeHitSlop(event.kind, gestureSettings));
}

@override
Expand Down Expand Up @@ -495,11 +499,14 @@ class WeakPanGestureRecognizer extends WeakDragGestureRecognizer {
}

@override
bool _hasSufficientGlobalDistanceToAccept(PointerEvent event, double? deviceTouchSlop) {
return allowedToAccept && (sign != null && _globalDistanceMoved.sign == sign!.sign) && _globalDistanceMoved.abs() > (weakness * computePanSlop(event.kind, gestureSettings)) || (
(_globalDistanceMoved.abs() > computePanSlop(event.kind, gestureSettings)) &&
_hasSufficientDurationToAccept(event)
);
double _calculateAcceptFactor(PointerEvent event, double? deviceTouchSlop) {
if (!allowedToAccept || _globalDistanceMoved.sign != sign?.sign) {
return 0;
}
if (_hasSufficientDurationToAccept(event)) {
return _globalDistanceMoved.abs() / computeHitSlop(event.kind, gestureSettings);
}
return _globalDistanceMoved.abs() / (weakness * computeHitSlop(event.kind, gestureSettings));
}

@override
Expand Down

0 comments on commit b896d91

Please sign in to comment.