From fe8267a0492694647bf8023a588ac26eced8bbd5 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Tue, 29 Jun 2021 09:56:02 -0700 Subject: [PATCH] [iOS TextInput] Disables system keyboard for TextInputType.none (flutter/engine#26979) --- .../Source/FlutterTextInputPlugin.mm | 27 +++++++++++++++++++ .../Source/FlutterTextInputPluginTest.m | 14 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index 038a40d378..b8f9cfd803 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -55,6 +55,14 @@ static NSString* const kAutocorrectionType = @"autocorrect"; #pragma mark - Static Functions +// "TextInputType.none" is a made-up input type that's typically +// used when there's an in-app virtual keyboard. If +// "TextInputType.none" is specified, disable the system +// keyboard. +static BOOL shouldShowSystemKeyboard(NSDictionary* type) { + NSString* inputType = type[@"name"]; + return ![inputType isEqualToString:@"TextInputType.none"]; +} static UIKeyboardType ToUIKeyboardType(NSDictionary* type) { NSString* inputType = type[@"name"]; if ([inputType isEqualToString:@"TextInputType.address"]) @@ -516,7 +524,12 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { int _textInputClient; const char* _selectionAffinity; FlutterTextRange* _selectedTextRange; + UIInputViewController* _inputViewController; CGRect _cachedFirstRect; + // Whether to show the system keyboard when this view + // becomes the first responder. Typically set to false + // when the app shows its own in-flutter keyboard. + bool _isSystemKeyboardEnabled; bool _isFloatingCursorActive; // The view has reached end of life, and is no longer // allowed to access its textInputDelegate. @@ -579,6 +592,8 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { NSDictionary* autofill = configuration[kAutofillProperties]; self.secureTextEntry = [configuration[kSecureTextEntry] boolValue]; + + _isSystemKeyboardEnabled = shouldShowSystemKeyboard(inputType); self.keyboardType = ToUIKeyboardType(inputType); self.returnKeyType = ToUIReturnKeyType(configuration[kInputAction]); self.autocapitalizationType = ToUITextAutoCapitalizationType(configuration); @@ -625,6 +640,17 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { return _textContentType; } +- (UIInputViewController*)inputViewController { + if (_isSystemKeyboardEnabled) { + return nil; + } + + if (!_inputViewController) { + _inputViewController = [UIInputViewController new]; + } + return _inputViewController; +} + - (id)textInputDelegate { return _decommissioned ? nil : _textInputDelegate; } @@ -649,6 +675,7 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) { [_selectedTextRange release]; [_tokenizer release]; [_autofillId release]; + [_inputViewController release]; [super dealloc]; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.m b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.m index d52359ecb3..def757b915 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.m +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.m @@ -194,6 +194,20 @@ FLUTTER_ASSERT_ARC XCTAssertEqual(inputView.keyboardType, UIKeyboardTypeURL); } +- (void)testSettingKeyboardTypeNoneDisablesSystemKeyboard { + NSDictionary* config = self.mutableTemplateCopy; + [config setValue:@{@"name" : @"TextInputType.none"} forKey:@"inputType"]; + [self setClientId:123 configuration:config]; + + // Verify the view's inputViewController is not nil; + XCTAssertNotNil(textInputPlugin.activeView.inputViewController); + + [config setValue:@{@"name" : @"TextInputType.url"} forKey:@"inputType"]; + [self setClientId:124 configuration:config]; + XCTAssertNotNil(textInputPlugin.activeView); + XCTAssertNil(textInputPlugin.activeView.inputViewController); +} + - (void)testAutocorrectionPromptRectAppears { FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithFrame:CGRectZero]; inputView.textInputDelegate = engine;