From b473589fe5a278474b0812f54c3459a488180061 Mon Sep 17 00:00:00 2001 From: Callum Moffat Date: Thu, 25 Jan 2024 16:10:06 -0500 Subject: [PATCH] Refactor macOS trackpad gesture tracking (flutter/engine#49424) The AppKit state machine was not well understood before. Sometimes the engine would leave orphan PanZoomStart after a certain sequences including NSEventPhaseMayBegin Now track each gesture's specific phase instead of boolean true/false which doesn't really accurately represent the system state. Fixes https://github.com/flutter/flutter/issues/140730 FIxes https://github.com/flutter/flutter/issues/136622 --- .../framework/Source/FlutterViewController.mm | 71 ++++++++++++------- .../Source/FlutterViewControllerTest.mm | 6 ++ 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm index 6f61cf9578..f93c3ddcc9 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm @@ -80,20 +80,25 @@ struct MouseState { */ bool has_pending_exit = false; - /** - * Pan gesture is currently sending us events. + /* + * Whether or not a kPanZoomStart has been sent since the last kAdd/kPanZoomEnd. */ - bool pan_gesture_active = false; + bool flutter_state_is_pan_zoom_started = false; /** - * Scale gesture is currently sending us events. + * State of pan gesture. */ - bool scale_gesture_active = false; + NSEventPhase pan_gesture_phase = NSEventPhaseNone; /** - * Rotate gesture is currently sending use events. + * State of scale gesture. */ - bool rotate_gesture_active = false; + NSEventPhase scale_gesture_phase = NSEventPhaseNone; + + /** + * State of rotate gesture. + */ + NSEventPhase rotate_gesture_phase = NSEventPhaseNone; /** * Time of last scroll momentum event. @@ -108,6 +113,10 @@ struct MouseState { delta_y = 0; scale = 0; rotation = 0; + flutter_state_is_pan_zoom_started = false; + pan_gesture_phase = NSEventPhaseNone; + scale_gesture_phase = NSEventPhaseNone; + rotate_gesture_phase = NSEventPhaseNone; } /** @@ -679,32 +688,42 @@ static void CommonInit(FlutterViewController* controller, FlutterEngine* engine) // Multiple gesture recognizers could be active at once, we can't send multiple kPanZoomStart. // For example: rotation and magnification. - if (phase == kPanZoomStart) { - bool gestureAlreadyDown = _mouseState.pan_gesture_active || _mouseState.scale_gesture_active || - _mouseState.rotate_gesture_active; + if (phase == kPanZoomStart || phase == kPanZoomEnd) { if (event.type == NSEventTypeScrollWheel) { - _mouseState.pan_gesture_active = true; - // Ensure scroll inertia cancel event is not sent afterwards. - _mouseState.last_scroll_momentum_changed_time = 0; + _mouseState.pan_gesture_phase = event.phase; } else if (event.type == NSEventTypeMagnify) { - _mouseState.scale_gesture_active = true; + _mouseState.scale_gesture_phase = event.phase; } else if (event.type == NSEventTypeRotate) { - _mouseState.rotate_gesture_active = true; - } - if (gestureAlreadyDown) { - return; + _mouseState.rotate_gesture_phase = event.phase; } } - if (phase == kPanZoomEnd) { + if (phase == kPanZoomStart) { if (event.type == NSEventTypeScrollWheel) { - _mouseState.pan_gesture_active = false; - } else if (event.type == NSEventTypeMagnify) { - _mouseState.scale_gesture_active = false; - } else if (event.type == NSEventTypeRotate) { - _mouseState.rotate_gesture_active = false; + // Ensure scroll inertia cancel event is not sent afterwards. + _mouseState.last_scroll_momentum_changed_time = 0; } - if (_mouseState.pan_gesture_active || _mouseState.scale_gesture_active || - _mouseState.rotate_gesture_active) { + if (_mouseState.flutter_state_is_pan_zoom_started) { + // Already started on a previous gesture type + return; + } + _mouseState.flutter_state_is_pan_zoom_started = true; + } + if (phase == kPanZoomEnd) { + if (!_mouseState.flutter_state_is_pan_zoom_started) { + // NSEventPhaseCancelled is sometimes received at incorrect times in the state + // machine, just ignore it here if it doesn't make sense + // (we have no active gesture to cancel). + NSAssert(event.phase == NSEventPhaseCancelled, + @"Received gesture event with unexpected phase"); + return; + } + // NSEventPhase values are powers of two, we can use this to inspect merged phases. + NSEventPhase all_gestures_fields = _mouseState.pan_gesture_phase | + _mouseState.scale_gesture_phase | + _mouseState.rotate_gesture_phase; + NSEventPhase active_mask = NSEventPhaseBegan | NSEventPhaseChanged; + if ((all_gestures_fields & active_mask) != 0) { + // Even though this gesture type ended, a different type is still active. return; } } diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTest.mm index ede7c3ca87..aa0a729b89 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTest.mm @@ -995,6 +995,12 @@ TEST_F(FlutterViewControllerTest, testViewControllerIsReleased) { EXPECT_EQ(last_event.device_kind, kFlutterPointerDeviceKindTrackpad); EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone); + // Test that stray NSEventPhaseCancelled event does not crash + called = false; + [viewController rotateWithEvent:flutter::testing::MockGestureEvent(NSEventTypeRotate, + NSEventPhaseCancelled, 0, 0)]; + EXPECT_FALSE(called); + return true; }