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 |
55 TextInputClient* client = GetTextInputClient(); | |
54 // If no text input client, do nothing. | 56 // If no text input client, do nothing. |
55 if (!GetTextInputClient()) | 57 if (!client) |
56 return DispatchKeyEventPostIME(event); | 58 return DispatchKeyEventPostIME(event); |
57 | 59 |
58 // Let an IME handle the key event first, and allow to fire a VKEY_PROCESSKEY | 60 suppress_next_result_ = false; |
59 // event for keydown events. Note that DOM Level 3 Events Sepc requires that | 61 composition_changed_ = false; |
60 // only keydown events fire keyCode=229 events and not for keyup events. | 62 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 | 63 |
68 // Otherwise, insert the character. | 64 bool filtered = false; |
69 const bool handled = DispatchKeyEventPostIME(event); | 65 { |
70 if (event.type() == ET_KEY_PRESSED && GetTextInputClient()) { | 66 base::AutoReset<bool> flipper(&is_sync_mode_, true); |
71 const uint16 ch = event.GetCharacter(); | 67 if (text_input_type_ != TEXT_INPUT_TYPE_NONE && |
72 if (ch) { | 68 text_input_type_ != TEXT_INPUT_TYPE_PASSWORD) { |
73 GetTextInputClient()->InsertChar(ch, event.flags()); | 69 filtered = context_->DispatchKeyEvent(event); |
74 return true; | 70 } else { |
71 filtered = context_simple_->DispatchKeyEvent(event); | |
75 } | 72 } |
76 } | 73 } |
77 return handled; | 74 |
75 if (event.type() == ui::ET_KEY_PRESSED && filtered) { | |
76 if (NeedInsertChar()) | |
77 DispatchKeyEventPostIME(event); | |
78 else if (HasInputMethodResult()) | |
79 SendFakeProcessKeyEvent(event.flags()); | |
80 | |
81 // Don't send VKEY_PROCESSKEY event if there is no result text or | |
82 // composition. This is to workaround the weird behavior of IBus with US | |
83 // keyboard, which mutes the keydown and later fake a new keydown with IME | |
84 // result in sync mode. In that case, user would expect only | |
85 // keydown/keypress/keyup event without an initial 229 keydown event. | |
86 } | |
87 | |
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 (!result_text_.empty() && !composition_.text.empty()) | |
116 composition_.Clear(); | |
James Su
2015/04/12 14:50:33
According to our discussion, we need to keep compo
Shu Chen
2015/04/13 01:48:44
Done. Test also updated.
| |
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 base::char16 ch = event.GetCharacter(); | |
128 if (ch && client) | |
129 client->InsertChar(ch, event.flags()); | |
130 } | |
131 } | |
132 | |
133 return true; | |
134 } | |
135 | |
136 void InputMethodAuraLinux::UpdateContextFocusState() { | |
137 bool old_text_input_type = text_input_type_; | |
138 text_input_type_ = GetTextInputType(); | |
139 | |
140 // We only focus in |context_| when the focus is in a textfield. | |
141 if (old_text_input_type != TEXT_INPUT_TYPE_NONE && | |
142 text_input_type_ == TEXT_INPUT_TYPE_NONE) { | |
143 context_->Blur(); | |
144 } else if (old_text_input_type == TEXT_INPUT_TYPE_NONE && | |
145 text_input_type_ != TEXT_INPUT_TYPE_NONE) { | |
146 context_->Focus(); | |
147 } | |
148 | |
149 // |context_simple_| can be used in any textfield, including password box, and | |
150 // even if the focused text input client's text input type is | |
151 // ui::TEXT_INPUT_TYPE_NONE. | |
152 if (GetTextInputClient()) | |
153 context_simple_->Focus(); | |
154 else | |
155 context_simple_->Blur(); | |
78 } | 156 } |
79 | 157 |
80 void InputMethodAuraLinux::OnTextInputTypeChanged( | 158 void InputMethodAuraLinux::OnTextInputTypeChanged( |
81 const TextInputClient* client) { | 159 const TextInputClient* client) { |
82 if (!IsTextInputClientFocused(client)) | 160 UpdateContextFocusState(); |
83 return; | |
84 input_method_context_->Reset(); | |
85 // TODO(yoichio): Support inputmode HTML attribute. | 161 // TODO(yoichio): Support inputmode HTML attribute. |
86 input_method_context_->OnTextInputTypeChanged(client->GetTextInputType()); | |
87 } | 162 } |
88 | 163 |
89 void InputMethodAuraLinux::OnCaretBoundsChanged(const TextInputClient* client) { | 164 void InputMethodAuraLinux::OnCaretBoundsChanged(const TextInputClient* client) { |
90 if (!IsTextInputClientFocused(client)) | 165 if (!IsTextInputClientFocused(client)) |
91 return; | 166 return; |
92 input_method_context_->OnCaretBoundsChanged( | 167 context_->SetCursorLocation(GetTextInputClient()->GetCaretBounds()); |
93 GetTextInputClient()->GetCaretBounds()); | |
94 } | 168 } |
95 | 169 |
96 void InputMethodAuraLinux::CancelComposition(const TextInputClient* client) { | 170 void InputMethodAuraLinux::CancelComposition(const TextInputClient* client) { |
97 if (!IsTextInputClientFocused(client)) | 171 if (!IsTextInputClientFocused(client)) |
98 return; | 172 return; |
99 input_method_context_->Reset(); | 173 ResetContext(); |
100 input_method_context_->OnTextInputTypeChanged(client->GetTextInputType()); | 174 } |
175 | |
176 void InputMethodAuraLinux::ResetContext() { | |
177 if (!GetTextInputClient()) | |
178 return; | |
179 | |
180 // To prevent any text from being committed when resetting the |context_|; | |
181 is_sync_mode_ = true; | |
182 suppress_next_result_ = true; | |
183 | |
184 context_->Reset(); | |
185 context_simple_->Reset(); | |
186 | |
187 // Some input methods may not honour the reset call. Focusing out/in the | |
188 // |context_| to make sure it gets reset correctly. | |
189 if (text_input_type_ != TEXT_INPUT_TYPE_NONE) { | |
190 context_->Blur(); | |
191 context_->Focus(); | |
192 } | |
193 | |
194 composition_.Clear(); | |
195 result_text_.clear(); | |
196 is_sync_mode_ = false; | |
197 composition_changed_ = false; | |
101 } | 198 } |
102 | 199 |
103 void InputMethodAuraLinux::OnInputLocaleChanged() { | 200 void InputMethodAuraLinux::OnInputLocaleChanged() { |
104 } | 201 } |
105 | 202 |
106 std::string InputMethodAuraLinux::GetInputLocale() { | 203 std::string InputMethodAuraLinux::GetInputLocale() { |
107 return ""; | 204 return ""; |
108 } | 205 } |
109 | 206 |
110 bool InputMethodAuraLinux::IsActive() { | 207 bool InputMethodAuraLinux::IsActive() { |
111 // InputMethodAuraLinux is always ready and up. | 208 // InputMethodAuraLinux is always ready and up. |
112 return true; | 209 return true; |
113 } | 210 } |
114 | 211 |
115 bool InputMethodAuraLinux::IsCandidatePopupOpen() const { | 212 bool InputMethodAuraLinux::IsCandidatePopupOpen() const { |
116 // There seems no way to detect candidate windows or any popups. | 213 // There seems no way to detect candidate windows or any popups. |
117 return false; | 214 return false; |
118 } | 215 } |
119 | 216 |
120 // Overriden from ui::LinuxInputMethodContextDelegate | 217 // Overriden from ui::LinuxInputMethodContextDelegate |
121 | 218 |
122 void InputMethodAuraLinux::OnCommit(const base::string16& text) { | 219 void InputMethodAuraLinux::OnCommit(const base::string16& text) { |
123 MaybeFireProcessKey(); | 220 if (suppress_next_result_ || !GetTextInputClient()) { |
124 if (!IsTextInputTypeNone()) | 221 suppress_next_result_ = false; |
222 return; | |
223 } | |
224 | |
225 if (is_sync_mode_) { | |
226 // Append the text to the buffer, because commit signal might be fired | |
227 // multiple times when processing a key event. | |
228 result_text_.append(text); | |
229 } else if (!IsTextInputTypeNone()) { | |
230 // If we are not handling key event, do not bother sending text result if | |
231 // the focused text input client does not support text input. | |
232 SendFakeProcessKeyEvent(0); | |
125 GetTextInputClient()->InsertText(text); | 233 GetTextInputClient()->InsertText(text); |
234 composition_.Clear(); | |
235 } | |
126 } | 236 } |
127 | 237 |
128 void InputMethodAuraLinux::OnPreeditChanged( | 238 void InputMethodAuraLinux::OnPreeditChanged( |
129 const CompositionText& composition_text) { | 239 const CompositionText& composition_text) { |
130 MaybeFireProcessKey(); | 240 if (suppress_next_result_ || IsTextInputTypeNone()) |
131 TextInputClient* text_input_client = GetTextInputClient(); | 241 return; |
132 if (text_input_client) | 242 |
133 text_input_client->SetCompositionText(composition_text); | 243 composition_ = composition_text; |
244 | |
245 if (is_sync_mode_) { | |
246 if (!composition_.text.empty() || !composition_text.text.empty()) | |
247 composition_changed_ = true; | |
248 } else { | |
249 SendFakeProcessKeyEvent(0); | |
250 GetTextInputClient()->SetCompositionText(composition_text); | |
251 } | |
134 } | 252 } |
135 | 253 |
136 void InputMethodAuraLinux::OnPreeditEnd() { | 254 void InputMethodAuraLinux::OnPreeditEnd() { |
137 MaybeFireProcessKey(); | 255 if (suppress_next_result_ || IsTextInputTypeNone()) |
138 TextInputClient* text_input_client = GetTextInputClient(); | 256 return; |
139 if (text_input_client && text_input_client->HasCompositionText()) | |
140 text_input_client->ClearCompositionText(); | |
141 } | |
142 | 257 |
143 void InputMethodAuraLinux::OnPreeditStart() { | 258 if (is_sync_mode_) { |
144 MaybeFireProcessKey(); | 259 if (!composition_.text.empty()) { |
260 composition_.Clear(); | |
261 composition_changed_ = true; | |
262 } | |
263 } else { | |
264 TextInputClient* client = GetTextInputClient(); | |
265 if (client && client->HasCompositionText()) { | |
266 SendFakeProcessKeyEvent(0); | |
267 client->ClearCompositionText(); | |
268 } | |
269 composition_.Clear(); | |
270 } | |
145 } | 271 } |
146 | 272 |
147 // Overridden from InputMethodBase. | 273 // Overridden from InputMethodBase. |
148 | 274 |
275 void InputMethodAuraLinux::OnFocus() { | |
276 InputMethodBase::OnFocus(); | |
277 UpdateContextFocusState(); | |
278 } | |
279 | |
280 void InputMethodAuraLinux::OnBlur() { | |
281 ConfirmCompositionText(); | |
282 InputMethodBase::OnBlur(); | |
283 UpdateContextFocusState(); | |
284 } | |
285 | |
286 void InputMethodAuraLinux::OnWillChangeFocusedClient( | |
287 TextInputClient* focused_before, | |
288 TextInputClient* focused) { | |
289 ConfirmCompositionText(); | |
290 } | |
291 | |
149 void InputMethodAuraLinux::OnDidChangeFocusedClient( | 292 void InputMethodAuraLinux::OnDidChangeFocusedClient( |
150 TextInputClient* focused_before, | 293 TextInputClient* focused_before, |
151 TextInputClient* focused) { | 294 TextInputClient* focused) { |
152 input_method_context_->Reset(); | 295 UpdateContextFocusState(); |
153 input_method_context_->OnTextInputTypeChanged( | 296 |
154 focused ? focused->GetTextInputType() : TEXT_INPUT_TYPE_NONE); | 297 // Force to update caret bounds, in case the View thinks that the caret |
298 // bounds has not changed. | |
299 if (text_input_type_ != TEXT_INPUT_TYPE_NONE) | |
300 OnCaretBoundsChanged(GetTextInputClient()); | |
155 | 301 |
156 InputMethodBase::OnDidChangeFocusedClient(focused_before, focused); | 302 InputMethodBase::OnDidChangeFocusedClient(focused_before, focused); |
157 } | 303 } |
158 | 304 |
159 // Helper functions to support VKEY_PROCESSKEY. | 305 // private |
160 | 306 |
161 void InputMethodAuraLinux::AllowToFireProcessKey(const ui::KeyEvent& event) { | 307 bool InputMethodAuraLinux::HasInputMethodResult() { |
162 allowed_to_fire_vkey_process_key_ = true; | 308 return !result_text_.empty() || composition_changed_; |
163 vkey_processkey_flags_ = event.flags(); | |
164 } | 309 } |
165 | 310 |
166 void InputMethodAuraLinux::MaybeFireProcessKey() { | 311 bool InputMethodAuraLinux::NeedInsertChar() const { |
167 if (!allowed_to_fire_vkey_process_key_) | 312 return IsTextInputTypeNone() || |
168 return; | 313 (!composition_changed_ && composition_.text.empty() && |
169 | 314 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 } | 315 } |
176 | 316 |
177 void InputMethodAuraLinux::StopFiringProcessKey() { | 317 void InputMethodAuraLinux::SendFakeProcessKeyEvent(int flags) const { |
178 allowed_to_fire_vkey_process_key_ = false; | 318 DispatchKeyEventPostIME( |
179 vkey_processkey_flags_ = 0; | 319 KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, flags)); |
320 } | |
321 | |
322 void InputMethodAuraLinux::ConfirmCompositionText() { | |
323 TextInputClient* client = GetTextInputClient(); | |
324 if (client && client->HasCompositionText()) | |
325 client->ConfirmCompositionText(); | |
326 | |
327 ResetContext(); | |
180 } | 328 } |
181 | 329 |
182 } // namespace ui | 330 } // namespace ui |
OLD | NEW |