Skip to content

Commit 6faa555

Browse files
committed
[iOS] Resign the restored responder when the framework declines focus
FlutterTextInputView now reads the framework's reply to TextInputClient.onFocusReceived. Any reply other than true resigns the restored first responder through resignFirstResponderFromFramework, so the keyboard is not left up without a focused client. The reply is ignored if the view has been given another client since the restore was reported.
1 parent 51c7055 commit 6faa555

5 files changed

Lines changed: 150 additions & 17 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,8 +1332,11 @@ - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
13321332
}
13331333

13341334
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1335-
didRestoreFirstResponderWithTextInputClient:(int)client {
1336-
[self.textInputChannel invokeMethod:@"TextInputClient.onFocusReceived" arguments:@[ @(client) ]];
1335+
didRestoreFirstResponderWithTextInputClient:(int)client
1336+
result:(FlutterResult)callback {
1337+
[self.textInputChannel invokeMethod:@"TextInputClient.onFocusReceived"
1338+
arguments:@[ @(client) ]
1339+
result:callback];
13371340
}
13381341

13391342
#pragma mark - Undo Manager Delegate

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,23 @@ - (void)testFlutterTextInputViewDidRestoreFirstResponderWillCallTextInputClientO
494494
FlutterEngine* engine = [[FlutterEngine alloc] init];
495495
[engine setBinaryMessenger:mockBinaryMessenger];
496496
[engine runWithEntrypoint:FlutterDefaultDartEntrypoint initialRoute:@"test"];
497-
[engine flutterTextInputView:nil didRestoreFirstResponderWithTextInputClient:123];
498497
FlutterMethodCall* methodCall =
499498
[FlutterMethodCall methodCallWithMethodName:@"TextInputClient.onFocusReceived"
500499
arguments:@[ @(123) ]];
501500
NSData* encodedMethodCall = [[FlutterJSONMethodCodec sharedInstance] encodeMethodCall:methodCall];
502-
OCMVerify([mockBinaryMessenger sendOnChannel:@"flutter/textinput" message:encodedMethodCall]);
501+
NSData* encodedReply = [[FlutterJSONMethodCodec sharedInstance] encodeSuccessEnvelope:@NO];
502+
id mockBinaryReply = [OCMArg invokeBlockWithArgs:encodedReply, nil];
503+
OCMStub([mockBinaryMessenger sendOnChannel:@"flutter/textinput"
504+
message:encodedMethodCall
505+
binaryReply:mockBinaryReply]);
506+
507+
__block id focusReceivedResult;
508+
[engine flutterTextInputView:nil
509+
didRestoreFirstResponderWithTextInputClient:123
510+
result:^(id result) {
511+
focusReceivedResult = result;
512+
}];
513+
XCTAssertEqualObjects(focusReceivedResult, @NO);
503514
}
504515

505516
- (void)testFlutterEngineUpdatesDisplays {

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#import <Foundation/Foundation.h>
99

10+
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
11+
1012
@class FlutterTextInputPlugin;
1113
@class FlutterTextInputView;
1214

@@ -66,7 +68,8 @@ typedef NS_ENUM(NSInteger, FlutterFloatingCursorDragState) {
6668
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView
6769
didResignFirstResponderWithTextInputClient:(int)client;
6870
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView
69-
didRestoreFirstResponderWithTextInputClient:(int)client;
71+
didRestoreFirstResponderWithTextInputClient:(int)client
72+
result:(FlutterResult)callback;
7073
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView
7174
willDismissEditMenuWithTextInputClient:(int)client;
7275
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,13 +1363,31 @@ - (BOOL)becomeFirstResponder {
13631363
if (success && !wasFirstResponder && self.isFirstResponder &&
13641364
_shouldNotifyFrameworkOnFirstResponderRestore) {
13651365
_shouldNotifyFrameworkOnFirstResponderRestore = NO;
1366+
int client = _textInputClient;
1367+
__weak FlutterTextInputView* weakSelf = self;
1368+
FlutterResult handleResult = ^(id result) {
1369+
[weakSelf handleFirstResponderRestoreResult:result client:client];
1370+
};
13661371
// Existing framework client and FocusNode guards decide whether to accept the restored focus.
13671372
[self.textInputDelegate flutterTextInputView:self
1368-
didRestoreFirstResponderWithTextInputClient:_textInputClient];
1373+
didRestoreFirstResponderWithTextInputClient:client
1374+
result:handleResult];
13691375
}
13701376
return success;
13711377
}
13721378

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+
13731391
- (BOOL)resignFirstResponder {
13741392
BOOL wasFirstResponder = self.isFirstResponder;
13751393
BOOL success = [super resignFirstResponder];

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm‎

Lines changed: 109 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,29 +1725,35 @@ - (void)testRestoringPlatformFirstResponderNotifiesFrameworkOnce {
17251725

17261726
XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
17271727
OCMVerify(never(), [engine flutterTextInputView:inputView
1728-
didRestoreFirstResponderWithTextInputClient:123]);
1728+
didRestoreFirstResponderWithTextInputClient:123
1729+
result:[OCMArg any]]);
17291730

17301731
XCTAssertTrue([inputView resignFirstResponder]);
17311732
OCMVerify(times(1), [engine flutterTextInputView:inputView
17321733
didResignFirstResponderWithTextInputClient:123]);
17331734
OCMVerify(never(), [engine flutterTextInputView:inputView
1734-
didRestoreFirstResponderWithTextInputClient:123]);
1735+
didRestoreFirstResponderWithTextInputClient:123
1736+
result:[OCMArg any]]);
17351737
XCTAssertTrue([inputView becomeFirstResponder]);
17361738
OCMVerify(times(1), [engine flutterTextInputView:inputView
1737-
didRestoreFirstResponderWithTextInputClient:123]);
1739+
didRestoreFirstResponderWithTextInputClient:123
1740+
result:[OCMArg any]]);
17381741

