@@ -808,6 +808,8 @@ @interface FlutterTextInputView ()
808808@property (nonatomic , strong ) NSArray <NSDictionary*>* editMenuItems;
809809
810810- (void )setEditableTransform : (NSArray *)matrix ;
811+ - (BOOL )becomeFirstResponderFromFramework ;
812+ - (BOOL )resignFirstResponderFromFramework ;
811813@end
812814
813815@implementation FlutterTextInputView {
@@ -825,6 +827,12 @@ @implementation FlutterTextInputView {
825827 bool _isFloatingCursorActive;
826828 CGPoint _floatingCursorOffset;
827829 bool _enableInteractiveSelection;
830+ BOOL _isResigningFirstResponderFromFramework;
831+ // A non-framework resign arms a one-shot notification for this exact view. The responder API
832+ // does not reveal which object, if any, takes focus next, so a handoff to a native input can arm
833+ // it too. Framework responder calls and client updates cancel it before UIKit can restore stale
834+ // input state.
835+ BOOL _shouldNotifyFrameworkOnFirstResponderRestore;
828836 UITextInteraction* _textInteraction API_AVAILABLE (ios (13.0 ));
829837}
830838
@@ -1180,6 +1188,7 @@ - (BOOL)respondsToSelector:(SEL)selector {
11801188}
11811189
11821190- (void )setTextInputClient : (int )client {
1191+ _shouldNotifyFrameworkOnFirstResponderRestore = NO ;
11831192 _textInputClient = client;
11841193 _hasPlaceholder = NO ;
11851194}
@@ -1348,17 +1357,64 @@ - (BOOL)canBecomeFirstResponder {
13481357 return _textInputClient != 0 ;
13491358}
13501359
1360+ - (BOOL )becomeFirstResponder {
1361+ BOOL wasFirstResponder = self.isFirstResponder ;
1362+ BOOL success = [super becomeFirstResponder ];
1363+ if (success && !wasFirstResponder && self.isFirstResponder &&
1364+ _shouldNotifyFrameworkOnFirstResponderRestore) {
1365+ _shouldNotifyFrameworkOnFirstResponderRestore = NO ;
1366+ int client = _textInputClient;
1367+ __weak FlutterTextInputView* weakSelf = self;
1368+ FlutterResult handleResult = ^(id result) {
1369+ [weakSelf handleFirstResponderRestoreResult: result client: client];
1370+ };
1371+ // Existing framework client and FocusNode guards decide whether to accept the restored focus.
1372+ [self .textInputDelegate flutterTextInputView: self
1373+ didRestoreFirstResponderWithTextInputClient: client
1374+ result: handleResult];
1375+ }
1376+ return success;
1377+ }
1378+
1379+ - (void )handleFirstResponderRestoreResult : (id )result client : (int )client {
1380+ if ([result isKindOfClass: [NSNumber class ]] && [result boolValue ]) {
1381+ return ;
1382+ }
1383+ // The framework declined the restored focus or failed to answer. Don't leave the keyboard up
1384+ // without a framework client behind it, unless the view has been given another client since the
1385+ // restore was reported.
1386+ if (client == _textInputClient && self.isFirstResponder ) {
1387+ [self resignFirstResponderFromFramework ];
1388+ }
1389+ }
1390+
13511391- (BOOL )resignFirstResponder {
1392+ BOOL wasFirstResponder = self.isFirstResponder ;
13521393 BOOL success = [super resignFirstResponder ];
1353- if (success) {
1354- if (!_preventCursorDismissWhenResignFirstResponder ) {
1355- [ self .textInputDelegate flutterTextInputView: self
1356- didResignFirstResponderWithTextInputClient: _textInputClient] ;
1394+ if (success && !_preventCursorDismissWhenResignFirstResponder ) {
1395+ if (wasFirstResponder ) {
1396+ _shouldNotifyFrameworkOnFirstResponderRestore =
1397+ !_isResigningFirstResponderFromFramework && _textInputClient != 0 ;
13571398 }
1399+ [self .textInputDelegate flutterTextInputView: self
1400+ didResignFirstResponderWithTextInputClient: _textInputClient];
13581401 }
13591402 return success;
13601403}
13611404
1405+ - (BOOL )becomeFirstResponderFromFramework {
1406+ _shouldNotifyFrameworkOnFirstResponderRestore = NO ;
1407+ return [self becomeFirstResponder ];
1408+ }
1409+
1410+ - (BOOL )resignFirstResponderFromFramework {
1411+ _shouldNotifyFrameworkOnFirstResponderRestore = NO ;
1412+ _isResigningFirstResponderFromFramework = YES ;
1413+ BOOL success = [self resignFirstResponder ];
1414+ _isResigningFirstResponderFromFramework = NO ;
1415+ return success;
1416+ }
1417+
13621418- (BOOL )canPerformAction : (SEL )action withSender : (id )sender {
13631419 if (action == @selector (paste: )) {
13641420 // Forbid pasting images, memojis, or other non-string content.
@@ -2869,7 +2925,7 @@ - (void)startLiveTextInput {
28692925- (void )showTextInput {
28702926 _activeView.viewResponder = _viewResponder;
28712927 [self addToInputParentViewIfNeeded: _activeView];
2872- [_activeView becomeFirstResponder ];
2928+ [_activeView becomeFirstResponderFromFramework ];
28732929}
28742930
28752931- (void )enableActiveViewAccessibility {
@@ -2880,7 +2936,7 @@ - (void)enableActiveViewAccessibility {
28802936}
28812937
28822938- (void )hideTextInput {
2883- [_activeView resignFirstResponder ];
2939+ [_activeView resignFirstResponderFromFramework ];
28842940
28852941 // Remove the input view from the view hierarchy after resigning first responder.
28862942 // This flag is set by clearTextInputClient when autofillContext is empty.
@@ -2894,7 +2950,7 @@ - (void)hideTextInput {
28942950}
28952951
28962952- (void )triggerAutofillSave : (BOOL )saveEntries {
2897- [_activeView resignFirstResponder ];
2953+ [_activeView resignFirstResponderFromFramework ];
28982954
28992955 if (saveEntries) {
29002956 // Make all the input fields in the autofill context visible,
0 commit comments