From 069154f7eea3a812d1a0bcdf4d32f663b1a1ff81 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 5 Mar 2021 17:51:33 +0100 Subject: [PATCH] [linux] Fix text selection via Shift+Home/End (flutter/engine#24623) Adds TextInputModel::SelectToBeginning/End() and applies when shift key is pressed. --- .../shell/platform/common/text_input_model.cc | 18 ++ .../shell/platform/common/text_input_model.h | 16 ++ .../common/text_input_model_unittests.cc | 232 ++++++++++++++++++ .../platform/linux/fl_text_input_plugin.cc | 12 +- 4 files changed, 276 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/shell/platform/common/text_input_model.cc b/engine/src/flutter/shell/platform/common/text_input_model.cc index 4e04775ff2..d6354650b2 100644 --- a/engine/src/flutter/shell/platform/common/text_input_model.cc +++ b/engine/src/flutter/shell/platform/common/text_input_model.cc @@ -239,6 +239,24 @@ bool TextInputModel::MoveCursorToEnd() { return true; } +bool TextInputModel::SelectToBeginning() { + size_t min_pos = editable_range().start(); + if (selection_.collapsed() && selection_.position() == min_pos) { + return false; + } + selection_ = TextRange(selection_.base(), min_pos); + return true; +} + +bool TextInputModel::SelectToEnd() { + size_t max_pos = editable_range().end(); + if (selection_.collapsed() && selection_.position() == max_pos) { + return false; + } + selection_ = TextRange(selection_.base(), max_pos); + return true; +} + bool TextInputModel::MoveCursorForward() { // If there's a selection, move to the end of the selection. if (!selection_.collapsed()) { diff --git a/engine/src/flutter/shell/platform/common/text_input_model.h b/engine/src/flutter/shell/platform/common/text_input_model.h index 7350c895b3..7704d36d97 100644 --- a/engine/src/flutter/shell/platform/common/text_input_model.h +++ b/engine/src/flutter/shell/platform/common/text_input_model.h @@ -155,6 +155,22 @@ class TextInputModel { // Returns true if the cursor could be moved. bool MoveCursorToEnd(); + // Attempts to select text from the cursor position to the beginning. + // + // If composing is active, the selection is applied to the beginning of the + // composing range; otherwise, it is applied to the beginning of the text. + // + // Returns true if the selection could be applied. + bool SelectToBeginning(); + + // Attempts to select text from the cursor position to the end. + // + // If composing is active, the selection is applied to the end of the + // composing range; otherwise, it is moved to the end of the text. + // + // Returns true if the selection could be applied. + bool SelectToEnd(); + // Gets the current text as UTF-8. std::string GetText() const; diff --git a/engine/src/flutter/shell/platform/common/text_input_model_unittests.cc b/engine/src/flutter/shell/platform/common/text_input_model_unittests.cc index d5879af2c8..c138fd1fac 100644 --- a/engine/src/flutter/shell/platform/common/text_input_model_unittests.cc +++ b/engine/src/flutter/shell/platform/common/text_input_model_unittests.cc @@ -1234,6 +1234,16 @@ TEST(TextInputModel, MoveCursorToBeginningStart) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningStart) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + EXPECT_TRUE(model->SetSelection(TextRange(0))); + EXPECT_FALSE(model->SelectToBeginning()); + EXPECT_EQ(model->selection(), TextRange(0)); + EXPECT_EQ(model->composing_range(), TextRange(0)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToBeginningMiddle) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1244,6 +1254,16 @@ TEST(TextInputModel, MoveCursorToBeginningMiddle) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningMiddle) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + EXPECT_TRUE(model->SetSelection(TextRange(2))); + EXPECT_TRUE(model->SelectToBeginning()); + EXPECT_EQ(model->selection(), TextRange(2, 0)); + EXPECT_EQ(model->composing_range(), TextRange(0)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToBeginningEnd) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1254,6 +1274,16 @@ TEST(TextInputModel, MoveCursorToBeginningEnd) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningEnd) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + EXPECT_TRUE(model->SetSelection(TextRange(5))); + EXPECT_TRUE(model->SelectToBeginning()); + EXPECT_EQ(model->selection(), TextRange(5, 0)); + EXPECT_EQ(model->composing_range(), TextRange(0)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToBeginningSelection) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1264,6 +1294,16 @@ TEST(TextInputModel, MoveCursorToBeginningSelection) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningSelection) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + EXPECT_TRUE(model->SetSelection(TextRange(1, 4))); + EXPECT_TRUE(model->SelectToBeginning()); + EXPECT_EQ(model->selection(), TextRange(1, 0)); + EXPECT_EQ(model->composing_range(), TextRange(0)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToBeginningReverseSelection) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1274,6 +1314,16 @@ TEST(TextInputModel, MoveCursorToBeginningReverseSelection) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningReverseSelection) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + EXPECT_TRUE(model->SetSelection(TextRange(4, 1))); + EXPECT_TRUE(model->SelectToBeginning()); + EXPECT_EQ(model->selection(), TextRange(4, 0)); + EXPECT_EQ(model->composing_range(), TextRange(0)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToBeginningStartComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1285,6 +1335,17 @@ TEST(TextInputModel, MoveCursorToBeginningStartComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningStartComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0)); + EXPECT_FALSE(model->SelectToBeginning()); + EXPECT_EQ(model->selection(), TextRange(1)); + EXPECT_EQ(model->composing_range(), TextRange(1, 4)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToBeginningStartReverseComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1296,6 +1357,17 @@ TEST(TextInputModel, MoveCursorToBeginningStartReverseComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningStartReverseComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 0)); + EXPECT_FALSE(model->SelectToBeginning()); + EXPECT_EQ(model->selection(), TextRange(1)); + EXPECT_EQ(model->composing_range(), TextRange(4, 1)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToBeginningMiddleComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1307,6 +1379,17 @@ TEST(TextInputModel, MoveCursorToBeginningMiddleComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningMiddleComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1)); + EXPECT_TRUE(model->SelectToBeginning()); + EXPECT_EQ(model->selection(), TextRange(2, 1)); + EXPECT_EQ(model->composing_range(), TextRange(1, 4)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToBeginningMiddleReverseComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1318,6 +1401,17 @@ TEST(TextInputModel, MoveCursorToBeginningMiddleReverseComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningMiddleReverseComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 1)); + EXPECT_TRUE(model->SelectToBeginning()); + EXPECT_EQ(model->selection(), TextRange(2, 1)); + EXPECT_EQ(model->composing_range(), TextRange(4, 1)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToBeginningEndComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1329,6 +1423,17 @@ TEST(TextInputModel, MoveCursorToBeginningEndComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningEndComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3)); + EXPECT_TRUE(model->MoveCursorToBeginning()); + EXPECT_EQ(model->selection(), TextRange(1)); + EXPECT_EQ(model->composing_range(), TextRange(1, 4)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToBeginningEndReverseComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1340,6 +1445,17 @@ TEST(TextInputModel, MoveCursorToBeginningEndReverseComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToBeginningEndReverseComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3)); + EXPECT_TRUE(model->SelectToBeginning()); + EXPECT_EQ(model->selection(), TextRange(4, 1)); + EXPECT_EQ(model->composing_range(), TextRange(4, 1)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndStart) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1350,6 +1466,16 @@ TEST(TextInputModel, MoveCursorToEndStart) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndStart) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + EXPECT_TRUE(model->SetSelection(TextRange(0))); + EXPECT_TRUE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(0, 5)); + EXPECT_EQ(model->composing_range(), TextRange(0)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndMiddle) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1360,6 +1486,16 @@ TEST(TextInputModel, MoveCursorToEndMiddle) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndMiddle) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + EXPECT_TRUE(model->SetSelection(TextRange(2))); + EXPECT_TRUE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(2, 5)); + EXPECT_EQ(model->composing_range(), TextRange(0)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndEnd) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1370,6 +1506,16 @@ TEST(TextInputModel, MoveCursorToEndEnd) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndEnd) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + EXPECT_TRUE(model->SetSelection(TextRange(5))); + EXPECT_FALSE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(5)); + EXPECT_EQ(model->composing_range(), TextRange(0)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndSelection) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1380,6 +1526,16 @@ TEST(TextInputModel, MoveCursorToEndSelection) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndSelection) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + EXPECT_TRUE(model->SetSelection(TextRange(1, 4))); + EXPECT_TRUE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(1, 5)); + EXPECT_EQ(model->composing_range(), TextRange(0)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndReverseSelection) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1390,6 +1546,16 @@ TEST(TextInputModel, MoveCursorToEndReverseSelection) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndReverseSelection) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + EXPECT_TRUE(model->SetSelection(TextRange(4, 1))); + EXPECT_TRUE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(4, 5)); + EXPECT_EQ(model->composing_range(), TextRange(0)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndStartComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1401,6 +1567,17 @@ TEST(TextInputModel, MoveCursorToEndStartComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndStartComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0)); + EXPECT_TRUE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(1, 4)); + EXPECT_EQ(model->composing_range(), TextRange(1, 4)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndStartReverseComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1412,6 +1589,17 @@ TEST(TextInputModel, MoveCursorToEndStartReverseComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndStartReverseComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 0)); + EXPECT_TRUE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(1, 4)); + EXPECT_EQ(model->composing_range(), TextRange(1, 4)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndMiddleComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1423,6 +1611,17 @@ TEST(TextInputModel, MoveCursorToEndMiddleComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndMiddleComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 1)); + EXPECT_TRUE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(2, 4)); + EXPECT_EQ(model->composing_range(), TextRange(1, 4)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndMiddleReverseComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1434,6 +1633,17 @@ TEST(TextInputModel, MoveCursorToEndMiddleReverseComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndMiddleReverseComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 1)); + EXPECT_TRUE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(2, 4)); + EXPECT_EQ(model->composing_range(), TextRange(4, 1)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndEndComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1445,6 +1655,17 @@ TEST(TextInputModel, MoveCursorToEndEndComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndEndComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(1, 4), 3)); + EXPECT_FALSE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(4)); + EXPECT_EQ(model->composing_range(), TextRange(1, 4)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, MoveCursorToEndEndReverseComposing) { auto model = std::make_unique(); model->SetText("ABCDE"); @@ -1456,6 +1677,17 @@ TEST(TextInputModel, MoveCursorToEndEndReverseComposing) { EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); } +TEST(TextInputModel, SelectToEndEndReverseComposing) { + auto model = std::make_unique(); + model->SetText("ABCDE"); + model->BeginComposing(); + EXPECT_TRUE(model->SetComposingRange(TextRange(4, 1), 3)); + EXPECT_FALSE(model->SelectToEnd()); + EXPECT_EQ(model->selection(), TextRange(4)); + EXPECT_EQ(model->composing_range(), TextRange(4, 1)); + EXPECT_STREQ(model->GetText().c_str(), "ABCDE"); +} + TEST(TextInputModel, GetCursorOffset) { auto model = std::make_unique(); // These characters take 1, 2, 3 and 4 bytes in UTF-8. 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 2e71dfb66c..2c4e4debe1 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 @@ -510,7 +510,11 @@ static gboolean fl_text_input_plugin_filter_keypress_default( switch (event->keyval) { case GDK_KEY_End: case GDK_KEY_KP_End: - changed = priv->text_model->MoveCursorToEnd(); + if (event->state & GDK_SHIFT_MASK) { + changed = priv->text_model->SelectToEnd(); + } else { + changed = priv->text_model->MoveCursorToEnd(); + } break; case GDK_KEY_Return: case GDK_KEY_KP_Enter: @@ -523,7 +527,11 @@ static gboolean fl_text_input_plugin_filter_keypress_default( break; case GDK_KEY_Home: case GDK_KEY_KP_Home: - changed = priv->text_model->MoveCursorToBeginning(); + if (event->state & GDK_SHIFT_MASK) { + changed = priv->text_model->SelectToBeginning(); + } else { + changed = priv->text_model->MoveCursorToBeginning(); + } break; case GDK_KEY_BackSpace: case GDK_KEY_Delete: