Skip to content

Commit

Permalink
[iOS text input] Use the correct coordinate space for firstRectForRan…
Browse files Browse the repository at this point in the history
  • Loading branch information
LongCatIsLooong authored Oct 11, 2022
1 parent c9f4e97 commit b783740
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ - (NSData*)encode:(id)message {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ - (void)forwardInvocation:(NSInvocation*)anInvocation {
@interface FlutterTextInputPlugin ()
@property(nonatomic, readonly) fml::WeakPtr<FlutterTextInputPlugin> weakPtr;
@property(nonatomic, readonly) id<FlutterTextInputDelegate> textInputDelegate;
@property(nonatomic, readonly) UIView* hostView;
@end

@interface FlutterTextInputView ()
Expand Down Expand Up @@ -1602,7 +1603,10 @@ - (CGRect)firstRectForRange:(UITextRange*)range {
_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 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,11 @@ - (void)testInputViewsDoNotHaveUITextInteractions {
#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}];

Expand Down Expand Up @@ -1272,6 +1276,16 @@ - (void)testUpdateFirstRectForRange {
[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 {
Expand Down

0 comments on commit b783740

Please sign in to comment.