Skip to content

Commit 4c1aae1

Browse files
committed
[ios17][text_input]fix ios 17.0 keyboard freeze when switching languages (without relying on text affinity) (flutter#47566)
After close examination of the UIKit's default string tokenizer, when querying the line enclosing the end of doc position **in forward direction**, we should return nil (regardless whether the position is forward or backward affinity). This aligns with the [API doc](https://developer.apple.com/documentation/uikit/uitextinputtokenizer/1614464-rangeenclosingposition?language=objc): > If the text position is at a text-unit boundary, it is considered enclosed only if the next position in the given direction is entirely enclosed. Will cherry pick this soon. Otherwise it will be less and less important as users upgrade to iOS 17.1. ### Why my previous workaround also works? It turns out my previous workaround PR flutter#46591 works only because our misuse of text affinity in our text input. Specifically, when adding text affinity support, we only added it to `FlutterTextPosition`, but not `FlutterTextRange`. So when getting the beginning/end position from the range, we assign arbitrary affinities. *List which issues are fixed by this PR. You must list at least one issue.* flutter#46591 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 0545f87 commit 4c1aae1

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ - (UITextRange*)rangeEnclosingPosition:(UITextPosition*)position
590590
case UITextGranularityLine:
591591
// The default UITextInputStringTokenizer does not handle line granularity
592592
// correctly. We need to implement our own line tokenizer.
593-
result = [self lineEnclosingPosition:position];
593+
result = [self lineEnclosingPosition:position inDirection:direction];
594594
break;
595595
case UITextGranularityCharacter:
596596
case UITextGranularityWord:
@@ -606,7 +606,21 @@ - (UITextRange*)rangeEnclosingPosition:(UITextPosition*)position
606606
return result;
607607
}
608608

609-
- (UITextRange*)lineEnclosingPosition:(UITextPosition*)position {
609+
- (UITextRange*)lineEnclosingPosition:(UITextPosition*)position
610+
inDirection:(UITextDirection)direction {
611+
// TODO(hellohuanlin): remove iOS 17 check. The same logic should apply to older iOS version.
612+
if (@available(iOS 17.0, *)) {
613+
// According to the API doc if the text position is at a text-unit boundary, it is considered
614+
// enclosed only if the next position in the given direction is entirely enclosed. Link:
615+
// https://developer.apple.com/documentation/uikit/uitextinputtokenizer/1614464-rangeenclosingposition?language=objc
616+
FlutterTextPosition* flutterPosition = (FlutterTextPosition*)position;
617+
if (flutterPosition.index > _textInputView.text.length ||
618+
(flutterPosition.index == _textInputView.text.length &&
619+
direction == UITextStorageDirectionForward)) {
620+
return nil;
621+
}
622+
}
623+
610624
// Gets the first line break position after the input position.
611625
NSString* textAfter = [_textInputView
612626
textInRange:[_textInputView textRangeFromPosition:position

shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,54 @@ - (void)testFlutterTokenizerCanParseLines {
25382538
XCTAssertEqual(range.range.length, 20u);
25392539
}
25402540

2541+
- (void)testFlutterTokenizerLineEnclosingEndOfDocumentInBackwardDirectionShouldNotReturnNil {
2542+
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
2543+
[inputView insertText:@"0123456789\n012345"];
2544+
id<UITextInputTokenizer> tokenizer = [inputView tokenizer];
2545+
2546+
FlutterTextRange* range =
2547+
(FlutterTextRange*)[tokenizer rangeEnclosingPosition:[inputView endOfDocument]
2548+
withGranularity:UITextGranularityLine
2549+
inDirection:UITextStorageDirectionBackward];
2550+
XCTAssertEqual(range.range.location, 11u);
2551+
XCTAssertEqual(range.range.length, 6u);
2552+
}
2553+
2554+
- (void)testFlutterTokenizerLineEnclosingEndOfDocumentInForwardDirectionShouldReturnNilOnIOS17 {
2555+
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
2556+
[inputView insertText:@"0123456789\n012345"];
2557+
id<UITextInputTokenizer> tokenizer = [inputView tokenizer];
2558+
2559+
FlutterTextRange* range =
2560+
(FlutterTextRange*)[tokenizer rangeEnclosingPosition:[inputView endOfDocument]
2561+
withGranularity:UITextGranularityLine
2562+
inDirection:UITextStorageDirectionForward];
2563+
if (@available(iOS 17.0, *)) {
2564+
XCTAssertNil(range);
2565+
} else {
2566+
XCTAssertEqual(range.range.location, 11u);
2567+
XCTAssertEqual(range.range.length, 6u);
2568+
}
2569+
}
2570+
2571+
- (void)testFlutterTokenizerLineEnclosingOutOfRangePositionShouldReturnNilOnIOS17 {
2572+
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
2573+
[inputView insertText:@"0123456789\n012345"];
2574+
id<UITextInputTokenizer> tokenizer = [inputView tokenizer];
2575+
2576+
FlutterTextPosition* position = [FlutterTextPosition positionWithIndex:100];
2577+
FlutterTextRange* range =
2578+
(FlutterTextRange*)[tokenizer rangeEnclosingPosition:position
2579+
withGranularity:UITextGranularityLine
2580+
inDirection:UITextStorageDirectionForward];
2581+
if (@available(iOS 17.0, *)) {
2582+
XCTAssertNil(range);
2583+
} else {
2584+
XCTAssertEqual(range.range.location, 0u);
2585+
XCTAssertEqual(range.range.length, 0u);
2586+
}
2587+
}
2588+
25412589
- (void)testFlutterTextInputPluginRetainsFlutterTextInputView {
25422590
FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];
25432591
FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] initWithDelegate:engine];

0 commit comments

Comments
 (0)