[macOS] fix text selection when there's composing text (flutter/engine#31936)
This commit is contained in:
@@ -28,10 +28,19 @@ TextInputModel::TextInputModel() = default;
|
||||
|
||||
TextInputModel::~TextInputModel() = default;
|
||||
|
||||
void TextInputModel::SetText(const std::string& text) {
|
||||
bool TextInputModel::SetText(const std::string& text,
|
||||
const TextRange& selection,
|
||||
const TextRange& composing_range) {
|
||||
text_ = fml::Utf8ToUtf16(text);
|
||||
selection_ = TextRange(0);
|
||||
composing_range_ = TextRange(0);
|
||||
if (!text_range().Contains(selection) ||
|
||||
!text_range().Contains(composing_range)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
selection_ = selection;
|
||||
composing_range_ = composing_range;
|
||||
composing_ = !composing_range.collapsed();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TextInputModel::SetSelection(const TextRange& range) {
|
||||
|
||||
@@ -20,22 +20,33 @@ class TextInputModel {
|
||||
TextInputModel();
|
||||
virtual ~TextInputModel();
|
||||
|
||||
// Sets the text.
|
||||
// Sets the text, as well as the selection and the composing region.
|
||||
//
|
||||
// Resets the selection base and extent.
|
||||
void SetText(const std::string& text);
|
||||
// This method is typically used to update the TextInputModel's editing state
|
||||
// when the Flutter framework sends its latest text editing state.
|
||||
bool SetText(const std::string& text,
|
||||
const TextRange& selection = TextRange(0),
|
||||
const TextRange& composing_range = TextRange(0));
|
||||
|
||||
// Attempts to set the text selection.
|
||||
//
|
||||
// Returns false if the selection is not within the bounds of the text.
|
||||
// While in composing mode, the selection is restricted to the composing
|
||||
// range; otherwise, it is restricted to the length of the text.
|
||||
//
|
||||
// To update both the text and the selection/composing range within the text
|
||||
// (for instance, when the framework sends its latest text editing state),
|
||||
// call |SetText| instead.
|
||||
bool SetSelection(const TextRange& range);
|
||||
|
||||
// Attempts to set the composing range.
|
||||
//
|
||||
// Returns false if the range or offset are out of range for the text, or if
|
||||
// the offset is outside the composing range.
|
||||
//
|
||||
// To update both the text and the selection/composing range within the text
|
||||
// (for instance, when the framework sends its latest text editing state),
|
||||
// call |SetText| instead.
|
||||
bool SetComposingRange(const TextRange& range, size_t cursor_offset);
|
||||
|
||||
// Begins IME composing mode.
|
||||
|
||||
@@ -356,9 +356,6 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
}
|
||||
|
||||
NSString* text = state[kTextKey];
|
||||
if (text != nil) {
|
||||
_activeModel->SetText([text UTF8String]);
|
||||
}
|
||||
|
||||
flutter::TextRange selected_range = RangeFromBaseExtent(
|
||||
state[kSelectionBaseKey], state[kSelectionExtentKey], _activeModel->selection());
|
||||
@@ -366,14 +363,12 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
|
||||
flutter::TextRange composing_range = RangeFromBaseExtent(
|
||||
state[kComposingBaseKey], state[kComposingExtentKey], _activeModel->composing_range());
|
||||
size_t cursor_offset = selected_range.base() - composing_range.start();
|
||||
if (!composing_range.collapsed() && !_activeModel->composing()) {
|
||||
_activeModel->BeginComposing();
|
||||
} else if (composing_range.collapsed() && _activeModel->composing()) {
|
||||
_activeModel->EndComposing();
|
||||
|
||||
const bool wasComposing = _activeModel->composing();
|
||||
_activeModel->SetText([text UTF8String], selected_range, composing_range);
|
||||
if (composing_range.collapsed() && wasComposing) {
|
||||
[_textInputContext discardMarkedText];
|
||||
}
|
||||
_activeModel->SetComposingRange(composing_range, cursor_offset);
|
||||
[_client becomeFirstResponder];
|
||||
[self updateTextAndSelection];
|
||||
}
|
||||
@@ -621,6 +616,15 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
std::string marked_text = isAttributedString ? [[string string] UTF8String] : [string UTF8String];
|
||||
_activeModel->UpdateComposingText(marked_text);
|
||||
|
||||
// Update the selection within the marked text.
|
||||
long signedLength = static_cast<long>(selectedRange.length);
|
||||
long location = selectedRange.location + _activeModel->composing_range().base();
|
||||
long textLength = _activeModel->text_range().end();
|
||||
|
||||
size_t base = std::clamp(location, 0L, textLength);
|
||||
size_t extent = std::clamp(location + signedLength, 0L, textLength);
|
||||
_activeModel->SetSelection(flutter::TextRange(base, extent));
|
||||
|
||||
if (_enableDeltaModel) {
|
||||
flutter::TextRange composing = _activeModel->composing_range();
|
||||
[self updateEditStateWithDelta:flutter::TextEditingDelta(textBeforeChange, composing,
|
||||
|
||||
@@ -100,6 +100,151 @@
|
||||
return true;
|
||||
}
|
||||
|
||||
- (bool)testSetMarkedTextWithSelectionChange {
|
||||
id engineMock = OCMClassMock([FlutterEngine class]);
|
||||
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[engineMock binaryMessenger])
|
||||
.andReturn(binaryMessengerMock);
|
||||
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
|
||||
nibName:@""
|
||||
bundle:nil];
|
||||
|
||||
FlutterTextInputPlugin* plugin =
|
||||
[[FlutterTextInputPlugin alloc] initWithViewController:viewController];
|
||||
|
||||
[plugin handleMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInput.setClient"
|
||||
arguments:@[
|
||||
@(1), @{
|
||||
@"inputAction" : @"action",
|
||||
@"inputType" : @{@"name" : @"inputName"},
|
||||
}
|
||||
]]
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
|
||||
arguments:@{
|
||||
@"text" : @"Text",
|
||||
@"selectionBase" : @(4),
|
||||
@"selectionExtent" : @(4),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
}];
|
||||
[plugin handleMethodCall:call
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
[plugin setMarkedText:@"marked"
|
||||
selectedRange:NSMakeRange(1, 0)
|
||||
replacementRange:NSMakeRange(NSNotFound, 0)];
|
||||
|
||||
NSDictionary* expectedState = @{
|
||||
@"selectionBase" : @(5),
|
||||
@"selectionExtent" : @(5),
|
||||
@"selectionAffinity" : @"TextAffinity.upstream",
|
||||
@"selectionIsDirectional" : @(NO),
|
||||
@"composingBase" : @(4),
|
||||
@"composingExtent" : @(10),
|
||||
@"text" : @"Textmarked",
|
||||
};
|
||||
|
||||
NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
|
||||
encodeMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInputClient.updateEditingState"
|
||||
arguments:@[ @(1), expectedState ]]];
|
||||
|
||||
OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
|
||||
@try {
|
||||
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
} @catch (...) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
- (bool)testComposingRegionRemovedByFramework {
|
||||
id engineMock = OCMClassMock([FlutterEngine class]);
|
||||
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[engineMock binaryMessenger])
|
||||
.andReturn(binaryMessengerMock);
|
||||
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
|
||||
nibName:@""
|
||||
bundle:nil];
|
||||
|
||||
FlutterTextInputPlugin* plugin =
|
||||
[[FlutterTextInputPlugin alloc] initWithViewController:viewController];
|
||||
|
||||
[plugin handleMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInput.setClient"
|
||||
arguments:@[
|
||||
@(1), @{
|
||||
@"inputAction" : @"action",
|
||||
@"inputType" : @{@"name" : @"inputName"},
|
||||
}
|
||||
]]
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
|
||||
arguments:@{
|
||||
@"text" : @"Text",
|
||||
@"selectionBase" : @(4),
|
||||
@"selectionExtent" : @(4),
|
||||
@"composingBase" : @(2),
|
||||
@"composingExtent" : @(4),
|
||||
}];
|
||||
[plugin handleMethodCall:call
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
// Update with the composing region removed.
|
||||
call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
|
||||
arguments:@{
|
||||
@"text" : @"Te",
|
||||
@"selectionBase" : @(2),
|
||||
@"selectionExtent" : @(2),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
}];
|
||||
[plugin handleMethodCall:call
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
NSDictionary* expectedState = @{
|
||||
@"selectionBase" : @(2),
|
||||
@"selectionExtent" : @(2),
|
||||
@"selectionAffinity" : @"TextAffinity.upstream",
|
||||
@"selectionIsDirectional" : @(NO),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
@"text" : @"Te",
|
||||
};
|
||||
|
||||
NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
|
||||
encodeMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInputClient.updateEditingState"
|
||||
arguments:@[ @(1), expectedState ]]];
|
||||
|
||||
OCMExpect( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
|
||||
@try {
|
||||
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
} @catch (...) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
- (bool)testClearClientDuringComposing {
|
||||
// Set up FlutterTextInputPlugin.
|
||||
id engineMock = OCMClassMock([FlutterEngine class]);
|
||||
@@ -484,6 +629,14 @@ TEST(FlutterTextInputPluginTest, TestEmptyCompositionRange) {
|
||||
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testEmptyCompositionRange]);
|
||||
}
|
||||
|
||||
TEST(FlutterTextInputPluginTest, TestSetMarkedTextWithSelectionChange) {
|
||||
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSetMarkedTextWithSelectionChange]);
|
||||
}
|
||||
|
||||
TEST(FlutterTextInputPluginTest, TestComposingRegionRemovedByFramework) {
|
||||
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testComposingRegionRemovedByFramework]);
|
||||
}
|
||||
|
||||
TEST(FlutterTextInputPluginTest, TestClearClientDuringComposing) {
|
||||
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testClearClientDuringComposing]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user