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
This commit is contained in:
Kohei Seino 2023-09-12 10:01:38 +09:00 committed by GitHub
parent 4a3ab6828a
commit 82cb74932f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -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<void>('onEnd', () => onEnd!(ScaleEndDetails(velocity: velocity, scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: _pointerCount)));
invokeCallback<void>('onEnd', () => onEnd!(ScaleEndDetails(velocity: velocity, scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: pointerCount)));
} else {
invokeCallback<void>('onEnd', () => onEnd!(ScaleEndDetails(scaleVelocity: _scaleVelocityTracker?.getVelocity().pixelsPerSecond.dx ?? -1, pointerCount: _pointerCount)));
invokeCallback<void>('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,
));
});
}

View File

@ -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;