From 82cb74932f7e1bf7965aaa5d4f4562e7feb08050 Mon Sep 17 00:00:00 2001 From: Kohei Seino Date: Tue, 12 Sep 2023 10:01:38 +0900 Subject: [PATCH] ScaleGestureRecognizer: make pointerCount public (#127310) make `pointCount` in `ScaleGestureRecognizer` public to handle pointer event depending on the number of pointers. https://github.com/flutter/flutter/issues/127309 --- packages/flutter/lib/src/gestures/scale.dart | 22 +++++++++++-------- .../flutter/test/gestures/scale_test.dart | 2 ++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/gestures/scale.dart b/packages/flutter/lib/src/gestures/scale.dart index a67283c7e5d..11b53cf52d4 100644 --- a/packages/flutter/lib/src/gestures/scale.dart +++ b/packages/flutter/lib/src/gestures/scale.dart @@ -393,6 +393,14 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { /// {@endtemplate} Offset trackpadScrollToScaleFactor; + /// The number of pointers being tracked by the gesture recognizer. + /// + /// Typically this is the number of fingers being used to pan the widget using the gesture + /// recognizer. + int get pointerCount { + return _pointerPanZooms.length + _pointerQueue.length; + } + late Offset _initialFocalPoint; Offset? _currentFocalPoint; late double _initialSpan; @@ -443,10 +451,6 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { return scale; } - int get _pointerCount { - return _pointerPanZooms.length + _pointerQueue.length; - } - double _computeRotationFactor() { double factor = 0.0; if (_initialLine != null && _currentLine != null) { @@ -566,7 +570,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { for (final _PointerPanZoomData p in _pointerPanZooms.values) { focalPoint += p.focalPoint; } - _currentFocalPoint = _pointerCount > 0 ? focalPoint / _pointerCount.toDouble() : Offset.zero; + _currentFocalPoint = pointerCount > 0 ? focalPoint / pointerCount.toDouble() : Offset.zero; if (previousFocalPoint == null) { _localFocalPoint = PointerEvent.transformPosition( @@ -662,9 +666,9 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { if (pixelsPerSecond.distanceSquared > kMaxFlingVelocity * kMaxFlingVelocity) { velocity = Velocity(pixelsPerSecond: (pixelsPerSecond / pixelsPerSecond.distance) * kMaxFlingVelocity); } - invokeCallback('onEnd', () => onEnd!(ScaleEndDetails(velocity: velocity, scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: _pointerCount))); + invokeCallback('onEnd', () => onEnd!(ScaleEndDetails(velocity: velocity, scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: pointerCount))); } else { - invokeCallback('onEnd', () => onEnd!(ScaleEndDetails(scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: _pointerCount))); + invokeCallback('onEnd', () => onEnd!(ScaleEndDetails(scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: pointerCount))); } } _state = _ScaleState.accepted; @@ -706,7 +710,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { focalPoint: _currentFocalPoint!, localFocalPoint: _localFocalPoint, rotation: _computeRotationFactor(), - pointerCount: _pointerCount, + pointerCount: pointerCount, focalPointDelta: _delta, )); }); @@ -721,7 +725,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer { onStart!(ScaleStartDetails( focalPoint: _currentFocalPoint!, localFocalPoint: _localFocalPoint, - pointerCount: _pointerCount, + pointerCount: pointerCount, )); }); } diff --git a/packages/flutter/test/gestures/scale_test.dart b/packages/flutter/test/gestures/scale_test.dart index a844c54cdf7..6892dcccc52 100644 --- a/packages/flutter/test/gestures/scale_test.dart +++ b/packages/flutter/test/gestures/scale_test.dart @@ -79,6 +79,7 @@ void main() { updatedDelta = null; expect(didEndScale, isFalse); expect(didTap, isFalse); + expect(scale.pointerCount, 1); // Two-finger scaling final TestPointer pointer2 = TestPointer(2); @@ -87,6 +88,7 @@ void main() { tap.addPointer(down2); tester.closeArena(2); tester.route(down2); + expect(scale.pointerCount, 2); expect(didEndScale, isTrue); didEndScale = false;