From 6010385a81d75c7a22a54b2a84e5b20e8c35b48a Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Fri, 5 May 2017 17:40:52 -0700 Subject: [PATCH] Default return key behaviour to 'done' on iOS (flutter/engine#3655) This change brings iOS return key behaviour in line with current behaviour on Android. * Implements TextInputClient.performAction for iOS Adds FlutterTextInputAction * enum Defaults return key to 'Done' Ends editing and hides the keyboard if * 'Done' is pressed Future work is planned to support configurable return/action key behaviour. See: #9573, #9210, #8028. --- .../ios/framework/Source/FlutterTextInputDelegate.h | 5 +++++ .../ios/framework/Source/FlutterTextInputPlugin.mm | 12 +++++++++++- .../ios/framework/Source/FlutterViewController.mm | 11 +++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h index 3187848991..2ed33436a2 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h @@ -7,9 +7,14 @@ #import +typedef NS_ENUM(NSInteger, FlutterTextInputAction) { + FlutterTextInputActionDone, +}; + @protocol FlutterTextInputDelegate - (void)updateEditingClient:(int)client withState:(NSDictionary*)state; +- (void)performAction:(FlutterTextInputAction)action withClient:(int)client; @end 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 5624173d4e..9e26b3a3f5 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 @@ -141,7 +141,7 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) { _enablesReturnKeyAutomatically = NO; _keyboardAppearance = UIKeyboardAppearanceDefault; _keyboardType = UIKeyboardTypeDefault; - _returnKeyType = UIReturnKeyDefault; + _returnKeyType = UIReturnKeyDone; _secureTextEntry = NO; } @@ -244,6 +244,16 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) { [self updateEditingState]; } +- (BOOL)shouldChangeTextInRange:(UITextRange*)range replacementText:(NSString*)text { + if (self.returnKeyType == UIReturnKeyDone && [text isEqualToString:@"\n"]) { + [self resignFirstResponder]; + [self removeFromSuperview]; + [_textInputDelegate performAction:FlutterTextInputActionDone withClient:_textInputClient]; + return NO; + } + return YES; +} + - (void)setMarkedText:(NSString*)markedText selectedRange:(NSRange)markedSelectedRange { NSRange selectedRange = _selectedTextRange.range; NSRange markedTextRange = ((FlutterTextRange*)self.markedTextRange).range; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 50abff05cf..6af60c885d 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -415,6 +415,17 @@ static inline PointerChangeMapperPhase PointerChangePhaseFromUITouchPhase(UITouc arguments:@[ @(client), state ]]; } +- (void)performAction:(FlutterTextInputAction)action withClient:(int)client { + NSString* actionString; + switch (action) { + case FlutterTextInputActionDone: + actionString = @"TextInputAction.done"; + break; + } + [_textInputChannel.get() invokeMethod:@"TextInputClient.performAction" + arguments:@[ @(client), actionString ]]; +} + #pragma mark - Orientation updates - (void)onOrientationPreferencesUpdated:(NSNotification*)notification {