From cd66f501cba63030ecaf2d2b70aa6f48d11969f8 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Mon, 27 Jul 2020 11:00:02 -0700 Subject: [PATCH] Fix invalid selection handling in text plugins (flutter/engine#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 https://github.com/flutter/flutter/issues/59140 --- .../flutter/shell/platform/glfw/text_input_plugin.cc | 10 +++++++--- .../shell/platform/linux/fl_text_input_plugin.cc | 8 ++++++-- .../shell/platform/windows/text_input_plugin.cc | 10 +++++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc b/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc index 760bea2ce8..496b9e0abc 100644 --- a/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc @@ -187,9 +187,13 @@ void TextInputPlugin::HandleMethodCall( "Selection base/extent values invalid."); return; } - active_model_->SetEditingState(selection_base->value.GetInt(), - selection_extent->value.GetInt(), - text->value.GetString()); + // Flutter uses -1/-1 for invalid; translate that to 0/0 for the model. + int base = selection_base->value.GetInt(); + int extent = selection_extent->value.GetInt(); + if (base == -1 && extent == -1) { + base = extent = 0; + } + active_model_->SetEditingState(base, extent, text->value.GetString()); } else { result->NotImplemented(); return; diff --git a/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.cc b/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.cc index a50c60b804..f62203f71b 100644 --- a/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/linux/fl_text_input_plugin.cc @@ -4,12 +4,12 @@ #include "flutter/shell/platform/linux/fl_text_input_plugin.h" +#include + #include "flutter/shell/platform/common/cpp/text_input_model.h" #include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h" #include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h" -#include - static constexpr char kChannelName[] = "flutter/textinput"; static constexpr char kBadArgumentsError[] = "Bad Arguments"; @@ -190,6 +190,10 @@ static FlMethodResponse* set_editing_state(FlTextInputPlugin* self, fl_value_get_int(fl_value_lookup_string(args, kSelectionBaseKey)); int64_t selection_extent = fl_value_get_int(fl_value_lookup_string(args, kSelectionExtentKey)); + // Flutter uses -1/-1 for invalid; translate that to 0/0 for the model. + if (selection_base == -1 && selection_extent == -1) { + selection_base = selection_extent = 0; + } self->text_model->SetEditingState(selection_base, selection_extent, text); diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc index 46849b0672..43bdc50be4 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc @@ -189,9 +189,13 @@ void TextInputPlugin::HandleMethodCall( "Selection base/extent values invalid."); return; } - active_model_->SetEditingState(selection_base->value.GetInt(), - selection_extent->value.GetInt(), - text->value.GetString()); + // Flutter uses -1/-1 for invalid; translate that to 0/0 for the model. + int base = selection_base->value.GetInt(); + int extent = selection_extent->value.GetInt(); + if (base == -1 && extent == -1) { + base = extent = 0; + } + active_model_->SetEditingState(base, extent, text->value.GetString()); } else { result->NotImplemented(); return;