17391742
XCTAssertTrue([inputView becomeFirstResponder]);
17401743
OCMVerify(times(1), [engine flutterTextInputView:inputView
1741-
didRestoreFirstResponderWithTextInputClient:123]);
1744+
didRestoreFirstResponderWithTextInputClient:123
1745+
result:[OCMArg any]]);
17421746

17431747
XCTAssertTrue([inputView resignFirstResponder]);
17441748
OCMVerify(times(2), [engine flutterTextInputView:inputView
17451749
didResignFirstResponderWithTextInputClient:123]);
17461750
OCMVerify(times(1), [engine flutterTextInputView:inputView
1747-
didRestoreFirstResponderWithTextInputClient:123]);
1751+
didRestoreFirstResponderWithTextInputClient:123
1752+
result:[OCMArg any]]);
17481753
XCTAssertTrue([inputView becomeFirstResponder]);
17491754
OCMVerify(times(2), [engine flutterTextInputView:inputView
1750-
didRestoreFirstResponderWithTextInputClient:123]);
1755+
didRestoreFirstResponderWithTextInputClient:123
1756+
result:[OCMArg any]]);
17511757

17521758
[inputView resignFirstResponderFromFramework];
17531759
[inputView removeFromSuperview];
@@ -1766,7 +1772,8 @@ - (void)testFrameworkResponderChangesCancelPendingFocusRestore {
17661772
XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
17671773

17681774
OCMVerify(never(), [engine flutterTextInputView:inputView
1769-
didRestoreFirstResponderWithTextInputClient:123]);
1775+
didRestoreFirstResponderWithTextInputClient:123
1776+
result:[OCMArg any]]);
17701777

17711778
[inputView resignFirstResponderFromFramework];
17721779
[inputView removeFromSuperview];
@@ -1786,7 +1793,8 @@ - (void)testInteractiveKeyboardResignDoesNotArmFocusRestore {
17861793
OCMVerify(never(), [engine flutterTextInputView:inputView
17871794
didResignFirstResponderWithTextInputClient:123]);
17881795
OCMVerify(never(), [engine flutterTextInputView:inputView
1789-
didRestoreFirstResponderWithTextInputClient:123]);
1796+
didRestoreFirstResponderWithTextInputClient:123
1797+
result:[OCMArg any]]);
17901798

17911799
[inputView resignFirstResponderFromFramework];
17921800
[inputView removeFromSuperview];
@@ -1803,9 +1811,11 @@ - (void)testChangingClientCancelsPendingFocusRestore {
18031811
XCTAssertTrue([inputView becomeFirstResponder]);
18041812

18051813
OCMVerify(never(), [engine flutterTextInputView:inputView
1806-
didRestoreFirstResponderWithTextInputClient:123]);
1814+
didRestoreFirstResponderWithTextInputClient:123
1815+
result:[OCMArg any]]);
18071816
OCMVerify(never(), [engine flutterTextInputView:inputView
1808-
didRestoreFirstResponderWithTextInputClient:456]);
1817+
didRestoreFirstResponderWithTextInputClient:456
1818+
result:[OCMArg any]]);
18091819

