Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b783740

Browse files
[iOS text input] Use the correct coordinate space for firstRectForRange (#36707)
1 parent c9f4e97 commit b783740

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

shell/platform/darwin/common/framework/Source/FlutterCodecs.mm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,18 @@ - (NSData*)encode:(id)message {
6666
return nil;
6767
}
6868
NSData* encoding;
69+
NSError* error;
6970
if ([message isKindOfClass:[NSArray class]] || [message isKindOfClass:[NSDictionary class]]) {
70-
encoding = [NSJSONSerialization dataWithJSONObject:message options:0 error:nil];
71+
encoding = [NSJSONSerialization dataWithJSONObject:message options:0 error:&error];
7172
} else {
7273
// NSJSONSerialization does not support top-level simple values.
7374
// We encode as singleton array, then extract the relevant bytes.
74-
encoding = [NSJSONSerialization dataWithJSONObject:@[ message ] options:0 error:nil];
75+
encoding = [NSJSONSerialization dataWithJSONObject:@[ message ] options:0 error:&error];
7576
if (encoding) {
7677
encoding = [encoding subdataWithRange:NSMakeRange(1, encoding.length - 2)];
7778
}
7879
}
79-
NSAssert(encoding, @"Invalid JSON message, encoding failed");
80+
NSAssert(encoding, @"Invalid JSON message, encoding failed: %@", error);
8081
return encoding;
8182
}
8283

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ - (void)forwardInvocation:(NSInvocation*)anInvocation {
707707
@interface FlutterTextInputPlugin ()
708708
@property(nonatomic, readonly) fml::WeakPtr<FlutterTextInputPlugin> weakPtr;
709709
@property(nonatomic, readonly) id<FlutterTextInputDelegate> textInputDelegate;
710+
@property(nonatomic, readonly) UIView* hostView;
710711
@end
711712

712713
@interface FlutterTextInputView ()
@@ -1602,7 +1603,10 @@ - (CGRect)firstRectForRange:(UITextRange*)range {
16021603
_cachedFirstRect = [self localRectFromFrameworkTransform:rect];
16031604
}
16041605

1605-
return _cachedFirstRect;
1606+
UIView* hostView = _textInputPlugin.get().hostView;
1607+
NSAssert(hostView == nil || [self isDescendantOfView:hostView], @"%@ is not a descendant of %@",
1608+
self, hostView);
1609+
return hostView ? [hostView convertRect:_cachedFirstRect toView:self] : _cachedFirstRect;
16061610
}
16071611

16081612
if (_scribbleInteractionStatus == FlutterScribbleInteractionStatusNone &&

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,11 @@ - (void)testInputViewsDoNotHaveUITextInteractions {
12171217
#pragma mark - UITextInput methods - Tests
12181218

12191219
- (void)testUpdateFirstRectForRange {
1220-
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
1220+
[self setClientId:123 configuration:self.mutableTemplateCopy];
1221+
1222+
FlutterTextInputView* inputView = textInputPlugin.activeView;
1223+
textInputPlugin.viewController.view.frame = CGRectMake(0, 0, 0, 0);
1224+
12211225
[inputView
12221226
setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @3}];
12231227

@@ -1272,6 +1276,16 @@ - (void)testUpdateFirstRectForRange {
12721276
[inputView setMarkedRect:testRect];
12731277
XCTAssertTrue(
12741278
CGRectEqualToRect(CGRectMake(-306, 3, 300, 300), [inputView firstRectForRange:range]));
1279+
1280+
NSAssert(inputView.superview, @"inputView is not in the view hierarchy!");
1281+
const CGPoint offset = CGPointMake(113, 119);
1282+
CGRect currentFrame = inputView.frame;
1283+
currentFrame.origin = offset;
1284+
inputView.frame = currentFrame;
1285+
// Moving the input view within the FlutterView shouldn't affect the coordinates,
1286+
// since the framework sends us global coordinates.
1287+
XCTAssertTrue(CGRectEqualToRect(CGRectMake(-306 - 113, 3 - 119, 300, 300),
1288+
[inputView firstRectForRange:range]));
12751289
}
12761290

12771291
- (void)testFirstRectForRangeReturnsCorrectSelectionRect {

0 commit comments

Comments
 (0)