From b7f187c7d2933da6e96656b1acd2ee856d49e73c Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 4 Mar 2022 13:31:06 -0800 Subject: [PATCH] TextEditingDelta Windows (flutter/engine#30329) Support for TextEditingDeltas (useful for rich text editors) on Windows. --- .../platform/windows/text_input_plugin.cc | 98 ++++++++++++++++++- .../platform/windows/text_input_plugin.h | 11 +++ .../windows/text_input_plugin_unittest.cc | 64 +++++++++++- 3 files changed, 167 insertions(+), 6 deletions(-) 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 543b0c0023..8e2badaf77 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc @@ -3,6 +3,8 @@ // found in the LICENSE file. #include "flutter/shell/platform/windows/text_input_plugin.h" +#include "flutter/fml/string_conversion.h" +#include "flutter/shell/platform/common/text_editing_delta.h" #include @@ -23,8 +25,16 @@ static constexpr char kMultilineInputType[] = "TextInputType.multiline"; static constexpr char kUpdateEditingStateMethod[] = "TextInputClient.updateEditingState"; +static constexpr char kUpdateEditingStateWithDeltasMethod[] = + "TextInputClient.updateEditingStateWithDeltas"; static constexpr char kPerformActionMethod[] = "TextInputClient.performAction"; +static constexpr char kDeltaOldTextKey[] = "oldText"; +static constexpr char kDeltaTextKey[] = "deltaText"; +static constexpr char kDeltaStartKey[] = "deltaStart"; +static constexpr char kDeltaEndKey[] = "deltaEnd"; +static constexpr char kDeltasKey[] = "deltas"; +static constexpr char kEnableDeltaModel[] = "enableDeltaModel"; static constexpr char kTextInputAction[] = "inputAction"; static constexpr char kTextInputType[] = "inputType"; static constexpr char kTextInputTypeName[] = "name"; @@ -54,8 +64,18 @@ void TextInputPlugin::TextHook(const std::u16string& text) { if (active_model_ == nullptr) { return; } + std::u16string text_before_change = + fml::Utf8ToUtf16(active_model_->GetText()); + TextRange selection_before_change = active_model_->selection(); active_model_->AddText(text); - SendStateUpdate(*active_model_); + + if (enable_delta_model) { + TextEditingDelta delta = + TextEditingDelta(text_before_change, selection_before_change, text); + SendStateUpdateWithDelta(*active_model_, &delta); + } else { + SendStateUpdate(*active_model_); + } } void TextInputPlugin::KeyboardHook(int key, @@ -103,13 +123,25 @@ void TextInputPlugin::ComposeBeginHook() { return; } active_model_->BeginComposing(); - SendStateUpdate(*active_model_); + if (enable_delta_model) { + std::string text = active_model_->GetText(); + TextRange selection = active_model_->selection(); + TextEditingDelta delta = TextEditingDelta(text); + SendStateUpdateWithDelta(*active_model_, &delta); + } else { + SendStateUpdate(*active_model_); + } } void TextInputPlugin::ComposeCommitHook() { if (active_model_ == nullptr) { return; } + std::string text_before_change = active_model_->GetText(); + TextRange selection_before_change = active_model_->selection(); + TextRange composing_before_change = active_model_->composing_range(); + std::string composing_text_before_change = text_before_change.substr( + composing_before_change.start(), composing_before_change.length()); active_model_->CommitComposing(); // We do not trigger SendStateUpdate here. @@ -140,9 +172,17 @@ void TextInputPlugin::ComposeEndHook() { if (active_model_ == nullptr) { return; } + std::string text_before_change = active_model_->GetText(); + TextRange selection_before_change = active_model_->selection(); active_model_->CommitComposing(); active_model_->EndComposing(); - SendStateUpdate(*active_model_); + if (enable_delta_model) { + std::string text = active_model_->GetText(); + TextEditingDelta delta = TextEditingDelta(text); + SendStateUpdateWithDelta(*active_model_, &delta); + } else { + SendStateUpdate(*active_model_); + } } void TextInputPlugin::ComposeChangeHook(const std::u16string& text, @@ -150,11 +190,20 @@ void TextInputPlugin::ComposeChangeHook(const std::u16string& text, if (active_model_ == nullptr) { return; } + std::string text_before_change = active_model_->GetText(); + TextRange composing_before_change = active_model_->composing_range(); active_model_->AddText(text); - cursor_pos += active_model_->composing_range().base(); + cursor_pos += active_model_->composing_range().extent(); active_model_->UpdateComposingText(text); active_model_->SetSelection(TextRange(cursor_pos, cursor_pos)); - SendStateUpdate(*active_model_); + std::string text_after_change = active_model_->GetText(); + if (enable_delta_model) { + TextEditingDelta delta = TextEditingDelta( + fml::Utf8ToUtf16(text_before_change), composing_before_change, text); + SendStateUpdateWithDelta(*active_model_, &delta); + } else { + SendStateUpdate(*active_model_); + } } void TextInputPlugin::HandleMethodCall( @@ -191,6 +240,11 @@ void TextInputPlugin::HandleMethodCall( return; } client_id_ = client_id_json.GetInt(); + auto enable_delta_model_json = client_config.FindMember(kEnableDeltaModel); + if (enable_delta_model_json != client_config.MemberEnd() && + enable_delta_model_json->value.IsBool()) { + enable_delta_model = enable_delta_model_json->value.GetBool(); + } input_action_ = ""; auto input_action_json = client_config.FindMember(kTextInputAction); if (input_action_json != client_config.MemberEnd() && @@ -356,6 +410,40 @@ void TextInputPlugin::SendStateUpdate(const TextInputModel& model) { channel_->InvokeMethod(kUpdateEditingStateMethod, std::move(args)); } +void TextInputPlugin::SendStateUpdateWithDelta(const TextInputModel& model, + const TextEditingDelta* delta) { + auto args = std::make_unique(rapidjson::kArrayType); + auto& allocator = args->GetAllocator(); + args->PushBack(client_id_, allocator); + + rapidjson::Value object(rapidjson::kObjectType); + rapidjson::Value deltas(rapidjson::kArrayType); + rapidjson::Value deltaJson(rapidjson::kObjectType); + + deltaJson.AddMember(kDeltaOldTextKey, delta->old_text(), allocator); + deltaJson.AddMember(kDeltaTextKey, delta->delta_text(), allocator); + deltaJson.AddMember(kDeltaStartKey, delta->delta_start(), allocator); + deltaJson.AddMember(kDeltaEndKey, delta->delta_end(), allocator); + + TextRange selection = model.selection(); + deltaJson.AddMember(kSelectionAffinityKey, kAffinityDownstream, allocator); + deltaJson.AddMember(kSelectionBaseKey, selection.base(), allocator); + deltaJson.AddMember(kSelectionExtentKey, selection.extent(), allocator); + deltaJson.AddMember(kSelectionIsDirectionalKey, false, allocator); + + int composing_base = model.composing() ? model.composing_range().base() : -1; + int composing_extent = + model.composing() ? model.composing_range().extent() : -1; + deltaJson.AddMember(kComposingBaseKey, composing_base, allocator); + deltaJson.AddMember(kComposingExtentKey, composing_extent, allocator); + + deltas.PushBack(deltaJson, allocator); + object.AddMember(kDeltasKey, deltas, allocator); + args->PushBack(object, allocator); + + channel_->InvokeMethod(kUpdateEditingStateWithDeltasMethod, std::move(args)); +} + void TextInputPlugin::EnterPressed(TextInputModel* model) { if (input_type_ == kMultilineInputType) { model->AddText(std::u16string({u'\n'})); diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin.h b/engine/src/flutter/shell/platform/windows/text_input_plugin.h index ad8a131b9f..e9c36c3966 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.h +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.h @@ -13,6 +13,7 @@ #include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" #include "flutter/shell/platform/common/geometry.h" #include "flutter/shell/platform/common/json_method_codec.h" +#include "flutter/shell/platform/common/text_editing_delta.h" #include "flutter/shell/platform/common/text_input_model.h" #include "flutter/shell/platform/windows/keyboard_handler_base.h" #include "flutter/shell/platform/windows/text_input_plugin_delegate.h" @@ -50,6 +51,10 @@ class TextInputPlugin { // Sends the current state of the given model to the Flutter engine. void SendStateUpdate(const TextInputModel& model); + // Sends the current state of the given model to the Flutter engine. + void SendStateUpdateWithDelta(const TextInputModel& model, + const TextEditingDelta*); + // Sends an action triggered by the Enter key to the Flutter engine. void EnterPressed(TextInputModel* model); @@ -74,6 +79,12 @@ class TextInputPlugin { // The active model. nullptr if not set. std::unique_ptr active_model_; + // Whether to enable that the engine sends text input updates to the framework + // as TextEditingDeltas or as one TextEditingValue. + // For more information on the delta model, see: + // https://master-api.flutter.dev/flutter/services/TextInputConfiguration/enableDeltaModel.html + bool enable_delta_model; + // Keyboard type of the client. See available options: // https://api.flutter.dev/flutter/services/TextInputType-class.html std::string input_type_; diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin_unittest.cc b/engine/src/flutter/shell/platform/windows/text_input_plugin_unittest.cc index 30af07d96e..e48eea90a7 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin_unittest.cc +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin_unittest.cc @@ -20,6 +20,11 @@ namespace { static constexpr char kScanCodeKey[] = "scanCode"; static constexpr int kHandledScanCode = 20; static constexpr int kUnhandledScanCode = 21; +static constexpr char kTextPlainFormat[] = "text/plain"; +// Should be identical to constants in text_input_plugin.cc. +static constexpr char kChannelName[] = "flutter/textinput"; +static constexpr char kEnableDeltaModel[] = "enableDeltaModel"; +static constexpr char kSetClientMethod[] = "TextInput.setClient"; static std::unique_ptr> CreateResponse(bool handled) { auto response_doc = @@ -76,7 +81,7 @@ TEST(TextInputPluginTest, ClearClientResetsComposing) { auto& codec = JsonMethodCodec::GetInstance(); auto message = codec.EncodeMethodCall({"TextInput.clearClient", nullptr}); - messenger.SimulateEngineMessage("flutter/textinput", message->data(), + messenger.SimulateEngineMessage(kChannelName, message->data(), message->size(), reply_handler); EXPECT_TRUE(delegate.ime_was_reset()); } @@ -103,6 +108,7 @@ TEST(TextInputPluginTest, VerifyComposingSendStateUpdate) { rapidjson::Value config(rapidjson::kObjectType); config.AddMember("inputAction", "done", allocator); config.AddMember("inputType", "text", allocator); + config.AddMember(kEnableDeltaModel, false, allocator); arguments->PushBack(config, allocator); auto message = codec.EncodeMethodCall({"TextInput.setClient", std::move(arguments)}); @@ -137,5 +143,61 @@ TEST(TextInputPluginTest, VerifyComposingSendStateUpdate) { EXPECT_TRUE(sent_message); } +TEST(TextInputPluginTest, TextEditingWorksWithDeltaModel) { + auto handled_message = CreateResponse(true); + auto unhandled_message = CreateResponse(false); + int received_scancode = 0; + + TestBinaryMessenger messenger( + [&received_scancode, &handled_message, &unhandled_message]( + const std::string& channel, const uint8_t* message, + size_t message_size, BinaryReply reply) {}); + EmptyTextInputPluginDelegate delegate; + + int redispatch_scancode = 0; + TextInputPlugin handler(&messenger, &delegate); + + auto args = std::make_unique(rapidjson::kArrayType); + auto& allocator = args->GetAllocator(); + args->PushBack(123, allocator); // client_id + + rapidjson::Value client_config(rapidjson::kObjectType); + client_config.AddMember(kEnableDeltaModel, true, allocator); + + args->PushBack(client_config, allocator); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(kSetClientMethod, std::move(args))); + + EXPECT_TRUE(messenger.SimulateEngineMessage( + kChannelName, encoded->data(), encoded->size(), + [](const uint8_t* reply, size_t reply_size) {})); + + handler.KeyboardHook(VK_RETURN, 100, WM_KEYDOWN, '\n', false, false); + handler.ComposeBeginHook(); + std::u16string text; + text.push_back('\n'); + handler.ComposeChangeHook(text, 1); + handler.ComposeEndHook(); + + handler.KeyboardHook(0x4E, 100, WM_KEYDOWN, 'n', false, false); + handler.ComposeBeginHook(); + std::u16string textN; + text.push_back('n'); + handler.ComposeChangeHook(textN, 1); + handler.KeyboardHook(0x49, 100, WM_KEYDOWN, 'i', false, false); + std::u16string textNi; + text.push_back('n'); + text.push_back('i'); + handler.ComposeChangeHook(textNi, 2); + handler.KeyboardHook(VK_RETURN, 100, WM_KEYDOWN, '\n', false, false); + std::u16string textChineseCharacter; + text.push_back(u'\u4F60'); + handler.ComposeChangeHook(textChineseCharacter, 1); + handler.ComposeCommitHook(); + handler.ComposeEndHook(); + + // Passes if it did not crash +} + } // namespace testing } // namespace flutter