18101820
[inputView resignFirstResponderFromFramework];
18111821
[inputView removeFromSuperview];
@@ -1822,7 +1832,95 @@ - (void)testResettingSameClientCancelsPendingFocusRestore {
18221832
XCTAssertTrue([inputView becomeFirstResponder]);
18231833

18241834
OCMVerify(never(), [engine flutterTextInputView:inputView
1825-
didRestoreFirstResponderWithTextInputClient:123]);
1835+
didRestoreFirstResponderWithTextInputClient:123
1836+
result:[OCMArg any]]);
1837+
1838+
[inputView resignFirstResponderFromFramework];
1839+
[inputView removeFromSuperview];
1840+
}
1841+
1842+
// Resigns and restores the first responder of |inputView| (client 123) from the platform side, and
1843+
// returns the callback that delivers the framework's reply to the resulting focus restore
1844+
// notification.
1845+
- (FlutterResult)restorePlatformFirstResponderOfInputView:(FlutterTextInputView*)inputView {
1846+
__block FlutterResult restoreResult;
1847+
OCMStub([engine flutterTextInputView:inputView
1848+
didRestoreFirstResponderWithTextInputClient:123
1849+
result:[OCMArg any]])
1850+
.andDo(^(NSInvocation* invocation) {
1851+
__unsafe_unretained FlutterResult resultUnsafe;
1852+
[invocation getArgument:&resultUnsafe atIndex:4];
1853+
restoreResult = resultUnsafe;
1854+
});
1855+
1856+
XCTAssertTrue([inputView becomeFirstResponderFromFramework]);
1857+
XCTAssertTrue([inputView resignFirstResponder]);
1858+
XCTAssertTrue([inputView becomeFirstResponder]);
1859+
XCTAssertNotNil(restoreResult);
1860+
return restoreResult;
1861+
}
1862+
1863+
- (void)testDeclinedFocusRestoreResignsFirstResponder {
1864+
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
1865+
[UIApplication.sharedApplication.keyWindow addSubview:inputView];
1866+
[inputView setTextInputClient:123];
1867+
1868+
FlutterResult restoreResult = [self restorePlatformFirstResponderOfInputView:inputView];
1869+
restoreResult(@NO);
1870+
1871+
XCTAssertFalse(inputView.isFirstResponder);
1872+
OCMVerify(times(2), [engine flutterTextInputView:inputView
1873+
didResignFirstResponderWithTextInputClient:123]);
1874+
1875+
// The resign goes through the framework path, so a later restore does not notify again.
1876+
XCTAssertTrue([inputView becomeFirstResponder]);
1877+
OCMVerify(times(1), [engine flutterTextInputView:inputView
1878+
didRestoreFirstResponderWithTextInputClient:123
1879+
result:[OCMArg any]]);
1880+
1881+
[inputView resignFirstResponderFromFramework];
1882+
[inputView removeFromSuperview];
1883+
}
1884+
1885+
- (void)testAcceptedFocusRestoreKeepsFirstResponder {
1886+
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
1887+
[UIApplication.sharedApplication.keyWindow addSubview:inputView];
1888+
[inputView setTextInputClient:123];
1889+
1890+
FlutterResult restoreResult = [self restorePlatformFirstResponderOfInputView:inputView];
1891+
restoreResult(@YES);
1892+
1893+
XCTAssertTrue(inputView.isFirstResponder);
1894+
OCMVerify(times(1), [engine flutterTextInputView:inputView
1895+
didResignFirstResponderWithTextInputClient:123]);
1896+
1897+
[inputView resignFirstResponderFromFramework];
1898+
[inputView removeFromSuperview];
1899+
}
1900+
1901+
- (void)testFailedFocusRestoreReplyResignsFirstResponder {
1902+
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
1903+
[UIApplication.sharedApplication.keyWindow addSubview:inputView];
1904+
[inputView setTextInputClient:123];
1905+
1906+
FlutterResult restoreResult = [self restorePlatformFirstResponderOfInputView:inputView];
1907+
restoreResult([FlutterError errorWithCode:@"error" message:nil details:nil]);
1908+
1909+
XCTAssertFalse(inputView.isFirstResponder);
1910+
1911+
[inputView removeFromSuperview];
1912+
}
1913+
1914+
- (void)testChangingClientIgnoresPendingFocusRestoreReply {
1915+
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
1916+
[UIApplication.sharedApplication.keyWindow addSubview:inputView];
1917+
[inputView setTextInputClient:123];
1918+
1919+
FlutterResult restoreResult = [self restorePlatformFirstResponderOfInputView:inputView];
1920+
[inputView setTextInputClient:456];
1921+
restoreResult(@NO);
1922+
1923+
XCTAssertTrue(inputView.isFirstResponder);
18261924

18271925
[inputView resignFirstResponderFromFramework];
18281926
[inputView removeFromSuperview];

0 commit comments

Comments
 (0)