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

Commit 29f6334

Browse files
Fix invalid selection handling in text plugins (#19899)
The Windows, Linux, and GLFW embeddings (which all share a common code ancestry) pass TextInput.setEditingState selection base and extents straight through to the shared text model class. The model expects those values to be valid, but the framework sends -1/-1 for "invalid" selections, which happen for some empty text cases (e.g., TextFieldController.clear()). This translates those invalid selection values to an empty selection at the start of the string, as expected by the model. Fixes flutter/flutter#59140
1 parent c0ac43d commit 29f6334

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

shell/platform/glfw/text_input_plugin.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,13 @@ void TextInputPlugin::HandleMethodCall(
187187
"Selection base/extent values invalid.");
188188
return;
189189
}
190-
active_model_->SetEditingState(selection_base->value.GetInt(),
191-
selection_extent->value.GetInt(),
192-
text->value.GetString());
190+
// Flutter uses -1/-1 for invalid; translate that to 0/0 for the model.
191+
int base = selection_base->value.GetInt();
192+
int extent = selection_extent->value.GetInt();
193+
if (base == -1 && extent == -1) {
194+
base = extent = 0;
195+
}
196+
active_model_->SetEditingState(base, extent, text->value.GetString());
193197
} else {
194198
result->NotImplemented();
195199
return;

shell/platform/linux/fl_text_input_plugin.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
#include "flutter/shell/platform/linux/fl_text_input_plugin.h"
66

7+
#include <gtk/gtk.h>
8+
79
#include "flutter/shell/platform/common/cpp/text_input_model.h"
810
#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h"
911
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
1012

11-
#include <gtk/gtk.h>
12-
1313
static constexpr char kChannelName[] = "flutter/textinput";
1414

1515
static constexpr char kBadArgumentsError[] = "Bad Arguments";
@@ -190,6 +190,10 @@ static FlMethodResponse* set_editing_state(FlTextInputPlugin* self,
190190
fl_value_get_int(fl_value_lookup_string(args, kSelectionBaseKey));
191191
int64_t selection_extent =
192192
fl_value_get_int(fl_value_lookup_string(args, kSelectionExtentKey));
193+
// Flutter uses -1/-1 for invalid; translate that to 0/0 for the model.
194+
if (selection_base == -1 && selection_extent == -1) {
195+
selection_base = selection_extent = 0;
196+
}
193197

194198
self->text_model->SetEditingState(selection_base, selection_extent, text);
195199

shell/platform/windows/text_input_plugin.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,13 @@ void TextInputPlugin::HandleMethodCall(
189189
"Selection base/extent values invalid.");
190190
return;
191191
}
192-
active_model_->SetEditingState(selection_base->value.GetInt(),
193-
selection_extent->value.GetInt(),
194-
text->value.GetString());
192+
// Flutter uses -1/-1 for invalid; translate that to 0/0 for the model.
193+
int base = selection_base->value.GetInt();
194+
int extent = selection_extent->value.GetInt();
195+
if (base == -1 && extent == -1) {
196+
base = extent = 0;
197+
}
198+
active_model_->SetEditingState(base, extent, text->value.GetString());
195199
} else {
196200
result->NotImplemented();
197201
return;

0 commit comments

Comments
 (0)