Skip to content

Commit

Permalink
[ios17][text_input]fix ios 17.0 keyboard freeze when switching langua…
Browse files Browse the repository at this point in the history
…ges (without relying on text affinity) (#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 #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.*

#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
  • Loading branch information
hellohuanlin committed Nov 14, 2023
1 parent 74d1662 commit 17537ff
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ - (UITextRange*)rangeEnclosingPosition:(UITextPosition*)position
case UITextGranularityLine:
// The default UITextInputStringTokenizer does not handle line granularity
// correctly. We need to implement our own line tokenizer.
result = [self lineEnclosingPosition:position];
result = [self lineEnclosingPosition:position inDirection:direction];
break;
case UITextGranularityCharacter:
case UITextGranularityWord:
Expand All @@ -618,7 +618,21 @@ - (UITextRange*)rangeEnclosingPosition:(UITextPosition*)position
return result;
}

- (UITextRange*)lineEnclosingPosition:(UITextPosition*)position {
- (UITextRange*)lineEnclosingPosition:(UITextPosition*)position
inDirection:(UITextDirection)direction {
// TODO(hellohuanlin): remove iOS 17 check. The same logic should apply to older iOS version.
if (@available(iOS 17.0, *)) {
// According to the API doc 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. Link:
// https://developer.apple.com/documentation/uikit/uitextinputtokenizer/1614464-rangeenclosingposition?language=objc
FlutterTextPosition* flutterPosition = (FlutterTextPosition*)position;
if (flutterPosition.index > _textInputView.text.length ||
(flutterPosition.index == _textInputView.text.length &&
direction == UITextStorageDirectionForward)) {
return nil;
}
}

// Gets the first line break position after the input position.
NSString* textAfter = [_textInputView
textInRange:[_textInputView textRangeFromPosition:position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2660,6 +2660,54 @@ - (void)testFlutterTokenizerCanParseLines {
XCTAssertEqual(range.range.length, 20u);
}

- (void)testFlutterTokenizerLineEnclosingEndOfDocumentInBackwardDirectionShouldNotReturnNil {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[inputView insertText:@"0123456789\n012345"];
id<UITextInputTokenizer> tokenizer = [inputView tokenizer];

FlutterTextRange* range =
(FlutterTextRange*)[tokenizer rangeEnclosingPosition:[inputView endOfDocument]
withGranularity:UITextGranularityLine
inDirection:UITextStorageDirectionBackward];
XCTAssertEqual(range.range.location, 11u);
XCTAssertEqual(range.range.length, 6u);
}

- (void)testFlutterTokenizerLineEnclosingEndOfDocumentInForwardDirectionShouldReturnNilOnIOS17 {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[inputView insertText:@"0123456789\n012345"];
id<UITextInputTokenizer> tokenizer = [inputView tokenizer];

FlutterTextRange* range =
(FlutterTextRange*)[tokenizer rangeEnclosingPosition:[inputView endOfDocument]
withGranularity:UITextGranularityLine
inDirection:UITextStorageDirectionForward];
if (@available(iOS 17.0, *)) {
XCTAssertNil(range);
} else {
XCTAssertEqual(range.range.location, 11u);
XCTAssertEqual(range.range.length, 6u);
}
}

- (void)testFlutterTokenizerLineEnclosingOutOfRangePositionShouldReturnNilOnIOS17 {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[inputView insertText:@"0123456789\n012345"];
id<UITextInputTokenizer> tokenizer = [inputView tokenizer];

FlutterTextPosition* position = [FlutterTextPosition positionWithIndex:100];
FlutterTextRange* range =
(FlutterTextRange*)[tokenizer rangeEnclosingPosition:position
withGranularity:UITextGranularityLine
inDirection:UITextStorageDirectionForward];
if (@available(iOS 17.0, *)) {
XCTAssertNil(range);
} else {
XCTAssertEqual(range.range.location, 0u);
XCTAssertEqual(range.range.length, 0u);
}
}

- (void)testFlutterTextInputPluginRetainsFlutterTextInputView {
FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];
FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] initWithDelegate:engine];
Expand Down

0 comments on commit 17537ff

Please sign in to comment.