Win32: Support Korean input (flutter/engine#24713)

This change fixes a bug in Korean input whereby WM_IME_COMPOSITION
messages of type GCS_RESULTSTR were assumed to end composing mode.

This change breaks out an additional handler for "commit composing text"
events. In Japanese/Chinese IMEs, these events typically occur on
selection of a candidate from the candidates list and are mostly
synonymous with an "end composing" event.

In Korean text input, there is no candidates list, but rather a
character is built up as keypresses are handled, and committed as soon
as the character is unambiguously complete; in other words, when either
space/return is pressed or a keypress is received that cannot be
interpreted as a modification of the character being composed and
therefore must be the first keystroke of a new character. In these
cases, we want to commit the previous character without ending the
composition.

To illustrate with an example:
1. User focuses on a text field and sets input mode to Hangul.
2. User presses 'ㄱ'. Composing region contains 'ㄱ'.
3. User presses 'ㅏ'. Composing region is updated to '가'.
4. User presses 'ㄴ'. Composing region is updated to '간'.
5. User presses 'ㅏ'. Result string '가' is committed. Composing region is
   updated to '나'.
6. User presses 'ㄷ'. Composing string is updated to '낟'.
7. User presses 'ㅏ'. Result string '나' is committed. Composing region is
   updated to '다'.
8. User presses space or enter. Result string '다' is committed.
   Composing is ended.

On a non-Korean QWERTY keyboard the following key mappings serve to
perform the above input:
* r -> ㄱ
* k -> ㅏ
* s -> ㄴ
* e -> ㄷ

To support the above, we break out a separate "commit composing" method
and commit on WM_IME_COMPOSITION events of type GCS_RESULTSTR and end
composing on WM_IME_ENDCOMPOSITION events. Further, we eliminate the
workaround in the GCS_RESULTSTR handler for continued composition on
Chinese/Japanese IMEs now that we're no longer ending composition on
that event type.
This commit is contained in:
Chris Bracken
2021-03-02 10:14:27 -08:00
committed by GitHub
parent 263d0d1734
commit ee19dbeb1b
14 changed files with 69 additions and 9 deletions

View File

@@ -176,6 +176,10 @@ void FlutterWindowWin32::OnComposeBegin() {
binding_handler_delegate_->OnComposeBegin();
}
void FlutterWindowWin32::OnComposeCommit() {
binding_handler_delegate_->OnComposeCommit();
}
void FlutterWindowWin32::OnComposeEnd() {
binding_handler_delegate_->OnComposeEnd();
}

View File

@@ -65,6 +65,9 @@ class FlutterWindowWin32 : public WindowWin32, public WindowBindingHandler {
// |WindowWin32|
void OnComposeBegin() override;
// |WindowWin32|
void OnComposeCommit() override;
// |WindowWin32|
void OnComposeEnd() override;

View File

@@ -78,6 +78,7 @@ class SpyKeyboardKeyHandler : public KeyboardHandlerBase {
MOCK_METHOD2(TextHook,
void(FlutterWindowsView* window, const std::u16string& text));
MOCK_METHOD0(ComposeBeginHook, void());
MOCK_METHOD0(ComposeCommitHook, void());
MOCK_METHOD0(ComposeEndHook, void());
MOCK_METHOD2(ComposeChangeHook,
void(const std::u16string& text, int cursor_pos));
@@ -112,6 +113,7 @@ class SpyTextInputPlugin : public KeyboardHandlerBase,
MOCK_METHOD2(TextHook,
void(FlutterWindowsView* window, const std::u16string& text));
MOCK_METHOD0(ComposeBeginHook, void());
MOCK_METHOD0(ComposeCommitHook, void());
MOCK_METHOD0(ComposeEndHook, void());
MOCK_METHOD2(ComposeChangeHook,
void(const std::u16string& text, int cursor_pos));

View File

@@ -210,6 +210,10 @@ void FlutterWindowsView::OnComposeBegin() {
SendComposeBegin();
}
void FlutterWindowsView::OnComposeCommit() {
SendComposeCommit();
}
void FlutterWindowsView::OnComposeEnd() {
SendComposeEnd();
}
@@ -329,6 +333,12 @@ void FlutterWindowsView::SendComposeBegin() {
}
}
void FlutterWindowsView::SendComposeCommit() {
for (const auto& handler : keyboard_handlers_) {
handler->ComposeCommitHook();
}
}
void FlutterWindowsView::SendComposeEnd() {
for (const auto& handler : keyboard_handlers_) {
handler->ComposeEndHook();

View File

@@ -111,6 +111,9 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate,
// |WindowBindingHandlerDelegate|
void OnComposeBegin() override;
// |WindowBindingHandlerDelegate|
void OnComposeCommit() override;
// |WindowBindingHandlerDelegate|
void OnComposeEnd() override;
@@ -202,6 +205,13 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate,
// input method such as in CJK text input.
void SendComposeBegin();
// Reports an IME compose commit event.
//
// Triggered when the user commits the current composing text while using a
// multi-step input method such as in CJK text input. Composing continues with
// the next keypress.
void SendComposeCommit();
// Reports an IME compose end event.
//
// Triggered when the user commits the composing text while using a multi-step

View File

@@ -45,10 +45,17 @@ class KeyboardHandlerBase {
// input method such as in CJK text input.
virtual void ComposeBeginHook() = 0;
// Handler for IME compose commit events.
//
// Triggered when the user commits the current composing text while using a
// multi-step input method such as in CJK text input. Composing continues with
// the next keypress.
virtual void ComposeCommitHook() = 0;
// Handler for IME compose end events.
//
// Triggered when the user commits the composing text while using a multi-step
// input method such as in CJK text input.
// Triggered when the user ends editing composing text while using a
// multi-step input method such as in CJK text input.
virtual void ComposeEndHook() = 0;
// Handler for IME compose change events.

View File

@@ -181,6 +181,10 @@ void KeyboardKeyHandler::ComposeBeginHook() {
// Ignore.
}
void KeyboardKeyHandler::ComposeCommitHook() {
// Ignore.
}
void KeyboardKeyHandler::ComposeEndHook() {
// Ignore.
}

View File

@@ -107,6 +107,9 @@ class KeyboardKeyHandler : public KeyboardHandlerBase {
// |KeyboardHandlerBase|
void ComposeBeginHook() override;
// |KeyboardHandlerBase|
void ComposeCommitHook() override;
// |KeyboardHandlerBase|
void ComposeEndHook() override;

View File

@@ -42,6 +42,7 @@ class MockWin32Window : public WindowWin32 {
MOCK_METHOD6(OnKey, bool(int, int, int, char32_t, bool, bool));
MOCK_METHOD2(OnScroll, void(double, double));
MOCK_METHOD0(OnComposeBegin, void());
MOCK_METHOD0(OnComposeCommit, void());
MOCK_METHOD0(OnComposeEnd, void());
MOCK_METHOD2(OnComposeChange, void(const std::u16string&, int));
MOCK_METHOD4(DefaultWindowProc, LRESULT(HWND, UINT, WPARAM, LPARAM));

View File

@@ -111,6 +111,14 @@ void TextInputPlugin::ComposeBeginHook() {
SendStateUpdate(*active_model_);
}
void TextInputPlugin::ComposeCommitHook() {
if (active_model_ == nullptr) {
return;
}
active_model_->CommitComposing();
SendStateUpdate(*active_model_);
}
void TextInputPlugin::ComposeEndHook() {
if (active_model_ == nullptr) {
return;

View File

@@ -47,6 +47,9 @@ class TextInputPlugin : public KeyboardHandlerBase {
// |KeyboardHandlerBase|
void ComposeBeginHook() override;
// |KeyboardHandlerBase|
void ComposeCommitHook() override;
// |KeyboardHandlerBase|
void ComposeEndHook() override;

View File

@@ -59,6 +59,13 @@ class WindowBindingHandlerDelegate {
// input method such as in CJK text input.
virtual void OnComposeBegin() = 0;
// Notifies the delegate that IME composing region have been committed.
//
// Triggered when the user commits the current composing text while using a
// multi-step input method such as in CJK text input. Composing continues with
// the next keypress.
virtual void OnComposeCommit() = 0;
// Notifies the delegate that IME composing mode has ended.
//
// Triggered when the user commits the composing text while using a multi-step

View File

@@ -142,18 +142,13 @@ void WindowWin32::OnImeComposition(UINT const message,
OnComposeChange(text.value(), pos);
}
} else if (lparam & GCS_RESULTSTR) {
// Commit but don't end composing.
// Read the committed composing string.
long pos = text_input_manager_.GetComposingCursorPosition();
std::optional<std::u16string> text = text_input_manager_.GetResultString();
if (text) {
OnComposeChange(text.value(), pos);
}
// Next, try reading the composing string. Some Japanese IMEs send a message
// containing both a GCS_RESULTSTR and a GCS_COMPSTR when one composition is
// committed and another immediately started.
text = text_input_manager_.GetResultString();
if (text) {
OnComposeChange(text.value(), pos);
OnComposeCommit();
}
}
}

View File

@@ -107,6 +107,9 @@ class WindowWin32 {
// Called when IME composing begins.
virtual void OnComposeBegin() = 0;
// Called when IME composing text is committed.
virtual void OnComposeCommit() = 0;
// Called when IME composing ends.
virtual void OnComposeEnd() = 0;