Skip to content

Commit e05d0dd

Browse files
authored
ScaleGestureRecognizer pointerCount=2 for trackpad gestures (#140745)
Now trackpad gestures will count as pointerCount=2 instead of 1. It makes it easier for people who want to have different behaviour for single-finger drag vs two-finger pan/zoom. Also fixed up `scale_test.dart` to verify `pointerCount` in more places. Related: flutter/flutter#13102 Fixes flutter/flutter#140730
1 parent b0c5fc9 commit e05d0dd

File tree

2 files changed

+105
-5
lines changed

2 files changed

+105
-5
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ class ScaleUpdateDetails {
230230
/// The number of pointers being tracked by the gesture recognizer.
231231
///
232232
/// Typically this is the number of fingers being used to pan the widget using the gesture
233-
/// recognizer.
233+
/// recognizer. Due to platform limitations, trackpad gestures count as two fingers
234+
/// even if more than two fingers are used.
234235
final int pointerCount;
235236

236237
/// Recorded timestamp of the source pointer event that triggered the scale
@@ -408,7 +409,10 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer {
408409
/// Typically this is the number of fingers being used to pan the widget using the gesture
409410
/// recognizer.
410411
int get pointerCount {
411-
return _pointerPanZooms.length + _pointerQueue.length;
412+
// PointerPanZoom protocol doesn't contain the exact number of pointers
413+
// used on the trackpad, as it isn't exposed by all platforms. However, it
414+
// will always be at least two.
415+
return (2 * _pointerPanZooms.length) + _pointerQueue.length;
412416
}
413417

414418
late Offset _initialFocalPoint;
@@ -583,7 +587,7 @@ class ScaleGestureRecognizer extends OneSequenceGestureRecognizer {
583587
for (final _PointerPanZoomData p in _pointerPanZooms.values) {
584588
focalPoint += p.focalPoint;
585589
}
586-
_currentFocalPoint = pointerCount > 0 ? focalPoint / pointerCount.toDouble() : Offset.zero;
590+
_currentFocalPoint = focalPoint / math.max(1, _pointerLocations.length + _pointerPanZooms.length).toDouble();
587591

588592
if (previousFocalPoint == null) {
589593
_localFocalPoint = PointerEvent.transformPosition(

0 commit comments

Comments
 (0)