[iOS] Add UITextInteraction only when inputDelegate is nil (flutter/engine#32881)

This commit is contained in:
LongCatIsLooong
2022-04-28 09:44:04 -07:00
committed by GitHub
parent fe2ff918ca
commit 61e0edc391
2 changed files with 64 additions and 23 deletions

View File

@@ -711,7 +711,6 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
@property(nonatomic, assign) CGRect markedRect;
@property(nonatomic) BOOL isVisibleToAutofill;
@property(nonatomic, assign) BOOL accessibilityEnabled;
@property(nonatomic, retain) UITextInteraction* textInteraction API_AVAILABLE(ios(13.0));
- (void)setEditableTransform:(NSArray*)matrix;
@end
@@ -734,6 +733,7 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
// allowed to access its textInputDelegate.
BOOL _decommissioned;
bool _enableInteractiveSelection;
UITextInteraction* _textInteraction API_AVAILABLE(ios(13.0));
}
@synthesize tokenizer = _tokenizer;
@@ -905,7 +905,27 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
_hasPlaceholder = NO;
}
- (UITextInteraction*)textInteraction API_AVAILABLE(ios(13.0)) {
if (!_textInteraction) {
_textInteraction =
[[UITextInteraction textInteractionForMode:UITextInteractionModeEditable] retain];
_textInteraction.textInput = self;
}
return _textInteraction;
}
- (void)setTextInputState:(NSDictionary*)state {
if (@available(iOS 13.0, *)) {
// [UITextInteraction willMoveToView:] sometimes sets the textInput's inputDelegate
// to nil. This is likely a bug in UIKit. In order to inform the keyboard of text
// and selection changes when that happens, add a dummy UITextInteraction to this
// view so it sets a valid inputDelegate that we can call textWillChange et al. on.
// See https://github.com/flutter/engine/pull/32881.
if (!self.inputDelegate && self.isFirstResponder) {
[self addInteraction:self.textInteraction];
}
}
NSString* newText = state[@"text"];
BOOL textChanged = ![self.text isEqualToString:newText];
if (textChanged) {
@@ -941,6 +961,12 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
if (textChanged) {
[self.inputDelegate textDidChange:self];
}
if (@available(iOS 13.0, *)) {
if (_textInteraction) {
[self removeInteraction:_textInteraction];
}
}
}
// Forward touches to the viewResponder to allow tapping inside the UITextField as normal.
@@ -1327,7 +1353,7 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
if (toIndex >= fromIndex) {
return [FlutterTextRange rangeWithNSRange:NSMakeRange(fromIndex, toIndex - fromIndex)];
} else {
// toIndex may be less than fromIndex, because
// toIndex can be smaller than fromIndex, because
// UITextInputStringTokenizer does not handle CJK characters
// well in some cases. See:
// https://github.com/flutter/flutter/issues/58750#issuecomment-644469521
@@ -1698,14 +1724,6 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
// call always turns a CGRect's negative dimensions into non-negative values, e.g.,
// (1, 2, -3, -4) would become (-2, -2, 3, 4).
_isFloatingCursorActive = true;
// This makes sure UITextSelectionView.interactionAssistant is not nil so
// UITextSelectionView has access to this view (and its bounds). Otherwise
// floating cursor breaks: https://github.com/flutter/flutter/issues/70267.
if (@available(iOS 13.0, *)) {
self.textInteraction = [UITextInteraction textInteractionForMode:UITextInteractionModeEditable];
self.textInteraction.textInput = self;
[self addInteraction:_textInteraction];
}
[self.textInputDelegate flutterTextInputView:self
updateFloatingCursor:FlutterFloatingCursorDragStateStart
withClient:_textInputClient
@@ -1722,12 +1740,6 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
- (void)endFloatingCursor {
_isFloatingCursorActive = false;
if (@available(iOS 13.0, *)) {
if (_textInteraction != NULL) {
[self removeInteraction:_textInteraction];
self.textInteraction = NULL;
}
}
[self.textInputDelegate flutterTextInputView:self
updateFloatingCursor:FlutterFloatingCursorDragStateEnd
withClient:_textInputClient

View File

@@ -20,6 +20,7 @@ FLUTTER_ASSERT_ARC
@interface FlutterTextInputView ()
@property(nonatomic, copy) NSString* autofillId;
- (void)setEditableTransform:(NSArray*)matrix;
- (void)setTextInputClient:(int)client;
- (void)setTextInputState:(NSDictionary*)state;
- (void)setMarkedRect:(CGRect)markedRect;
- (void)updateEditingState;
@@ -1071,6 +1072,41 @@ FLUTTER_ASSERT_ARC
}]]);
}
- (void)testInputViewsHasNonNilInputDelegate {
if (@available(iOS 13.0, *)) {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[UIApplication.sharedApplication.keyWindow addSubview:inputView];
[inputView setTextInputClient:123];
[inputView reloadInputViews];
[inputView becomeFirstResponder];
NSAssert(inputView.isFirstResponder, @"inputView is not first responder");
inputView.inputDelegate = nil;
FlutterTextInputView* mockInputView = OCMPartialMock(inputView);
[mockInputView setTextInputState:@{
@"text" : @"COMPOSING",
@"composingBase" : @1,
@"composingExtent" : @3
}];
OCMVerify([mockInputView setInputDelegate:[OCMArg isNotNil]]);
}
}
- (void)testInputViewsDoNotHaveUITextInteractions {
if (@available(iOS 13.0, *)) {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
BOOL hasTextInteraction = NO;
for (id interaction in inputView.interactions) {
hasTextInteraction = [interaction isKindOfClass:[UITextInteraction class]];
if (hasTextInteraction) {
break;
}
}
XCTAssertFalse(hasTextInteraction);
}
}
#pragma mark - UITextInput methods - Tests
- (void)testUpdateFirstRectForRange {
@@ -1244,13 +1280,6 @@ FLUTTER_ASSERT_ARC
#pragma mark - Floating Cursor - Tests
- (void)testInputViewsHaveUIInteractions {
if (@available(iOS 13.0, *)) {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
XCTAssertGreaterThan(inputView.interactions.count, 0ul);
}
}
- (void)testFloatingCursorDoesNotThrow {
// The keyboard implementation may send unbalanced calls to the input view.
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];