[iOS] Add iOS live text input engine side support (flutter/engine#34751)

This commit is contained in:
WenJingRui
2022-07-27 15:30:05 +08:00
committed by GitHub
parent 469fb0ac1f
commit f6335c330f
4 changed files with 68 additions and 0 deletions

View File

@@ -37,6 +37,8 @@ using namespace flutter;
@implementation FlutterPlatformPlugin {
fml::WeakPtr<FlutterEngine> _engine;
// Used to detect whether this device has live text input ability or not.
UITextField* _textField;
}
- (instancetype)initWithEngine:(fml::WeakPtr<FlutterEngine>)engine {
@@ -88,6 +90,8 @@ using namespace flutter;
result(nil);
} else if ([method isEqualToString:@"Clipboard.hasStrings"]) {
result([self clipboardHasStrings]);
} else if ([method isEqualToString:@"LiveText.isLiveTextInputAvailable"]) {
result(@([self isLiveTextInputAvailable]));
} else {
result(FlutterMethodNotImplemented);
}
@@ -277,4 +281,19 @@ using namespace flutter;
return @{@"value" : @([UIPasteboard generalPasteboard].hasStrings)};
}
- (BOOL)isLiveTextInputAvailable {
return [[self textField] canPerformAction:@selector(captureTextFromCamera:) withSender:nil];
}
- (UITextField*)textField {
if (_textField == nil) {
_textField = [[UITextField alloc] init];
}
return _textField;
}
- (void)dealloc {
[_textField release];
[super dealloc];
}
@end

View File

@@ -14,6 +14,10 @@
@interface FlutterPlatformPluginTest : XCTestCase
@end
@interface FlutterPlatformPlugin ()
- (BOOL)isLiveTextInputAvailable;
@end
@implementation FlutterPlatformPluginTest
- (void)testClipboardHasCorrectStrings {
@@ -111,4 +115,24 @@
OCMVerify([navigationControllerMock popViewControllerAnimated:YES]);
}
- (void)testWhetherDeviceHasLiveTextInputInvokeCorrectly {
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
std::unique_ptr<fml::WeakPtrFactory<FlutterEngine>> _weakFactory =
std::make_unique<fml::WeakPtrFactory<FlutterEngine>>(engine);
XCTestExpectation* invokeExpectation =
[self expectationWithDescription:@"isLiveTextInputAvailableInvoke"];
FlutterPlatformPlugin* plugin =
[[FlutterPlatformPlugin alloc] initWithEngine:_weakFactory->GetWeakPtr()];
FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin);
FlutterMethodCall* methodCall =
[FlutterMethodCall methodCallWithMethodName:@"LiveText.isLiveTextInputAvailable"
arguments:nil];
FlutterResult result = ^(id result) {
OCMVerify([mockPlugin isLiveTextInputAvailable]);
[invokeExpectation fulfill];
};
[mockPlugin handleMethodCall:methodCall result:result];
[self waitForExpectationsWithTimeout:1 handler:nil];
}
@end

View File

@@ -49,6 +49,7 @@ static NSString* const kSetEditableSizeAndTransformMethod =
static NSString* const kSetMarkedTextRectMethod = @"TextInput.setMarkedTextRect";
static NSString* const kFinishAutofillContextMethod = @"TextInput.finishAutofillContext";
static NSString* const kSetSelectionRectsMethod = @"TextInput.setSelectionRects";
static NSString* const kStartLiveTextInputMethod = @"TextInput.startLiveTextInput";
#pragma mark - TextInputConfiguration Field Names
static NSString* const kSecureTextEntry = @"obscureText";
@@ -2098,6 +2099,9 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
} else if ([method isEqualToString:kSetSelectionRectsMethod]) {
[self setSelectionRects:args];
result(nil);
} else if ([method isEqualToString:kStartLiveTextInputMethod]) {
[self startLiveTextInput];
result(nil);
} else {
result(FlutterMethodNotImplemented);
}
@@ -2142,6 +2146,15 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
_activeView.selectionRects = rectsAsRect;
}
- (void)startLiveTextInput {
if (@available(iOS 15.0, *)) {
if (_activeView == nil || !_activeView.isFirstResponder) {
return;
}
[_activeView captureTextFromCamera:nil];
}
}
- (void)showTextInput {
_activeView.viewResponder = _viewResponder;
[self addToInputParentViewIfNeeded:_activeView];

View File

@@ -68,6 +68,7 @@ FLUTTER_ASSERT_ARC
- (UIView*)hostView;
- (fml::WeakPtr<FlutterTextInputPlugin>)getWeakPtr;
- (void)addToInputParentViewIfNeeded:(FlutterTextInputView*)inputView;
- (void)startLiveTextInput;
@end
@interface FlutterTextInputPluginTest : XCTestCase
@@ -165,6 +166,17 @@ FLUTTER_ASSERT_ARC
}
#pragma mark - Tests
- (void)testInvokeStartLiveTextInput {
FlutterMethodCall* methodCall =
[FlutterMethodCall methodCallWithMethodName:@"TextInput.startLiveTextInput" arguments:nil];
FlutterTextInputPlugin* mockPlugin = OCMPartialMock(textInputPlugin);
[mockPlugin handleMethodCall:methodCall
result:^(id _Nullable result){
}];
OCMVerify([mockPlugin startLiveTextInput]);
}
- (void)testNoDanglingEnginePointer {
__weak FlutterTextInputPlugin* weakFlutterTextInputPlugin;
FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];