[iOS text input] Use the correct coordinate space for firstRectForRange (flutter/engine#36707)

This commit is contained in:
LongCatIsLooong
2022-10-11 14:07:22 -07:00
committed by GitHub
parent e29b34dfcb
commit a0c098ec46
3 changed files with 24 additions and 5 deletions

View File

@@ -66,17 +66,18 @@
return nil;
}
NSData* encoding;
NSError* error;
if ([message isKindOfClass:[NSArray class]] || [message isKindOfClass:[NSDictionary class]]) {
encoding = [NSJSONSerialization dataWithJSONObject:message options:0 error:nil];
encoding = [NSJSONSerialization dataWithJSONObject:message options:0 error:&error];
} else {
// NSJSONSerialization does not support top-level simple values.
// We encode as singleton array, then extract the relevant bytes.
encoding = [NSJSONSerialization dataWithJSONObject:@[ message ] options:0 error:nil];
encoding = [NSJSONSerialization dataWithJSONObject:@[ message ] options:0 error:&error];
if (encoding) {
encoding = [encoding subdataWithRange:NSMakeRange(1, encoding.length - 2)];
}
}
NSAssert(encoding, @"Invalid JSON message, encoding failed");
NSAssert(encoding, @"Invalid JSON message, encoding failed: %@", error);
return encoding;
}

View File

@@ -707,6 +707,7 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
@interface FlutterTextInputPlugin ()
@property(nonatomic, readonly) fml::WeakPtr<FlutterTextInputPlugin> weakPtr;
@property(nonatomic, readonly) id<FlutterTextInputDelegate> textInputDelegate;
@property(nonatomic, readonly) UIView* hostView;
@end
@interface FlutterTextInputView ()
@@ -1602,7 +1603,10 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point,
_cachedFirstRect = [self localRectFromFrameworkTransform:rect];
}
return _cachedFirstRect;
UIView* hostView = _textInputPlugin.get().hostView;
NSAssert(hostView == nil || [self isDescendantOfView:hostView], @"%@ is not a descendant of %@",
self, hostView);
return hostView ? [hostView convertRect:_cachedFirstRect toView:self] : _cachedFirstRect;
}
if (_scribbleInteractionStatus == FlutterScribbleInteractionStatusNone &&

View File

@@ -1217,7 +1217,11 @@ FLUTTER_ASSERT_ARC
#pragma mark - UITextInput methods - Tests
- (void)testUpdateFirstRectForRange {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[self setClientId:123 configuration:self.mutableTemplateCopy];
FlutterTextInputView* inputView = textInputPlugin.activeView;
textInputPlugin.viewController.view.frame = CGRectMake(0, 0, 0, 0);
[inputView
setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @3}];
@@ -1272,6 +1276,16 @@ FLUTTER_ASSERT_ARC
[inputView setMarkedRect:testRect];
XCTAssertTrue(
CGRectEqualToRect(CGRectMake(-306, 3, 300, 300), [inputView firstRectForRange:range]));
NSAssert(inputView.superview, @"inputView is not in the view hierarchy!");
const CGPoint offset = CGPointMake(113, 119);
CGRect currentFrame = inputView.frame;
currentFrame.origin = offset;
inputView.frame = currentFrame;
// Moving the input view within the FlutterView shouldn't affect the coordinates,
// since the framework sends us global coordinates.
XCTAssertTrue(CGRectEqualToRect(CGRectMake(-306 - 113, 3 - 119, 300, 300),
[inputView firstRectForRange:range]));
}
- (void)testFirstRectForRangeReturnsCorrectSelectionRect {