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

Commit 1d581ab

Browse files
authored
[macOS] Clear IME mark text on clear input client (#31849)
When the embedder receives a TextInput.clearClient message from the framework (typically when a text field loses focus), if the user is currently inputting composing text using an IME, commit the composing text, end composing, and clear the IME's composing state. This also exposes a public `editingState` getter on FlutterTextInputPlugin as part of the TestMethods informal protocol. This allows us to get at the text editing state as a dictionary in tests. Issue: flutter/flutter#92060
1 parent 46dc713 commit 1d581ab

3 files changed

Lines changed: 83 additions & 3 deletions

File tree

‎shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@
5252
@interface FlutterTextInputPlugin (TestMethods)
5353
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
5454
- (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(NSRangePointer)actualRange;
55+
- (NSDictionary*)editingState;
5556
@end

‎shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
275275
_shown = FALSE;
276276
[_textInputContext deactivate];
277277
} else if ([method isEqualToString:kClearClientMethod]) {
278+
// If there's an active mark region, commit it, end composing, and clear the IME's mark text.
279+
if (_activeModel && _activeModel->composing()) {
280+
_activeModel->CommitComposing();
281+
_activeModel->EndComposing();
282+
}
283+
[_textInputContext discardMarkedText];
284+
278285
_clientID = nil;
279286
_inputAction = nil;
280287
_enableDeltaModel = NO;
@@ -360,22 +367,28 @@ - (void)setEditingState:(NSDictionary*)state {
360367
flutter::TextRange composing_range = RangeFromBaseExtent(
361368
state[kComposingBaseKey], state[kComposingExtentKey], _activeModel->composing_range());
362369
size_t cursor_offset = selected_range.base() - composing_range.start();
370+
if (!composing_range.collapsed() && !_activeModel->composing()) {
371+
_activeModel->BeginComposing();
372+
} else if (composing_range.collapsed() && _activeModel->composing()) {
373+
_activeModel->EndComposing();
374+
[_textInputContext discardMarkedText];
375+
}
363376
_activeModel->SetComposingRange(composing_range, cursor_offset);
364377
[_client becomeFirstResponder];
365378
[self updateTextAndSelection];
366379
}
367380

368-
- (void)updateEditState {
381+
- (NSDictionary*)editingState {
369382
if (_activeModel == nullptr) {
370-
return;
383+
return nil;
371384
}
372385

373386
NSString* const textAffinity = [self textAffinityString];
374387

375388
int composingBase = _activeModel->composing() ? _activeModel->composing_range().base() : -1;
376389
int composingExtent = _activeModel->composing() ? _activeModel->composing_range().extent() : -1;
377390

378-
NSDictionary* state = @{
391+
return @{
379392
kSelectionBaseKey : @(_activeModel->selection().base()),
380393
kSelectionExtentKey : @(_activeModel->selection().extent()),
381394
kSelectionAffinityKey : textAffinity,
@@ -384,7 +397,14 @@ - (void)updateEditState {
384397
kComposingExtentKey : @(composingExtent),
385398
kTextKey : [NSString stringWithUTF8String:_activeModel->GetText().c_str()]
386399
};
400+
}
401+
402+
- (void)updateEditState {
403+
if (_activeModel == nullptr) {
404+
return;
405+
}
387406

407+
NSDictionary* state = [self editingState];
388408
[_channel invokeMethod:kUpdateEditStateResponseMethod arguments:@[ self.clientID, state ]];
389409
[self updateTextAndSelection];
390410
}

‎shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ - (void)updateString:(NSString*)string withSelection:(NSRange)selection {
3030

3131
@interface FlutterInputPluginTestObjc : NSObject
3232
- (bool)testEmptyCompositionRange;
33+
- (bool)testClearClientDuringComposing;
3334
@end
3435

3536
@implementation FlutterInputPluginTestObjc
@@ -99,6 +100,60 @@ - (bool)testEmptyCompositionRange {
99100
return true;
100101
}
101102

103+
- (bool)testClearClientDuringComposing {
104+
// Set up FlutterTextInputPlugin.
105+
id engineMock = OCMClassMock([FlutterEngine class]);
106+
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
107+
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
108+
[engineMock binaryMessenger])
109+
.andReturn(binaryMessengerMock);
110+
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
111+
nibName:@""
112+
bundle:nil];
113+
FlutterTextInputPlugin* plugin =
114+
[[FlutterTextInputPlugin alloc] initWithViewController:viewController];
115+
116+
// Set input client 1.
117+
[plugin handleMethodCall:[FlutterMethodCall
118+
methodCallWithMethodName:@"TextInput.setClient"
119+
arguments:@[
120+
@(1), @{
121+
@"inputAction" : @"action",
122+
@"inputType" : @{@"name" : @"inputName"},
123+
}
124+
]]
125+
result:^(id){
126+
}];
127+
128+
// Set editing state with an active composing range.
129+
[plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
130+
arguments:@{
131+
@"text" : @"Text",
132+
@"selectionBase" : @(0),
133+
@"selectionExtent" : @(0),
134+
@"composingBase" : @(0),
135+
@"composingExtent" : @(1),
136+
}]
137+
result:^(id){
138+
}];
139+
140+
// Verify composing range is (0, 1).
141+
NSDictionary* editingState = [plugin editingState];
142+
EXPECT_EQ([editingState[@"composingBase"] intValue], 0);
143+
EXPECT_EQ([editingState[@"composingExtent"] intValue], 1);
144+
145+
// Clear input client.
146+
[plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.clearClient"
147+
arguments:@[]]
148+
result:^(id){
149+
}];
150+
151+
// Verify composing range is collapsed.
152+
editingState = [plugin editingState];
153+
EXPECT_EQ([editingState[@"composingBase"] intValue], [editingState[@"composingExtent"] intValue]);
154+
return true;
155+
}
156+
102157
- (bool)testFirstRectForCharacterRange {
103158
id engineMock = OCMClassMock([FlutterEngine class]);
104159
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
@@ -368,6 +423,10 @@ - (bool)testOperationsThatTriggerDelta {
368423
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testEmptyCompositionRange]);
369424
}
370425

426+
TEST(FlutterTextInputPluginTest, TestClearClientDuringComposing) {
427+
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testClearClientDuringComposing]);
428+
}
429+
371430
TEST(FlutterTextInputPluginTest, TestFirstRectForCharacterRange) {
372431
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testFirstRectForCharacterRange]);
373432
}

0 commit comments

Comments
 (0)