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|
| ------------- | ------------- |
| <img src="https://github.com/flutter/engine/assets/36148254/763004f0-970f-4d8a-9a9a-133401ef0c1b" width="360" />| <img src="https://github.com/flutter/engine/assets/36148254/b20fe03f-6c9d-4a4a-98dc-8bcd840406db" width="360" />|

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)
This commit is contained in:
LouiseHsu
2023-07-06 12:38:48 -07:00
committed by GitHub
parent 1f859f035a
commit 9e79a79066
2 changed files with 51 additions and 0 deletions

View File

@@ -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];

View File

@@ -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<fml::WeakPtrFactory<FlutterEngine>> _weakFactory =
std::make_unique<fml::WeakPtrFactory<FlutterEngine>>(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];