This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Add support for IME-based text input on Windows #23853
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/shell/platform/windows/text_input_manager.h" | ||
|
|
||
| #include <imm.h> | ||
|
|
||
| #include <memory> | ||
|
|
||
| namespace flutter { | ||
|
|
||
| void TextInputManager::SetWindowHandle(HWND window_handle) { | ||
| window_handle_ = window_handle; | ||
| } | ||
|
|
||
| void TextInputManager::CreateImeWindow() { | ||
| if (window_handle_ == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| // Some IMEs ignore calls to ::ImmSetCandidateWindow() and use the position of | ||
| // the current system caret instead via ::GetCaretPos(). In order to behave | ||
| // as expected with these IMEs, we create a temporary system caret. | ||
| if (!ime_active_) { | ||
| ::CreateCaret(window_handle_, nullptr, 1, 1); | ||
| } | ||
| ime_active_ = true; | ||
|
|
||
| // Set the position of the IME windows. | ||
| UpdateImeWindow(); | ||
| } | ||
|
|
||
| void TextInputManager::DestroyImeWindow() { | ||
| if (window_handle_ == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| // Destroy the system caret created in CreateImeWindow(). | ||
| if (ime_active_) { | ||
| ::DestroyCaret(); | ||
| } | ||
| ime_active_ = false; | ||
| } | ||
|
|
||
| void TextInputManager::UpdateImeWindow() { | ||
| if (window_handle_ == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| HIMC imm_context = ::ImmGetContext(window_handle_); | ||
| if (imm_context) { | ||
| MoveImeWindow(imm_context); | ||
| ::ImmReleaseContext(window_handle_, imm_context); | ||
| } | ||
| } | ||
|
|
||
| void TextInputManager::UpdateCaretRect(const Rect& rect) { | ||
| caret_rect_ = rect; | ||
|
|
||
| if (window_handle_ == nullptr) { | ||
| return; | ||
| } | ||
|
|
||
| // TODO(cbracken): wrap these in an RAII container. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Big +1 on this. |
||
| HIMC imm_context = ::ImmGetContext(window_handle_); | ||
| if (imm_context) { | ||
| MoveImeWindow(imm_context); | ||
| ::ImmReleaseContext(window_handle_, imm_context); | ||
| } | ||
| } | ||
|
|
||
| long TextInputManager::GetComposingCursorPosition() const { | ||
| if (window_handle_ == nullptr) { | ||
| return false; | ||
| } | ||
|
|
||
| HIMC imm_context = ::ImmGetContext(window_handle_); | ||
| if (imm_context) { | ||
| // Read the cursor position within the composing string. | ||
| const int pos = | ||
| ImmGetCompositionStringW(imm_context, GCS_CURSORPOS, nullptr, 0); | ||
| ::ImmReleaseContext(window_handle_, imm_context); | ||
| return pos; | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| std::optional<std::u16string> TextInputManager::GetComposingString() const { | ||
| return GetString(GCS_COMPSTR); | ||
| } | ||
|
|
||
| std::optional<std::u16string> TextInputManager::GetResultString() const { | ||
| return GetString(GCS_RESULTSTR); | ||
| } | ||
|
|
||
| std::optional<std::u16string> TextInputManager::GetString(int type) const { | ||
| if (window_handle_ == nullptr || !ime_active_) { | ||
| return std::nullopt; | ||
| } | ||
| HIMC imm_context = ::ImmGetContext(window_handle_); | ||
| if (imm_context) { | ||
| // Read the composing string length. | ||
| const long compose_bytes = | ||
| ::ImmGetCompositionString(imm_context, type, nullptr, 0); | ||
| const long compose_length = compose_bytes / sizeof(wchar_t); | ||
| if (compose_length <= 0) { | ||
| ::ImmReleaseContext(window_handle_, imm_context); | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| std::u16string text(compose_length, '\0'); | ||
| ::ImmGetCompositionString(imm_context, type, &text[0], compose_bytes); | ||
| ::ImmReleaseContext(window_handle_, imm_context); | ||
| return text; | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| void TextInputManager::MoveImeWindow(HIMC imm_context) { | ||
| if (GetFocus() != window_handle_ || !ime_active_) { | ||
| return; | ||
| } | ||
| LONG x = caret_rect_.left(); | ||
| LONG y = caret_rect_.top(); | ||
| ::SetCaretPos(x, y); | ||
|
|
||
| COMPOSITIONFORM cf = {CFS_POINT, {x, y}}; | ||
| ::ImmSetCompositionWindow(imm_context, &cf); | ||
|
|
||
| CANDIDATEFORM candidate_form = {0, CFS_CANDIDATEPOS, {x, y}, {0, 0, 0, 0}}; | ||
| ::ImmSetCandidateWindow(imm_context, &candidate_form); | ||
| } | ||
|
|
||
| } // namespace flutter | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.