diff --git a/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc b/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc index 35bc07592f..48fba3894a 100644 --- a/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc +++ b/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc @@ -4,6 +4,7 @@ #include "flutter/shell/platform/common/cpp/text_input_model.h" +#include #include #include #include @@ -73,8 +74,13 @@ bool TextInputModel::SetEditingState(size_t selection_base, } std::wstring_convert, char32_t> utf32conv; text_ = utf32conv.from_bytes(text); - selection_base_ = text_.begin() + selection_base; - selection_extent_ = text_.begin() + selection_extent; + // selection_base and selection_extent are relative to UTF-16, so don't + // necessarily line up. The full fix would be to use UTF-16 here so + // the models align, but for now just make sure it doesn't crash; + // longer term the goal is to eliminate having two sources of truth. + size_t length = text_.length(); + selection_base_ = text_.begin() + std::min(selection_base, length); + selection_extent_ = text_.begin() + std::min(selection_extent, length); return true; } diff --git a/engine/src/flutter/shell/platform/windows/win32_window.cc b/engine/src/flutter/shell/platform/windows/win32_window.cc index 6f8c48e9c0..11f4b4c021 100644 --- a/engine/src/flutter/shell/platform/windows/win32_window.cc +++ b/engine/src/flutter/shell/platform/windows/win32_window.cc @@ -196,29 +196,29 @@ Win32Window::MessageHandler(HWND hwnd, case WM_CHAR: case WM_SYSCHAR: { char32_t code_point = static_cast(wparam); - static char32_t lead_surrogate = 0; + static char32_t s_pending_lead_surrogate = 0; // If code_point is LeadSurrogate, save to combine to potentially form // a complex Unicode character. if ((code_point & 0xFFFFFC00) == 0xD800) { - lead_surrogate = code_point; - } else if (lead_surrogate != 0 && (code_point & 0xFFFFFC00) == 0xDC00) { + s_pending_lead_surrogate = code_point; + } else if (s_pending_lead_surrogate != 0 && + (code_point & 0xFFFFFC00) == 0xDC00) { // Merge TrailSurrogate and LeadSurrogate. - code_point = 0x10000 + ((lead_surrogate & 0x000003FF) << 10) + + code_point = 0x10000 + + ((s_pending_lead_surrogate & 0x000003FF) << 10) + (code_point & 0x3FF); - lead_surrogate = 0; + s_pending_lead_surrogate = 0; } - // In an ENG-INTL keyboard, pressing "'" + "e" produces é. In this case, - // the "'" key is a dead char, and shouldn't be sent to window->OnChar - // for text input. However, the key event should still be sent to - // Flutter. The result would be: - // * Key event - key code: 222 (quote) - key label: ' - // * Key event - key code: 69 (e) - key label: é - // - // As for text input, only the second key press will display a - // character. - if (wparam != VK_BACK && message != WM_DEADCHAR && - message != WM_SYSDEADCHAR) { + // Of the messages handled here, only WM_CHAR should be treated as + // characters. WM_SYS*CHAR are not part of text input, and WM_DEADCHAR + // will be incorporated into a later WM_CHAR with the full character. + // Also filter out: + // - Lead surrogates, which like dead keys will be send once combined. + // - ASCII control characters, which are sent as WM_CHAR events for all + // control key shortcuts. + if (message == WM_CHAR && code_point >= U' ' && + s_pending_lead_surrogate == 0) { window->OnChar(code_point); }