Skip to content

Commit 21eb449

Browse files
authored
[ios][platform_view] Fix Platform view gesture recognizer with iPad pencil getting stuck (flutter-team-archive/engine#55724)
## Summary I came across our the "gesture recognizer delegate" implementation and it is quite odd (see below). After fixing it, the problem is resolved. However, it's hard to reason about how it's related to iPad pencil, since it's internal logic that we don't know (see my research below). ## Gesture recognizer delegate ### Existing odd implementation - shouldBeRequiredToFailByGestureRecognizer: `otherGestureRecognizer != self` is always YES because the delegate set to self, hence `gestureRecognizer` must be self, hence `otherGestureRecognizer` must not be self. - shouldRequireFailureOfGestureRecognizer: `otherGestureRecognizer == self` is always NO, for the same reason described above. ### new implementation: After digging into various PRs, the idea seems to be that we want to have a precedence of "Forwarding recognizer > Delaying recognizer > Other recognizers in platform view". - shouldBeRequiredToFailByGestureRecognizer: `return otherGestureRecognizer != _forwardingRecognizer` means Delaying recognizer needs to be higher precedence than all non-Forwarding recognizer. (aka "Delaying recognizer > Other recognizers in platform view") - shouldRequireFailureOfGestureRecognizer: `return otherGestureRecognizer == _forwardingRecognizer` means Delaying recognizer needs to have lower precedence than forwarding recognizer. (aka "Forwarding recognizer > Delaying recognizer"). ## Some research This is a tricky one since pencil and finger triggers exactly the same callbacks. It turns out that when pencil is involved after finger interaction, the platform view's "forwarding" gesture recognizer is stuck at failed state. This seems to be an iOS bug, because according to [the API doc](https://developer.apple.com/documentation/uikit/uigesturerecognizerstate/uigesturerecognizerstatefailed?language=objc), it should be reset back to "possible" state: > No action message is sent and the gesture recognizer is reset to [UIGestureRecognizerStatePossible](https://developer.apple.com/documentation/uikit/uigesturerecognizerstate/uigesturerecognizerstatepossible?language=objc). However, when iPad pencil is involved, the state is not reset. I tried to KVO the state property, and wasn't able to capture the change. This means the state change very likely happened internally within the recognizer via the backing ivar of the state property. *List which issues are fixed by this PR. You must list at least one issue.* Fixes flutter#136244 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 3f0d57e commit 21eb449

2 files changed

Lines changed: 77 additions & 4 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,79 @@ - (void)testFlutterPlatformViewTouchesCancelledEventAreForcedToBeCancelled {
27822782
flutterPlatformViewsController->Reset();
27832783
}
27842784

2785+
- (void)testFlutterPlatformViewForwardingAndDelayingRecognizerFailureCondition {
2786+
flutter::FlutterPlatformViewsTestMockPlatformViewDelegate mock_delegate;
2787+
2788+
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
2789+
/*platform=*/GetDefaultTaskRunner(),
2790+
/*raster=*/GetDefaultTaskRunner(),
2791+
/*ui=*/GetDefaultTaskRunner(),
2792+
/*io=*/GetDefaultTaskRunner());
2793+
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
2794+
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
2795+
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
2796+
/*delegate=*/mock_delegate,
2797+
/*rendering_api=*/mock_delegate.settings_.enable_impeller
2798+
? flutter::IOSRenderingAPI::kMetal
2799+
: flutter::IOSRenderingAPI::kSoftware,
2800+
/*platform_views_controller=*/flutterPlatformViewsController,
2801+
/*task_runners=*/runners,
2802+
/*worker_task_runner=*/nil,
2803+
/*is_gpu_disabled_jsync_switch=*/std::make_shared<fml::SyncSwitch>());
2804+
2805+
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
2806+
[[FlutterPlatformViewsTestMockFlutterPlatformFactory alloc] init];
2807+
flutterPlatformViewsController->RegisterViewFactory(
2808+
factory, @"MockFlutterPlatformView",
2809+
FlutterPlatformViewGestureRecognizersBlockingPolicyEager);
2810+
FlutterResult result = ^(id result) {
2811+
};
2812+
flutterPlatformViewsController->OnMethodCall(
2813+
[FlutterMethodCall
2814+
methodCallWithMethodName:@"create"
2815+
arguments:@{@"id" : @2, @"viewType" : @"MockFlutterPlatformView"}],
2816+
result);
2817+
2818+
XCTAssertNotNil(gMockPlatformView);
2819+
2820+
// Find touch inteceptor view
2821+
UIView* touchInteceptorView = gMockPlatformView;
2822+
while (touchInteceptorView != nil &&
2823+
![touchInteceptorView isKindOfClass:[FlutterTouchInterceptingView class]]) {
2824+
touchInteceptorView = touchInteceptorView.superview;
2825+
}
2826+
XCTAssertNotNil(touchInteceptorView);
2827+
2828+
// Find ForwardGestureRecognizer
2829+
UIGestureRecognizer* forwardingGestureRecognizer = nil;
2830+
UIGestureRecognizer* delayingGestureRecognizer = nil;
2831+
for (UIGestureRecognizer* gestureRecognizer in touchInteceptorView.gestureRecognizers) {
2832+
if ([gestureRecognizer isKindOfClass:NSClassFromString(@"ForwardingGestureRecognizer")]) {
2833+
forwardingGestureRecognizer = gestureRecognizer;
2834+
}
2835+
if ([gestureRecognizer isKindOfClass:NSClassFromString(@"FlutterDelayingGestureRecognizer")]) {
2836+
delayingGestureRecognizer = gestureRecognizer;
2837+
}
2838+
}
2839+
UIGestureRecognizer* otherGestureRecognizer = OCMClassMock([UIGestureRecognizer class]);
2840+
2841+
id flutterViewContoller = OCMClassMock([FlutterViewController class]);
2842+
flutterPlatformViewsController->SetFlutterViewController(flutterViewContoller);
2843+
2844+
XCTAssertFalse([delayingGestureRecognizer.delegate
2845+
gestureRecognizer:delayingGestureRecognizer
2846+
shouldBeRequiredToFailByGestureRecognizer:forwardingGestureRecognizer]);
2847+
XCTAssertTrue([delayingGestureRecognizer.delegate gestureRecognizer:delayingGestureRecognizer
2848+
shouldBeRequiredToFailByGestureRecognizer:otherGestureRecognizer]);
2849+
2850+
XCTAssertTrue([delayingGestureRecognizer.delegate gestureRecognizer:delayingGestureRecognizer
2851+
shouldRequireFailureOfGestureRecognizer:forwardingGestureRecognizer]);
2852+
XCTAssertFalse([delayingGestureRecognizer.delegate gestureRecognizer:delayingGestureRecognizer
2853+
shouldRequireFailureOfGestureRecognizer:otherGestureRecognizer]);
2854+
2855+
flutterPlatformViewsController->Reset();
2856+
}
2857+
27852858
- (void)testFlutterPlatformViewControllerSubmitFrameWithoutFlutterViewNotCrashing {
27862859
flutter::FlutterPlatformViewsTestMockPlatformViewDelegate mock_delegate;
27872860

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.mm‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,14 @@ - (instancetype)initWithTarget:(id)target
654654

655655
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
656656
shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
657-
// The forwarding gesture recognizer should always get all touch events, so it should not be
658-
// required to fail by any other gesture recognizer.
659-
return otherGestureRecognizer != _forwardingRecognizer && otherGestureRecognizer != self;
657+
// The forwarding gesture recognizer should always get all touch events, so it should not
658+
// require other gesture recognizer to fail.
659+
return otherGestureRecognizer != _forwardingRecognizer;
660660
}
661661

662662
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
663663
shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
664-
return otherGestureRecognizer == self;
664+
return otherGestureRecognizer == _forwardingRecognizer;
665665
}
666666

667667
- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {

0 commit comments

Comments
 (0)