OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/ime/input_method_auralinux.h" | 5 #include "ui/base/ime/input_method_auralinux.h" |
6 | 6 |
| 7 #include "base/auto_reset.h" |
7 #include "base/environment.h" | 8 #include "base/environment.h" |
8 #include "ui/base/ime/linux/linux_input_method_context_factory.h" | 9 #include "ui/base/ime/linux/linux_input_method_context_factory.h" |
9 #include "ui/base/ime/text_input_client.h" | 10 #include "ui/base/ime/text_input_client.h" |
10 #include "ui/events/event.h" | 11 #include "ui/events/event.h" |
11 | 12 |
12 namespace ui { | 13 namespace ui { |
13 | 14 |
14 InputMethodAuraLinux::InputMethodAuraLinux( | 15 InputMethodAuraLinux::InputMethodAuraLinux( |
15 internal::InputMethodDelegate* delegate) | 16 internal::InputMethodDelegate* delegate) |
16 : allowed_to_fire_vkey_process_key_(false), vkey_processkey_flags_(0) { | 17 : text_input_type_(TEXT_INPUT_TYPE_NONE), |
| 18 is_sync_mode_(false), |
| 19 composition_changed_(false), |
| 20 suppress_next_result_(false) { |
17 SetDelegate(delegate); | 21 SetDelegate(delegate); |
| 22 context_ = |
| 23 LinuxInputMethodContextFactory::instance()->CreateInputMethodContext( |
| 24 this, false); |
| 25 context_simple_ = |
| 26 LinuxInputMethodContextFactory::instance()->CreateInputMethodContext( |
| 27 this, true); |
18 } | 28 } |
19 | 29 |
20 InputMethodAuraLinux::~InputMethodAuraLinux() {} | 30 InputMethodAuraLinux::~InputMethodAuraLinux() {} |
21 | 31 |
| 32 LinuxInputMethodContext* InputMethodAuraLinux::GetContextForTesting( |
| 33 bool is_simple) { |
| 34 return is_simple ? context_simple_.get() : context_.get(); |
| 35 } |
| 36 |
22 // Overriden from InputMethod. | 37 // Overriden from InputMethod. |
23 | 38 |
24 void InputMethodAuraLinux::Init(bool focused) { | 39 void InputMethodAuraLinux::Init(bool focused) { |
25 CHECK(LinuxInputMethodContextFactory::instance()) | |
26 << "This failure was likely caused because " | |
27 << "ui::InitializeInputMethod(ForTesting) was not called " | |
28 << "before instantiating this class."; | |
29 input_method_context_ = | |
30 LinuxInputMethodContextFactory::instance()->CreateInputMethodContext( | |
31 this); | |
32 CHECK(input_method_context_.get()); | |
33 | |
34 InputMethodBase::Init(focused); | 40 InputMethodBase::Init(focused); |
35 | 41 |
36 if (focused) { | 42 UpdateContextFocusState(); |
37 input_method_context_->OnTextInputTypeChanged( | |
38 GetTextInputClient() ? | |
39 GetTextInputClient()->GetTextInputType() : | |
40 TEXT_INPUT_TYPE_TEXT); | |
41 } | |
42 } | 43 } |
43 | 44 |
44 bool InputMethodAuraLinux::OnUntranslatedIMEMessage( | 45 bool InputMethodAuraLinux::OnUntranslatedIMEMessage( |
45 const base::NativeEvent& event, | 46 const base::NativeEvent& event, |
46 NativeEventResult* result) { | 47 NativeEventResult* result) { |
47 return false; | 48 return false; |
48 } | 49 } |
49 | 50 |
50 bool InputMethodAuraLinux::DispatchKeyEvent(const ui::KeyEvent& event) { | 51 bool InputMethodAuraLinux::DispatchKeyEvent(const ui::KeyEvent& event) { |
51 DCHECK(event.type() == ET_KEY_PRESSED || event.type() == ET_KEY_RELEASED); | 52 DCHECK(event.type() == ET_KEY_PRESSED || event.type() == ET_KEY_RELEASED); |
52 DCHECK(system_toplevel_window_focused()); | 53 DCHECK(system_toplevel_window_focused()); |
53 | 54 |
54 // If no text input client, do nothing. | 55 // If no text input client, do nothing. |
55 if (!GetTextInputClient()) | 56 if (!GetTextInputClient()) |
56 return DispatchKeyEventPostIME(event); | 57 return DispatchKeyEventPostIME(event); |
57 | 58 |
58 // Let an IME handle the key event first, and allow to fire a VKEY_PROCESSKEY | 59 suppress_next_result_ = false; |
59 // event for keydown events. Note that DOM Level 3 Events Sepc requires that | 60 composition_changed_ = false; |
60 // only keydown events fire keyCode=229 events and not for keyup events. | 61 result_text_.clear(); |
61 if (event.type() == ET_KEY_PRESSED && | |
62 (event.flags() & ui::EF_IME_FABRICATED_KEY) == 0) | |
63 AllowToFireProcessKey(event); | |
64 if (input_method_context_->DispatchKeyEvent(event)) | |
65 return true; | |
66 StopFiringProcessKey(); | |
67 | 62 |
68 // Otherwise, insert the character. | 63 bool filtered = false; |
69 const bool handled = DispatchKeyEventPostIME(event); | 64 { |
70 if (event.type() == ET_KEY_PRESSED && GetTextInputClient()) { | 65 base::AutoReset<bool> flipper(&is_sync_mode_, true); |
71 const uint16 ch = event.GetCharacter(); | 66 if (text_input_type_ != TEXT_INPUT_TYPE_NONE && |
72 if (ch) { | 67 text_input_type_ != TEXT_INPUT_TYPE_PASSWORD) { |
73 GetTextInputClient()->InsertChar(ch, event.flags()); | 68 filtered = context_->DispatchKeyEvent(event); |
74 return true; | 69 } else { |
| 70 filtered = context_simple_->DispatchKeyEvent(event); |
75 } | 71 } |
76 } | 72 } |
77 return handled; | 73 |
| 74 if (event.type() == ui::ET_KEY_PRESSED && filtered) { |
| 75 if (NeedInsertChar()) |
| 76 DispatchKeyEventPostIME(event); |
| 77 else if (HasInputMethodResult()) |
| 78 SendFakeProcessKeyEvent(event.flags()); |
| 79 |
| 80 // Don't send VKEY_PROCESSKEY event if there is no result text or |
| 81 // composition. This is to workaround the weird behavior of IBus with US |
| 82 // keyboard, which mutes the keydown and later fake a new keydown with IME |
| 83 // result in sync mode. In that case, user would expect only |
| 84 // keydown/keypress/keyup event without an initial 229 keydown event. |
| 85 } |
| 86 |
| 87 TextInputClient* client = GetTextInputClient(); |
| 88 // Processes the result text before composition for sync mode. |
| 89 if (!result_text_.empty()) { |
| 90 if (filtered && NeedInsertChar()) { |
| 91 for (const auto ch : result_text_) |
| 92 client->InsertChar(ch, event.flags()); |
| 93 } else { |
| 94 // If |filtered| is false, that means the IME wants to commit some text |
| 95 // but still release the key to the application. For example, Korean IME |
| 96 // handles ENTER key to confirm its composition but still release it for |
| 97 // the default behavior (e.g. trigger search, etc.) |
| 98 // In such case, don't do InsertChar because a key should only trigger the |
| 99 // keydown event once. |
| 100 client->InsertText(result_text_); |
| 101 } |
| 102 } |
| 103 |
| 104 if (composition_changed_ && !IsTextInputTypeNone()) { |
| 105 // If composition changed, does SetComposition if composition is not empty. |
| 106 // And ClearComposition if composition is empty. |
| 107 if (!composition_.text.empty()) |
| 108 client->SetCompositionText(composition_); |
| 109 else if (result_text_.empty()) |
| 110 client->ClearCompositionText(); |
| 111 } |
| 112 |
| 113 // Makes sure the cached composition is cleared after committing any text or |
| 114 // cleared composition. |
| 115 if (!client->HasCompositionText()) |
| 116 composition_.Clear(); |
| 117 |
| 118 if (!filtered) { |
| 119 DispatchKeyEventPostIME(event); |
| 120 if (event.type() == ui::ET_KEY_PRESSED) { |
| 121 // If a key event was not filtered by |context_| or |context_simple_|, |
| 122 // then it means the key event didn't generate any result text. For some |
| 123 // cases, the key event may still generate a valid character, eg. a |
| 124 // control-key event (ctrl-a, return, tab, etc.). We need to send the |
| 125 // character to the focused text input client by calling |
| 126 // TextInputClient::InsertChar(). |
| 127 // Note: don't use |client| and use GetTextInputClient() here because |
| 128 // DispatchKeyEventPostIME may cause the current text input client change. |
| 129 base::char16 ch = event.GetCharacter(); |
| 130 if (ch && GetTextInputClient()) |
| 131 GetTextInputClient()->InsertChar(ch, event.flags()); |
| 132 } |
| 133 } |
| 134 |
| 135 return true; |
| 136 } |
| 137 |
| 138 void InputMethodAuraLinux::UpdateContextFocusState() { |
| 139 bool old_text_input_type = text_input_type_; |
| 140 text_input_type_ = GetTextInputType(); |
| 141 |
| 142 // We only focus in |context_| when the focus is in a textfield. |
| 143 if (old_text_input_type != TEXT_INPUT_TYPE_NONE && |
| 144 text_input_type_ == TEXT_INPUT_TYPE_NONE) { |
| 145 context_->Blur(); |
| 146 } else if (old_text_input_type == TEXT_INPUT_TYPE_NONE && |
| 147 text_input_type_ != TEXT_INPUT_TYPE_NONE) { |
| 148 context_->Focus(); |
| 149 } |
| 150 |
| 151 // |context_simple_| can be used in any textfield, including password box, and |
| 152 // even if the focused text input client's text input type is |
| 153 // ui::TEXT_INPUT_TYPE_NONE. |
| 154 if (GetTextInputClient()) |
| 155 context_simple_->Focus(); |
| 156 else |
| 157 context_simple_->Blur(); |
78 } | 158 } |
79 | 159 |
80 void InputMethodAuraLinux::OnTextInputTypeChanged( | 160 void InputMethodAuraLinux::OnTextInputTypeChanged( |
81 const TextInputClient* client) { | 161 const TextInputClient* client) { |
82 if (!IsTextInputClientFocused(client)) | 162 UpdateContextFocusState(); |
83 return; | |
84 input_method_context_->Reset(); | |
85 // TODO(yoichio): Support inputmode HTML attribute. | 163 // TODO(yoichio): Support inputmode HTML attribute. |
86 input_method_context_->OnTextInputTypeChanged(client->GetTextInputType()); | |
87 } | 164 } |
88 | 165 |
89 void InputMethodAuraLinux::OnCaretBoundsChanged(const TextInputClient* client) { | 166 void InputMethodAuraLinux::OnCaretBoundsChanged(const TextInputClient* client) { |
90 if (!IsTextInputClientFocused(client)) | 167 if (!IsTextInputClientFocused(client)) |
91 return; | 168 return; |
92 input_method_context_->OnCaretBoundsChanged( | 169 context_->SetCursorLocation(GetTextInputClient()->GetCaretBounds()); |
93 GetTextInputClient()->GetCaretBounds()); | |
94 } | 170 } |
95 | 171 |
96 void InputMethodAuraLinux::CancelComposition(const TextInputClient* client) { | 172 void InputMethodAuraLinux::CancelComposition(const TextInputClient* client) { |
97 if (!IsTextInputClientFocused(client)) | 173 if (!IsTextInputClientFocused(client)) |
98 return; | 174 return; |
99 input_method_context_->Reset(); | 175 ResetContext(); |
100 input_method_context_->OnTextInputTypeChanged(client->GetTextInputType()); | 176 } |
| 177 |
| 178 void InputMethodAuraLinux::ResetContext() { |
| 179 if (!GetTextInputClient()) |
| 180 return; |
| 181 |
| 182 // To prevent any text from being committed when resetting the |context_|; |
| 183 is_sync_mode_ = true; |
| 184 suppress_next_result_ = true; |
| 185 |
| 186 context_->Reset(); |
| 187 context_simple_->Reset(); |
| 188 |
| 189 // Some input methods may not honour the reset call. Focusing out/in the |
| 190 // |context_| to make sure it gets reset correctly. |
| 191 if (text_input_type_ != TEXT_INPUT_TYPE_NONE) { |
| 192 context_->Blur(); |
| 193 context_->Focus(); |
| 194 } |
| 195 |
| 196 composition_.Clear(); |
| 197 result_text_.clear(); |
| 198 is_sync_mode_ = false; |
| 199 composition_changed_ = false; |
101 } | 200 } |
102 | 201 |
103 void InputMethodAuraLinux::OnInputLocaleChanged() { | 202 void InputMethodAuraLinux::OnInputLocaleChanged() { |
104 } | 203 } |
105 | 204 |
106 std::string InputMethodAuraLinux::GetInputLocale() { | 205 std::string InputMethodAuraLinux::GetInputLocale() { |
107 return ""; | 206 return ""; |
108 } | 207 } |
109 | 208 |
110 bool InputMethodAuraLinux::IsActive() { | 209 bool InputMethodAuraLinux::IsActive() { |
111 // InputMethodAuraLinux is always ready and up. | 210 // InputMethodAuraLinux is always ready and up. |
112 return true; | 211 return true; |
113 } | 212 } |
114 | 213 |
115 bool InputMethodAuraLinux::IsCandidatePopupOpen() const { | 214 bool InputMethodAuraLinux::IsCandidatePopupOpen() const { |
116 // There seems no way to detect candidate windows or any popups. | 215 // There seems no way to detect candidate windows or any popups. |
117 return false; | 216 return false; |
118 } | 217 } |
119 | 218 |
120 // Overriden from ui::LinuxInputMethodContextDelegate | 219 // Overriden from ui::LinuxInputMethodContextDelegate |
121 | 220 |
122 void InputMethodAuraLinux::OnCommit(const base::string16& text) { | 221 void InputMethodAuraLinux::OnCommit(const base::string16& text) { |
123 MaybeFireProcessKey(); | 222 if (suppress_next_result_ || !GetTextInputClient()) { |
124 if (!IsTextInputTypeNone()) | 223 suppress_next_result_ = false; |
| 224 return; |
| 225 } |
| 226 |
| 227 if (is_sync_mode_) { |
| 228 // Append the text to the buffer, because commit signal might be fired |
| 229 // multiple times when processing a key event. |
| 230 result_text_.append(text); |
| 231 } else if (!IsTextInputTypeNone()) { |
| 232 // If we are not handling key event, do not bother sending text result if |
| 233 // the focused text input client does not support text input. |
| 234 SendFakeProcessKeyEvent(0); |
125 GetTextInputClient()->InsertText(text); | 235 GetTextInputClient()->InsertText(text); |
| 236 composition_.Clear(); |
| 237 } |
126 } | 238 } |
127 | 239 |
128 void InputMethodAuraLinux::OnPreeditChanged( | 240 void InputMethodAuraLinux::OnPreeditChanged( |
129 const CompositionText& composition_text) { | 241 const CompositionText& composition_text) { |
130 MaybeFireProcessKey(); | 242 if (suppress_next_result_ || IsTextInputTypeNone()) |
131 TextInputClient* text_input_client = GetTextInputClient(); | 243 return; |
132 if (text_input_client) | 244 |
133 text_input_client->SetCompositionText(composition_text); | 245 composition_ = composition_text; |
| 246 |
| 247 if (is_sync_mode_) { |
| 248 if (!composition_.text.empty() || !composition_text.text.empty()) |
| 249 composition_changed_ = true; |
| 250 } else { |
| 251 SendFakeProcessKeyEvent(0); |
| 252 GetTextInputClient()->SetCompositionText(composition_text); |
| 253 } |
134 } | 254 } |
135 | 255 |
136 void InputMethodAuraLinux::OnPreeditEnd() { | 256 void InputMethodAuraLinux::OnPreeditEnd() { |
137 MaybeFireProcessKey(); | 257 if (suppress_next_result_ || IsTextInputTypeNone()) |
138 TextInputClient* text_input_client = GetTextInputClient(); | 258 return; |
139 if (text_input_client && text_input_client->HasCompositionText()) | |
140 text_input_client->ClearCompositionText(); | |
141 } | |
142 | 259 |
143 void InputMethodAuraLinux::OnPreeditStart() { | 260 if (is_sync_mode_) { |
144 MaybeFireProcessKey(); | 261 if (!composition_.text.empty()) { |
| 262 composition_.Clear(); |
| 263 composition_changed_ = true; |
| 264 } |
| 265 } else { |
| 266 TextInputClient* client = GetTextInputClient(); |
| 267 if (client && client->HasCompositionText()) { |
| 268 SendFakeProcessKeyEvent(0); |
| 269 client->ClearCompositionText(); |
| 270 } |
| 271 composition_.Clear(); |
| 272 } |
145 } | 273 } |
146 | 274 |
147 // Overridden from InputMethodBase. | 275 // Overridden from InputMethodBase. |
148 | 276 |
| 277 void InputMethodAuraLinux::OnFocus() { |
| 278 InputMethodBase::OnFocus(); |
| 279 UpdateContextFocusState(); |
| 280 } |
| 281 |
| 282 void InputMethodAuraLinux::OnBlur() { |
| 283 ConfirmCompositionText(); |
| 284 InputMethodBase::OnBlur(); |
| 285 UpdateContextFocusState(); |
| 286 } |
| 287 |
| 288 void InputMethodAuraLinux::OnWillChangeFocusedClient( |
| 289 TextInputClient* focused_before, |
| 290 TextInputClient* focused) { |
| 291 ConfirmCompositionText(); |
| 292 } |
| 293 |
149 void InputMethodAuraLinux::OnDidChangeFocusedClient( | 294 void InputMethodAuraLinux::OnDidChangeFocusedClient( |
150 TextInputClient* focused_before, | 295 TextInputClient* focused_before, |
151 TextInputClient* focused) { | 296 TextInputClient* focused) { |
152 input_method_context_->Reset(); | 297 UpdateContextFocusState(); |
153 input_method_context_->OnTextInputTypeChanged( | 298 |
154 focused ? focused->GetTextInputType() : TEXT_INPUT_TYPE_NONE); | 299 // Force to update caret bounds, in case the View thinks that the caret |
| 300 // bounds has not changed. |
| 301 if (text_input_type_ != TEXT_INPUT_TYPE_NONE) |
| 302 OnCaretBoundsChanged(GetTextInputClient()); |
155 | 303 |
156 InputMethodBase::OnDidChangeFocusedClient(focused_before, focused); | 304 InputMethodBase::OnDidChangeFocusedClient(focused_before, focused); |
157 } | 305 } |
158 | 306 |
159 // Helper functions to support VKEY_PROCESSKEY. | 307 // private |
160 | 308 |
161 void InputMethodAuraLinux::AllowToFireProcessKey(const ui::KeyEvent& event) { | 309 bool InputMethodAuraLinux::HasInputMethodResult() { |
162 allowed_to_fire_vkey_process_key_ = true; | 310 return !result_text_.empty() || composition_changed_; |
163 vkey_processkey_flags_ = event.flags(); | |
164 } | 311 } |
165 | 312 |
166 void InputMethodAuraLinux::MaybeFireProcessKey() { | 313 bool InputMethodAuraLinux::NeedInsertChar() const { |
167 if (!allowed_to_fire_vkey_process_key_) | 314 return IsTextInputTypeNone() || |
168 return; | 315 (!composition_changed_ && composition_.text.empty() && |
169 | 316 result_text_.length() == 1); |
170 const ui::KeyEvent fabricated_event(ET_KEY_PRESSED, | |
171 VKEY_PROCESSKEY, | |
172 vkey_processkey_flags_); | |
173 DispatchKeyEventPostIME(fabricated_event); | |
174 StopFiringProcessKey(); | |
175 } | 317 } |
176 | 318 |
177 void InputMethodAuraLinux::StopFiringProcessKey() { | 319 void InputMethodAuraLinux::SendFakeProcessKeyEvent(int flags) const { |
178 allowed_to_fire_vkey_process_key_ = false; | 320 DispatchKeyEventPostIME( |
179 vkey_processkey_flags_ = 0; | 321 KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, flags)); |
| 322 } |
| 323 |
| 324 void InputMethodAuraLinux::ConfirmCompositionText() { |
| 325 TextInputClient* client = GetTextInputClient(); |
| 326 if (client && client->HasCompositionText()) |
| 327 client->ConfirmCompositionText(); |
| 328 |
| 329 ResetContext(); |
180 } | 330 } |
181 | 331 |
182 } // namespace ui | 332 } // namespace ui |
OLD | NEW |