From c2b7e18d5593eb9406a662de91e7f97cac0457b2 Mon Sep 17 00:00:00 2001 From: Callum Moffat Date: Thu, 21 Jul 2022 16:05:02 -0400 Subject: [PATCH] Scroll inertia cancel for macOS (flutter/engine#34451) --- .../framework/Source/FlutterViewController.mm | 80 ++++++++++++++----- .../Source/FlutterViewControllerTest.mm | 42 ++++++++++ 2 files changed, 103 insertions(+), 19 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 1badc52346..2962164c65 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 @@ -35,6 +35,27 @@ static constexpr int32_t kPointerPanZoomDeviceId = 1; * events that the embedding API expects. */ struct MouseState { + /** + * The currently pressed buttons, as represented in FlutterPointerEvent. + */ + int64_t buttons = 0; + + /** + * The accumulated gesture pan. + */ + CGFloat delta_x = 0; + CGFloat delta_y = 0; + + /** + * The accumulated gesture zoom scale. + */ + CGFloat scale = 0; + + /** + * The accumulated gesture rotation. + */ + CGFloat rotation = 0; + /** * Whether or not a kAdd event has been sent (or sent again since the last kRemove if tracking is * enabled). Used to determine whether to send a kAdd event before sending an incoming mouse @@ -56,41 +77,25 @@ struct MouseState { */ bool has_pending_exit = false; - /** - * The currently pressed buttons, as represented in FlutterPointerEvent. - */ - int64_t buttons = 0; - /** * Pan gesture is currently sending us events. */ bool pan_gesture_active = false; - /** - * The accumulated gesture pan. - */ - CGFloat delta_x = 0; - CGFloat delta_y = 0; - /** * Scale gesture is currently sending us events. */ bool scale_gesture_active = false; - /** - * The accumulated gesture zoom scale. - */ - CGFloat scale = 0; - /** * Rotate gesture is currently sending use events. */ bool rotate_gesture_active = false; /** - * The accumulated gesture rotation. + * System scroll inertia is currently sending us events. */ - CGFloat rotation = 0; + bool system_scroll_inertia_active = false; /** * Resets all gesture state to default values. @@ -375,6 +380,10 @@ static void CommonInit(FlutterViewController* controller) { - (void)viewDidLoad { [self configureTrackingArea]; + if (@available(macOS 10.12.2, *)) { + [self.view setAllowedTouchTypes:NSTouchTypeMaskIndirect]; + [self.view setWantsRestingTouches:YES]; + } } - (void)viewWillAppear { @@ -510,7 +519,13 @@ static void CommonInit(FlutterViewController* controller) { } else if (event.phase == NSEventPhaseNone && event.momentumPhase == NSEventPhaseNone) { [self dispatchMouseEvent:event phase:kHover]; } else { - // Skip momentum events, the framework will generate scroll momentum. + if (event.momentumPhase == NSEventPhaseBegan) { + _mouseState.system_scroll_inertia_active = true; + } else if (event.momentumPhase == NSEventPhaseEnded || + event.momentumPhase == NSEventPhaseCancelled) { + _mouseState.system_scroll_inertia_active = false; + } + // Skip momentum update events, the framework will generate scroll momentum. NSAssert(event.momentumPhase != NSEventPhaseNone, @"Received gesture event with unexpected phase"); } @@ -825,4 +840,31 @@ static void CommonInit(FlutterViewController* controller) { // Not needed, it's handled by scrollWheel. } +- (void)touchesBeganWithEvent:(NSEvent*)event { + if (@available(macOS 10.12, *)) { + NSTouch* touch = event.allTouches.anyObject; + if (touch != nil) { + if (_mouseState.system_scroll_inertia_active) { + // The trackpad has been touched and a scroll gesture is still sending inertia events. + // A scroll inertia cancel message should be sent to the framework. + NSPoint locationInView = [self.flutterView convertPoint:event.locationInWindow + fromView:nil]; + NSPoint locationInBackingCoordinates = + [self.flutterView convertPointToBacking:locationInView]; + FlutterPointerEvent flutterEvent = { + .struct_size = sizeof(flutterEvent), + .timestamp = static_cast(event.timestamp * USEC_PER_SEC), + .x = locationInBackingCoordinates.x, + .y = -locationInBackingCoordinates.y, // convertPointToBacking makes this negative. + .device = kPointerPanZoomDeviceId, + .signal_kind = kFlutterPointerSignalKindScrollInertiaCancel, + .device_kind = kFlutterPointerDeviceKindTrackpad, + }; + + [_engine sendPointerEvent:flutterEvent]; + } + } + } +} + @end 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 1d78483985..3a356eb008 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 @@ -485,6 +485,48 @@ TEST(FlutterViewControllerTest, testViewWillAppearCalledMultipleTimes) { EXPECT_EQ(last_event.device_kind, kFlutterPointerDeviceKindTrackpad); EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone); + if (@available(macOS 10.12, *)) { + // Start system momentum. + CGEventRef cgEventMomentumStart = CGEventCreateCopy(cgEventStart); + CGEventSetIntegerValueField(cgEventMomentumStart, kCGScrollWheelEventScrollPhase, 0); + CGEventSetIntegerValueField(cgEventMomentumStart, kCGScrollWheelEventMomentumPhase, + kCGMomentumScrollPhaseBegin); + + called = false; + [viewController scrollWheel:[NSEvent eventWithCGEvent:cgEventMomentumStart]]; + EXPECT_FALSE(called); + + // Mock a touch on the trackpad. + id touchMock = OCMClassMock([NSTouch class]); + NSSet* touchSet = [NSSet setWithObject:touchMock]; + id touchEventMock = OCMClassMock([NSEvent class]); + OCMStub([touchEventMock allTouches]).andReturn(touchSet); + CGPoint touchLocation = {0, 0}; + OCMStub([touchEventMock locationInWindow]).andReturn(touchLocation); + + // Scroll inertia cancel event should be issued. + called = false; + [viewController touchesBeganWithEvent:touchEventMock]; + EXPECT_TRUE(called); + EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindScrollInertiaCancel); + EXPECT_EQ(last_event.device_kind, kFlutterPointerDeviceKindTrackpad); + + // End system momentum. + CGEventRef cgEventMomentumEnd = CGEventCreateCopy(cgEventStart); + CGEventSetIntegerValueField(cgEventMomentumEnd, kCGScrollWheelEventScrollPhase, 0); + CGEventSetIntegerValueField(cgEventMomentumEnd, kCGScrollWheelEventMomentumPhase, + kCGMomentumScrollPhaseEnd); + + called = false; + [viewController scrollWheel:[NSEvent eventWithCGEvent:cgEventMomentumEnd]]; + EXPECT_FALSE(called); + + // Scroll inertia cancel event should not be issued after momentum has ended. + called = false; + [viewController touchesBeganWithEvent:touchEventMock]; + EXPECT_FALSE(called); + } + // May-begin and cancel are used while macOS determines which type of gesture to choose. CGEventRef cgEventMayBegin = CGEventCreateCopy(cgEventStart); CGEventSetIntegerValueField(cgEventMayBegin, kCGScrollWheelEventScrollPhase,