Mac trackpad gestures (flutter/engine#31593)

* Mac trackpad gestures

* Address feedback and add test

* Address feedback

* Punctuation

* Follow member naming scheme
This commit is contained in:
Callum Moffat
2022-04-29 15:44:23 -04:00
committed by GitHub
parent 6859738b93
commit 86baf448e2
2 changed files with 265 additions and 10 deletions

View File

@@ -35,6 +35,11 @@ static NSString* const EnhancedUserInterfaceNotification =
@"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification";
static NSString* const EnhancedUserInterfaceKey = @"AXEnhancedUserInterface";
// Use different device ID for mouse and pan/zoom events, since we can't differentiate the actual
// device (mouse v.s. trackpad).
static constexpr int32_t kMousePointerDeviceId = 0;
static constexpr int32_t kPointerPanZoomDeviceId = 1;
/**
* State tracking for mouse events, to adapt between the events coming from the system and the
* events that the embedding API expects.
@@ -66,6 +71,47 @@ struct MouseState {
*/
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.
*/
CGFloat rotation = 0;
/**
* Resets all gesture state to default values.
*/
void GestureReset() {
delta_x = 0;
delta_y = 0;
scale = 0;
rotation = 0;
}
/**
* Resets all state to default values.
*/
@@ -74,6 +120,7 @@ struct MouseState {
flutter_state_is_down = false;
has_pending_exit = false;
buttons = 0;
GestureReset();
}
};
@@ -178,6 +225,11 @@ NSData* currentKeyboardLayoutData() {
*/
- (void)dispatchMouseEvent:(nonnull NSEvent*)event;
/**
* Calls dispatchMouseEvent:phase: with a phase determined by event.phase.
*/
- (void)dispatchGestureEvent:(nonnull NSEvent*)event;
/**
* Converts |event| to a FlutterPointerEvent with the given phase, and sends it to the engine.
*/
@@ -530,6 +582,22 @@ static void CommonInit(FlutterViewController* controller) {
[self dispatchMouseEvent:event phase:phase];
}
- (void)dispatchGestureEvent:(nonnull NSEvent*)event {
if (event.phase == NSEventPhaseBegan || event.phase == NSEventPhaseMayBegin) {
[self dispatchMouseEvent:event phase:kPanZoomStart];
} else if (event.phase == NSEventPhaseChanged) {
[self dispatchMouseEvent:event phase:kPanZoomUpdate];
} else if (event.phase == NSEventPhaseEnded || event.phase == NSEventPhaseCancelled) {
[self dispatchMouseEvent:event phase:kPanZoomEnd];
} else if (event.phase == NSEventPhaseNone && event.momentumPhase == NSEventPhaseNone) {
[self dispatchMouseEvent:event phase:kHover];
} else {
// Skip momentum events, the framework will generate scroll momentum.
NSAssert(event.momentumPhase != NSEventPhaseNone,
@"Received gesture event with unexpected phase");
}
}
- (void)dispatchMouseEvent:(NSEvent*)event phase:(FlutterPointerPhase)phase {
NSAssert(self.viewLoaded, @"View must be loaded before it handles the mouse event");
// There are edge cases where the system will deliver enter out of order relative to other
@@ -539,6 +607,36 @@ static void CommonInit(FlutterViewController* controller) {
return;
}
// 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 (event.type == NSEventTypeScrollWheel) {
_mouseState.pan_gesture_active = true;
} else if (event.type == NSEventTypeMagnify) {
_mouseState.scale_gesture_active = true;
} else if (event.type == NSEventTypeRotate) {
_mouseState.rotate_gesture_active = true;
}
if (gestureAlreadyDown) {
return;
}
}
if (phase == kPanZoomEnd) {
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;
}
if (_mouseState.pan_gesture_active || _mouseState.scale_gesture_active ||
_mouseState.rotate_gesture_active) {
return;
}
}
// If a pointer added event hasn't been sent, synthesize one using this event for the basic
// information.
if (!_mouseState.flutter_state_is_added && phase != kAdd) {
@@ -557,27 +655,51 @@ static void CommonInit(FlutterViewController* controller) {
NSPoint locationInView = [self.flutterView convertPoint:event.locationInWindow fromView:nil];
NSPoint locationInBackingCoordinates = [self.flutterView convertPointToBacking:locationInView];
int32_t device = kMousePointerDeviceId;
FlutterPointerDeviceKind deviceKind = kFlutterPointerDeviceKindMouse;
if (phase == kPanZoomStart || phase == kPanZoomUpdate || phase == kPanZoomEnd) {
device = kPointerPanZoomDeviceId;
deviceKind = kFlutterPointerDeviceKindTrackpad;
}
FlutterPointerEvent flutterEvent = {
.struct_size = sizeof(flutterEvent),
.phase = phase,
.timestamp = static_cast<size_t>(event.timestamp * USEC_PER_SEC),
.x = locationInBackingCoordinates.x,
.y = -locationInBackingCoordinates.y, // convertPointToBacking makes this negative.
.device_kind = kFlutterPointerDeviceKindMouse,
.device = device,
.device_kind = deviceKind,
// If a click triggered a synthesized kAdd, don't pass the buttons in that event.
.buttons = phase == kAdd ? 0 : _mouseState.buttons,
};
if (event.type == NSEventTypeScrollWheel) {
if (phase == kPanZoomUpdate) {
if (event.type == NSEventTypeScrollWheel) {
_mouseState.delta_x += event.scrollingDeltaX * self.flutterView.layer.contentsScale;
_mouseState.delta_y += event.scrollingDeltaY * self.flutterView.layer.contentsScale;
} else if (event.type == NSEventTypeMagnify) {
_mouseState.scale += event.magnification;
} else if (event.type == NSEventTypeRotate) {
_mouseState.rotation += event.rotation * (M_PI / 180.0);
}
flutterEvent.pan_x = _mouseState.delta_x;
flutterEvent.pan_y = _mouseState.delta_y;
// Scale value needs to be normalized to range 0->infinity.
flutterEvent.scale = pow(2.0, _mouseState.scale);
flutterEvent.rotation = _mouseState.rotation;
} else if (phase == kPanZoomEnd) {
_mouseState.GestureReset();
} else if (phase != kPanZoomStart && event.type == NSEventTypeScrollWheel) {
flutterEvent.signal_kind = kFlutterPointerSignalKindScroll;
double pixelsPerLine = 1.0;
if (!event.hasPreciseScrollingDeltas) {
CGEventSourceRef source = CGEventCreateSourceFromEvent(event.CGEvent);
pixelsPerLine = CGEventSourceGetPixelsPerLine(source);
if (source) {
CFRelease(source);
}
// The scrollingDelta needs to be multiplied by the line height.
// CGEventSourceGetPixelsPerLine() will return 10, which will result in
// scrolling that is noticeably slower than in other applications.
// Using 40.0 as the multiplier to match Chromium.
// See https://source.chromium.org/chromium/chromium/src/+/main:ui/events/cocoa/events_mac.mm
pixelsPerLine = 40.0;
}
double scaleFactor = self.flutterView.layer.contentsScale;
flutterEvent.scroll_delta_x = -event.scrollingDeltaX * pixelsPerLine * scaleFactor;
@@ -881,9 +1003,19 @@ static void CommonInit(FlutterViewController* controller) {
}
- (void)scrollWheel:(NSEvent*)event {
// TODO: Add gesture-based (trackpad) scroll support once it's supported by the engine rather
// than always using kHover.
[self dispatchMouseEvent:event phase:kHover];
[self dispatchGestureEvent:event];
}
- (void)magnifyWithEvent:(NSEvent*)event {
[self dispatchGestureEvent:event];
}
- (void)rotateWithEvent:(NSEvent*)event {
[self dispatchGestureEvent:event];
}
- (void)swipeWithEvent:(NSEvent*)event {
// Not needed, it's handled by scrollWheel.
}
@end

View File

@@ -11,6 +11,7 @@
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h"
#import "flutter/testing/testing.h"
@@ -21,6 +22,7 @@
- (bool)testFlagsChangedEventsArePropagatedIfNotHandled;
- (bool)testPerformKeyEquivalentSynthesizesKeyUp;
- (bool)testKeyboardIsRestartedOnEngineRestart;
- (bool)testTrackpadGesturesAreSentToFramework;
+ (void)respondFalseForSendEvent:(const FlutterKeyEvent&)event
callback:(nullable FlutterKeyEventCallback)callback
@@ -176,6 +178,10 @@ TEST(FlutterViewControllerTest, TestKeyboardIsRestartedOnEngineRestart) {
ASSERT_TRUE([[FlutterViewControllerTestObjC alloc] testKeyboardIsRestartedOnEngineRestart]);
}
TEST(FlutterViewControllerTest, TestTrackpadGesturesAreSentToFramework) {
ASSERT_TRUE([[FlutterViewControllerTestObjC alloc] testTrackpadGesturesAreSentToFramework]);
}
} // namespace flutter::testing
@implementation FlutterViewControllerTestObjC
@@ -523,4 +529,121 @@ TEST(FlutterViewControllerTest, TestKeyboardIsRestartedOnEngineRestart) {
}
}
- (bool)testTrackpadGesturesAreSentToFramework {
id engineMock = OCMClassMock([FlutterEngine class]);
// Need to return a real renderer to allow view controller to load.
id renderer_ = [[FlutterMetalRenderer alloc] initWithFlutterEngine:engineMock];
OCMStub([engineMock renderer]).andReturn(renderer_);
__block bool called = false;
__block FlutterPointerEvent last_event;
OCMStub([[engineMock ignoringNonObjectArgs] sendPointerEvent:FlutterPointerEvent{}])
.andDo((^(NSInvocation* invocation) {
FlutterPointerEvent* event;
[invocation getArgument:&event atIndex:2];
called = true;
last_event = *event;
}));
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
nibName:@""
bundle:nil];
[viewController loadView];
// Start gesture.
CGEventRef cgEventStart = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 1, 0);
CGEventSetType(cgEventStart, kCGEventScrollWheel);
CGEventSetIntegerValueField(cgEventStart, kCGScrollWheelEventScrollPhase, kCGScrollPhaseBegan);
CGEventSetIntegerValueField(cgEventStart, kCGScrollWheelEventIsContinuous, 1);
called = false;
[viewController scrollWheel:[NSEvent eventWithCGEvent:cgEventStart]];
EXPECT_TRUE(called);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
EXPECT_EQ(last_event.phase, kPanZoomStart);
EXPECT_EQ(last_event.device_kind, kFlutterPointerDeviceKindTrackpad);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
// Update gesture.
CGEventRef cgEventUpdate = CGEventCreateCopy(cgEventStart);
CGEventSetIntegerValueField(cgEventUpdate, kCGScrollWheelEventScrollPhase, kCGScrollPhaseChanged);
CGEventSetIntegerValueField(cgEventUpdate, kCGScrollWheelEventDeltaAxis2, 1); // pan_x
CGEventSetIntegerValueField(cgEventUpdate, kCGScrollWheelEventDeltaAxis1, 2); // pan_y
called = false;
[viewController scrollWheel:[NSEvent eventWithCGEvent:cgEventUpdate]];
EXPECT_TRUE(called);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
EXPECT_EQ(last_event.phase, kPanZoomUpdate);
EXPECT_EQ(last_event.device_kind, kFlutterPointerDeviceKindTrackpad);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
EXPECT_EQ(last_event.pan_x, 8 * viewController.flutterView.layer.contentsScale);
EXPECT_EQ(last_event.pan_y, 16 * viewController.flutterView.layer.contentsScale);
// Make sure the pan values accumulate.
called = false;
[viewController scrollWheel:[NSEvent eventWithCGEvent:cgEventUpdate]];
EXPECT_TRUE(called);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
EXPECT_EQ(last_event.phase, kPanZoomUpdate);
EXPECT_EQ(last_event.device_kind, kFlutterPointerDeviceKindTrackpad);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
EXPECT_EQ(last_event.pan_x, 16 * viewController.flutterView.layer.contentsScale);
EXPECT_EQ(last_event.pan_y, 32 * viewController.flutterView.layer.contentsScale);
// End gesture.
CGEventRef cgEventEnd = CGEventCreateCopy(cgEventStart);
CGEventSetIntegerValueField(cgEventEnd, kCGScrollWheelEventScrollPhase, kCGScrollPhaseEnded);
called = false;
[viewController scrollWheel:[NSEvent eventWithCGEvent:cgEventEnd]];
EXPECT_TRUE(called);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
EXPECT_EQ(last_event.phase, kPanZoomEnd);
EXPECT_EQ(last_event.device_kind, kFlutterPointerDeviceKindTrackpad);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
// May-begin and cancel are used while macOS determines which type of gesture to choose.
CGEventRef cgEventMayBegin = CGEventCreateCopy(cgEventStart);
CGEventSetIntegerValueField(cgEventMayBegin, kCGScrollWheelEventScrollPhase,
kCGScrollPhaseMayBegin);
called = false;
[viewController scrollWheel:[NSEvent eventWithCGEvent:cgEventMayBegin]];
EXPECT_TRUE(called);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
EXPECT_EQ(last_event.phase, kPanZoomStart);
EXPECT_EQ(last_event.device_kind, kFlutterPointerDeviceKindTrackpad);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
// Cancel gesture.
CGEventRef cgEventCancel = CGEventCreateCopy(cgEventStart);
CGEventSetIntegerValueField(cgEventCancel, kCGScrollWheelEventScrollPhase,
kCGScrollPhaseCancelled);
called = false;
[viewController scrollWheel:[NSEvent eventWithCGEvent:cgEventCancel]];
EXPECT_TRUE(called);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
EXPECT_EQ(last_event.phase, kPanZoomEnd);
EXPECT_EQ(last_event.device_kind, kFlutterPointerDeviceKindTrackpad);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindNone);
// A discrete scroll event should use the PointerSignal system.
CGEventRef cgEventDiscrete = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 1, 0);
CGEventSetType(cgEventDiscrete, kCGEventScrollWheel);
CGEventSetIntegerValueField(cgEventDiscrete, kCGScrollWheelEventIsContinuous, 0);
CGEventSetIntegerValueField(cgEventDiscrete, kCGScrollWheelEventDeltaAxis2, 1); // scroll_delta_x
CGEventSetIntegerValueField(cgEventDiscrete, kCGScrollWheelEventDeltaAxis1, 2); // scroll_delta_y
called = false;
[viewController scrollWheel:[NSEvent eventWithCGEvent:cgEventDiscrete]];
EXPECT_TRUE(called);
EXPECT_EQ(last_event.signal_kind, kFlutterPointerSignalKindScroll);
// pixelsPerLine is 40.0 and direction is reversed.
EXPECT_EQ(last_event.scroll_delta_x, -40 * viewController.flutterView.layer.contentsScale);
EXPECT_EQ(last_event.scroll_delta_y, -80 * viewController.flutterView.layer.contentsScale);
return true;
}
@end