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
Expand file tree
/
Copy pathtext_input_manager.cc
More file actions
139 lines (113 loc) · 3.71 KB
/
text_input_manager.cc
File metadata and controls
139 lines (113 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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.
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