Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[iOS] Send connectionClosed message when resignFirstResponder to ensure framework focus state is correct. #40703

Merged
merged 6 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion shell/platform/darwin/ios/framework/Source/FlutterEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,13 @@ - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
arguments:@[ @(client) ]];
}

- (void)flutterTextInputViewDidResignFirstResponder:(FlutterTextInputView*)textInputView {
- (void)flutterTextInputViewDidResignFirstResponder:(FlutterTextInputView*)textInputView
client:(int)client {
// When flutter text input view resign first responder, send a message to
// framework to ensure the focus state is correct. This is useful when close
// keyboard from platform side.
[_textInputChannel.get() invokeMethod:@"TextInputClient.unfocus" arguments:@[ @(client) ]];

// Platform view's first responder detection logic:
//
// All text input widgets (e.g. EditableText) are backed by a dummy UITextInput view
Expand Down
18 changes: 18 additions & 0 deletions shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h"

FLUTTER_ASSERT_ARC

@interface FlutterEngine ()
- (void)flutterTextInputViewDidResignFirstResponder:(FlutterTextInputView*)textInputView
client:(int)client;
@end

@interface FlutterEngineTest : XCTestCase
@end

Expand Down Expand Up @@ -313,4 +319,16 @@ - (void)testCanEnableDisableEmbedderAPIThroughInfoPlist {
}
}

- (void)testEngineFlutterTextInputViewDidResignFirstResponderWillUnfocusTextInputClient {
id mockBinaryMessenger = OCMClassMock([FlutterBinaryMessengerRelay class]);
FlutterEngine* engine = [[FlutterEngine alloc] init];
[engine setBinaryMessenger:mockBinaryMessenger];
[engine runWithEntrypoint:FlutterDefaultDartEntrypoint initialRoute:@"test"];
[engine flutterTextInputViewDidResignFirstResponder:nil client:1];
FlutterMethodCall* methodCall =
[FlutterMethodCall methodCallWithMethodName:@"TextInputClient.unfocus" arguments:@[ @(1) ]];
NSData* encodedMethodCall = [[FlutterJSONMethodCodec sharedInstance] encodeMethodCall:methodCall];
OCMVerify([mockBinaryMessenger sendOnChannel:@"flutter/textinput" message:encodedMethodCall]);
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ typedef NS_ENUM(NSInteger, FlutterFloatingCursorDragState) {
insertTextPlaceholderWithSize:(CGSize)size
withClient:(int)client;
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView removeTextPlaceholder:(int)client;
- (void)flutterTextInputViewDidResignFirstResponder:(FlutterTextInputView*)textInputView;
- (void)flutterTextInputViewDidResignFirstResponder:(FlutterTextInputView*)textInputView
client:(int)client;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,8 @@ - (BOOL)canBecomeFirstResponder {
- (BOOL)resignFirstResponder {
BOOL success = [super resignFirstResponder];
if (success) {
[self.textInputDelegate flutterTextInputViewDidResignFirstResponder:self];
[self.textInputDelegate flutterTextInputViewDidResignFirstResponder:self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Looks like this one will be called when either the Flutter framework or UIKit tells the input view to give up its first responder status. It seems redundant for the former since there's no need to inform the Flutter framework when it is the one that initiated the focus change.

Copy link
Contributor Author

@luckysmg luckysmg Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. When framework close the keyboard first, this will send a msg to framework but will be handled return by

https://github.com/flutter/flutter/blob/659ba386f6d045690f9783c79fe5d320fb7847a1/packages/flutter/lib/src/services/text_input.dart#L1788

And only when platform starts to unfocus, the _currentConnection!._client.connectionClosed(); will be called.

Currently, engine side doens't have a good simple way to know the original focus/unfocus signal is from framework or from platform, this maybe can be done by set some var like BOOL sendFromFramework to judge, but I think that will be overkill........(Maybe I missed something that can judge this)

So I think this cost can be acceptable? ^_^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense the old connection is already torn down by the framework so messages associated with the old client id will be thrown away.

client:_textInputClient];
}
return success;
}
Expand Down