From 9e79a79066d86ce7656c28e35cde8a450660ac3b Mon Sep 17 00:00:00 2001 From: LouiseHsu Date: Thu, 6 Jul 2023 12:38:48 -0700 Subject: [PATCH] Add Look Up to iOS selection controls (flutter/engine#43308) In native iOS, when Look Up is initiated on highlighted text, a view DDParsecCollectionViewController is presented with definitions of the the selected text if available, as well as Siri integrated suggestions. However, since the view controller is gated behind a private API, we will use [UIReferenceLibraryViewController](https://developer.apple.com/documentation/uikit/uireferencelibraryviewcontroller) instead, which is functionally identical and visually very similar. | Native | UIReferenceLibraryViewController| | ------------- | ------------- | | | | This PR is the engine portion of the changes that will allow this feature change. This PR addresses https://github.com/flutter/flutter/issues/82907 More details are available in this [design doc](flutter.dev/go/add-missing-features-to-selection-controls) --- .../framework/Source/FlutterPlatformPlugin.mm | 13 +++++++ .../Source/FlutterPlatformPluginTest.mm | 38 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm index 6d79dcee51..1785923100 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm @@ -115,6 +115,9 @@ using namespace flutter; result([self clipboardHasStrings]); } else if ([method isEqualToString:@"LiveText.isLiveTextInputAvailable"]) { result(@([self isLiveTextInputAvailable])); + } else if ([method isEqualToString:@"LookUp.invoke"]) { + [self showLookUpViewController:args]; + result(nil); } else { result(FlutterMethodNotImplemented); } @@ -307,6 +310,16 @@ using namespace flutter; return [[self textField] canPerformAction:@selector(captureTextFromCamera:) withSender:nil]; } +- (void)showLookUpViewController:(NSString*)term { + UIViewController* engineViewController = [_engine.get() viewController]; + FML_DCHECK(![engineViewController presentingViewController]); + UIReferenceLibraryViewController* referenceLibraryViewController = + [[[UIReferenceLibraryViewController alloc] initWithTerm:term] autorelease]; + [engineViewController presentViewController:referenceLibraryViewController + animated:YES + completion:nil]; +} + - (UITextField*)textField { if (_textField == nil) { _textField = [[UITextField alloc] init]; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm index b07261352a..2c02799027 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm @@ -17,10 +17,48 @@ @interface FlutterPlatformPlugin () - (BOOL)isLiveTextInputAvailable; +- (void)showLookUpViewController:(NSString*)term; +@end + +@interface UIViewController () +- (void)presentViewController:(UIViewController*)viewControllerToPresent + animated:(BOOL)flag + completion:(void (^)(void))completion; @end @implementation FlutterPlatformPluginTest +- (void)testLookUpCallInitiated { + FlutterEngine* engine = [[[FlutterEngine alloc] initWithName:@"test" project:nil] autorelease]; + [engine runWithEntrypoint:nil]; + std::unique_ptr> _weakFactory = + std::make_unique>(engine); + + XCTestExpectation* presentExpectation = + [self expectationWithDescription:@"Look Up view controller presented"]; + + FlutterViewController* engineViewController = [[FlutterViewController alloc] initWithEngine:engine + nibName:nil + bundle:nil]; + FlutterViewController* mockEngineViewController = OCMPartialMock(engineViewController); + + FlutterPlatformPlugin* plugin = + [[[FlutterPlatformPlugin alloc] initWithEngine:_weakFactory->GetWeakPtr()] autorelease]; + FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin); + + FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"LookUp.invoke" + arguments:@"Test"]; + FlutterResult result = ^(id result) { + OCMVerify([mockEngineViewController + presentViewController:[OCMArg isKindOfClass:[UIReferenceLibraryViewController class]] + animated:YES + completion:nil]); + [presentExpectation fulfill]; + }; + [mockPlugin handleMethodCall:methodCall result:result]; + [self waitForExpectationsWithTimeout:2 handler:nil]; +} + - (void)testClipboardHasCorrectStrings { [UIPasteboard generalPasteboard].string = nil; FlutterEngine* engine = [[[FlutterEngine alloc] initWithName:@"test" project:nil] autorelease];