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/environment.h" | 7 #include "base/environment.h" |
8 #include "ui/base/ime/linux/linux_input_method_context_factory.h" | 8 #include "ui/base/ime/linux/linux_input_method_context_factory.h" |
9 #include "ui/base/ime/text_input_client.h" | 9 #include "ui/base/ime/text_input_client.h" |
10 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
11 | 11 |
12 namespace ui { | 12 namespace ui { |
13 | 13 |
14 namespace { | |
15 | |
16 class ScopedBooleanFlipper { | |
yukawa
2015/04/08 22:26:48
Can we use base::AutoReset in //src/base/auto_rese
Shu Chen
2015/04/09 01:16:32
Done.
| |
17 public: | |
18 ScopedBooleanFlipper(bool* v) : v_(v) { DCHECK(!*v_); *v_ = true; } | |
19 ~ScopedBooleanFlipper() { *v_ = false; } | |
20 private: | |
21 bool* v_; | |
22 }; | |
23 | |
24 } // namespace | |
25 | |
14 InputMethodAuraLinux::InputMethodAuraLinux( | 26 InputMethodAuraLinux::InputMethodAuraLinux( |
15 internal::InputMethodDelegate* delegate) | 27 internal::InputMethodDelegate* delegate) |
16 : allowed_to_fire_vkey_process_key_(false), vkey_processkey_flags_(0) { | 28 : context_focused_(false), handling_key_event_(false), |
29 composing_text_(false), composition_changed_(false), | |
30 suppress_next_result_(false) { | |
17 SetDelegate(delegate); | 31 SetDelegate(delegate); |
32 context_ = LinuxInputMethodContextFactory::instance()-> | |
33 CreateInputMethodContext(this, false); | |
34 context_simple_ = LinuxInputMethodContextFactory::instance()-> | |
35 CreateInputMethodContext(this, true); | |
18 } | 36 } |
19 | 37 |
20 InputMethodAuraLinux::~InputMethodAuraLinux() {} | 38 InputMethodAuraLinux::~InputMethodAuraLinux() {} |
21 | 39 |
40 LinuxInputMethodContext* InputMethodAuraLinux::GetContextForTesting( | |
41 bool is_simple) { | |
42 return is_simple ? context_simple_.get() : context_.get(); | |
43 } | |
44 | |
22 // Overriden from InputMethod. | 45 // Overriden from InputMethod. |
23 | 46 |
24 void InputMethodAuraLinux::Init(bool focused) { | 47 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); | 48 InputMethodBase::Init(focused); |
35 | 49 |
36 if (focused) { | 50 UpdateContextFocusState(); |
37 input_method_context_->OnTextInputTypeChanged( | |
38 GetTextInputClient() ? | |
39 GetTextInputClient()->GetTextInputType() : | |
40 TEXT_INPUT_TYPE_TEXT); | |
41 } | |
42 } | 51 } |
43 | 52 |
44 bool InputMethodAuraLinux::OnUntranslatedIMEMessage( | 53 bool InputMethodAuraLinux::OnUntranslatedIMEMessage( |
45 const base::NativeEvent& event, | 54 const base::NativeEvent& event, |
46 NativeEventResult* result) { | 55 NativeEventResult* result) { |
47 return false; | 56 return false; |
48 } | 57 } |
49 | 58 |
50 bool InputMethodAuraLinux::DispatchKeyEvent(const ui::KeyEvent& event) { | 59 bool InputMethodAuraLinux::DispatchKeyEvent(const ui::KeyEvent& event) { |
51 DCHECK(event.type() == ET_KEY_PRESSED || event.type() == ET_KEY_RELEASED); | 60 DCHECK(event.type() == ET_KEY_PRESSED || event.type() == ET_KEY_RELEASED); |
52 DCHECK(system_toplevel_window_focused()); | 61 DCHECK(system_toplevel_window_focused()); |
62 suppress_next_result_ = false; | |
53 | 63 |
54 // If no text input client, do nothing. | 64 // If no text input client, do nothing. |
55 if (!GetTextInputClient()) | 65 if (!GetTextInputClient()) |
56 return DispatchKeyEventPostIME(event); | 66 return DispatchKeyEventPostIME(event); |
57 | 67 |
58 // Let an IME handle the key event first, and allow to fire a VKEY_PROCESSKEY | 68 composition_changed_ = false; |
59 // event for keydown events. Note that DOM Level 3 Events Sepc requires that | 69 result_text_.clear(); |
60 // only keydown events fire keyCode=229 events and not for keyup events. | |
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 | 70 |
68 // Otherwise, insert the character. | 71 bool filtered = false; |
69 const bool handled = DispatchKeyEventPostIME(event); | 72 { |
70 if (event.type() == ET_KEY_PRESSED && GetTextInputClient()) { | 73 ScopedBooleanFlipper flipper(&handling_key_event_); |
71 const uint16 ch = event.GetCharacter(); | 74 if (context_focused_) |
72 if (ch) { | 75 filtered = context_->DispatchKeyEvent(event); |
73 GetTextInputClient()->InsertChar(ch, event.flags()); | 76 else |
74 return true; | 77 filtered = context_simple_->DispatchKeyEvent(event); |
78 } | |
79 | |
80 if (event.type() == ui::ET_KEY_PRESSED && filtered) { | |
81 if (NeedInsertChar()) { | |
82 DispatchKeyEventPostIME(event); | |
83 } else { | |
84 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, event.flags()); | |
85 DispatchKeyEventPostIME(key); | |
75 } | 86 } |
76 } | 87 } |
77 return handled; | 88 |
89 // If has input result process the result. | |
90 if (result_text_.length() || composition_changed_) | |
yukawa
2015/04/08 22:26:49
I slightly prefer string::empty(). How about this?
Shu Chen
2015/04/09 01:16:32
Done.
| |
91 ProcessInputMethodResult(event, filtered); | |
92 | |
93 if (event.type() == ui::ET_KEY_PRESSED && !filtered) { | |
94 DispatchKeyEventPostIME(event); | |
95 | |
96 // If a key event was not filtered by |context_| or |context_simple_|, then | |
97 // it means the key event didn't generate any result text. For some cases, | |
98 // the key event may still generate a valid character, eg. a control-key | |
99 // event (ctrl-a, return, tab, etc.). We need to send the character to the | |
100 // focused text input client by calling TextInputClient::InsertChar(). | |
101 base::char16 ch = event.GetCharacter(); | |
102 TextInputClient* client = GetTextInputClient(); | |
103 if (ch && client) | |
104 client->InsertChar(ch, event.flags()); | |
105 } else if (event.type() == ui::ET_KEY_RELEASED && !filtered) { | |
106 DispatchKeyEventPostIME(event); | |
107 } | |
James Su
2015/04/09 08:40:02
add a comment somewhere to clarify that filtered k
| |
108 | |
109 return true; | |
110 } | |
111 | |
112 void InputMethodAuraLinux::UpdateContextFocusState() { | |
113 bool old_context_focused = context_focused_; | |
114 // Use switch here in case we are going to add more text input types. | |
115 switch (GetTextInputType()) { | |
116 case ui::TEXT_INPUT_TYPE_NONE: | |
117 case ui::TEXT_INPUT_TYPE_PASSWORD: | |
Seigo Nonaka
2015/04/08 17:15:40
Probably off the topic but I just noticed the issu
yukawa
2015/04/08 22:26:49
Sounds good. But I think it's OK to try it later
Shu Chen
2015/04/09 01:16:33
I would also prefer to consider it in a separated
| |
118 context_focused_ = false; | |
119 break; | |
120 default: | |
121 context_focused_ = true; | |
122 break; | |
123 } | |
124 | |
125 // We only focus in |context_| when the focus is in a normal textfield. | |
126 if (old_context_focused && !context_focused_) | |
127 context_->Blur(); | |
128 else if (!old_context_focused && context_focused_) | |
129 context_->Focus(); | |
130 | |
131 // |context_simple_| can be used in any textfield, including password box, and | |
132 // even if the focused text input client's text input type is | |
133 // ui::TEXT_INPUT_TYPE_NONE. | |
134 if (GetTextInputClient()) | |
135 context_simple_->Focus(); | |
136 else | |
137 context_simple_->Blur(); | |
78 } | 138 } |
79 | 139 |
80 void InputMethodAuraLinux::OnTextInputTypeChanged( | 140 void InputMethodAuraLinux::OnTextInputTypeChanged( |
81 const TextInputClient* client) { | 141 const TextInputClient* client) { |
82 if (!IsTextInputClientFocused(client)) | 142 DCHECK(!composing_text_); |
83 return; | 143 UpdateContextFocusState(); |
84 input_method_context_->Reset(); | |
85 // TODO(yoichio): Support inputmode HTML attribute. | 144 // TODO(yoichio): Support inputmode HTML attribute. |
86 input_method_context_->OnTextInputTypeChanged(client->GetTextInputType()); | |
87 } | 145 } |
88 | 146 |
89 void InputMethodAuraLinux::OnCaretBoundsChanged(const TextInputClient* client) { | 147 void InputMethodAuraLinux::OnCaretBoundsChanged(const TextInputClient* client) { |
90 if (!IsTextInputClientFocused(client)) | 148 if (!IsTextInputClientFocused(client)) |
91 return; | 149 return; |
92 input_method_context_->OnCaretBoundsChanged( | 150 context_->SetCursorLocation(GetTextInputClient()->GetCaretBounds()); |
93 GetTextInputClient()->GetCaretBounds()); | |
94 } | 151 } |
95 | 152 |
96 void InputMethodAuraLinux::CancelComposition(const TextInputClient* client) { | 153 void InputMethodAuraLinux::CancelComposition(const TextInputClient* client) { |
97 if (!IsTextInputClientFocused(client)) | 154 if (!IsTextInputClientFocused(client)) |
98 return; | 155 return; |
99 input_method_context_->Reset(); | 156 ResetContext(); |
100 input_method_context_->OnTextInputTypeChanged(client->GetTextInputType()); | 157 } |
158 | |
159 void InputMethodAuraLinux::ResetContext() { | |
160 if (!GetTextInputClient()) | |
161 return; | |
162 | |
163 DCHECK(!handling_key_event_); | |
Seigo Nonaka
2015/04/08 17:15:40
ditto?
Shu Chen
2015/04/09 01:16:32
Done.
| |
164 | |
165 // To prevent any text from being committed when resetting the |context_|; | |
166 handling_key_event_ = true; | |
167 suppress_next_result_ = true; | |
168 | |
169 context_->Reset(); | |
170 context_simple_->Reset(); | |
171 | |
172 // Some input methods may not honour the reset call. Focusing out/in the | |
173 // |context_| to make sure it gets reset correctly. | |
174 if (context_focused_) { | |
175 context_->Blur(); | |
176 context_->Focus(); | |
177 } | |
178 | |
179 composition_.Clear(); | |
180 result_text_.clear(); | |
181 handling_key_event_ = false; | |
182 composing_text_ = false; | |
183 composition_changed_ = false; | |
101 } | 184 } |
102 | 185 |
103 void InputMethodAuraLinux::OnInputLocaleChanged() { | 186 void InputMethodAuraLinux::OnInputLocaleChanged() { |
104 } | 187 } |
105 | 188 |
106 std::string InputMethodAuraLinux::GetInputLocale() { | 189 std::string InputMethodAuraLinux::GetInputLocale() { |
107 return ""; | 190 return ""; |
108 } | 191 } |
109 | 192 |
110 bool InputMethodAuraLinux::IsActive() { | 193 bool InputMethodAuraLinux::IsActive() { |
111 // InputMethodAuraLinux is always ready and up. | 194 // InputMethodAuraLinux is always ready and up. |
112 return true; | 195 return true; |
113 } | 196 } |
114 | 197 |
115 bool InputMethodAuraLinux::IsCandidatePopupOpen() const { | 198 bool InputMethodAuraLinux::IsCandidatePopupOpen() const { |
116 // There seems no way to detect candidate windows or any popups. | 199 // There seems no way to detect candidate windows or any popups. |
117 return false; | 200 return false; |
118 } | 201 } |
119 | 202 |
120 // Overriden from ui::LinuxInputMethodContextDelegate | 203 // Overriden from ui::LinuxInputMethodContextDelegate |
121 | 204 |
122 void InputMethodAuraLinux::OnCommit(const base::string16& text) { | 205 void InputMethodAuraLinux::OnCommit(const base::string16& text) { |
123 MaybeFireProcessKey(); | 206 if (suppress_next_result_) { |
124 if (!IsTextInputTypeNone()) | 207 suppress_next_result_ = false; |
208 return; | |
209 } | |
210 if (!GetTextInputClient()) | |
211 return; | |
212 | |
213 if (!composing_text_ && !text.length()) | |
yukawa
2015/04/08 22:26:48
I slightly prefer string::empty(). How about this?
Shu Chen
2015/04/09 01:16:32
Done.
| |
214 return; | |
215 | |
216 // Append the text to the buffer, because commit signal might be fired | |
217 // multiple times when processing a key event. | |
218 result_text_.append(text); | |
219 | |
220 // If we are not handling key event, do not bother sending text result if the | |
221 // focused text input client does not support text input. | |
222 if (!handling_key_event_ && !IsTextInputTypeNone()) { | |
223 SendFakeProcessKeyEvent(true); | |
125 GetTextInputClient()->InsertText(text); | 224 GetTextInputClient()->InsertText(text); |
225 } | |
226 } | |
227 | |
228 void InputMethodAuraLinux::OnPreeditStart() { | |
229 if (suppress_next_result_ || IsTextInputTypeNone()) | |
230 return; | |
231 | |
232 composing_text_ = true; | |
126 } | 233 } |
127 | 234 |
128 void InputMethodAuraLinux::OnPreeditChanged( | 235 void InputMethodAuraLinux::OnPreeditChanged( |
129 const CompositionText& composition_text) { | 236 const CompositionText& composition_text) { |
130 MaybeFireProcessKey(); | 237 if (suppress_next_result_ || IsTextInputTypeNone()) |
131 TextInputClient* text_input_client = GetTextInputClient(); | 238 return; |
132 if (text_input_client) | 239 |
133 text_input_client->SetCompositionText(composition_text); | 240 if (!composition_.text.length() && !composition_text.text.length()) |
yukawa
2015/04/08 22:26:48
I slightly prefer string::empty(). How about this?
Shu Chen
2015/04/09 01:16:32
Done.
| |
241 return; | |
242 | |
243 composition_ = composition_text; | |
244 composition_changed_ = true; | |
245 if (composition_.text.length()) | |
yukawa
2015/04/08 22:26:48
I slightly prefer string::empty(). How about this?
Shu Chen
2015/04/09 01:16:32
Done.
| |
246 composing_text_ = true; | |
247 | |
248 if (!handling_key_event_ && !IsTextInputTypeNone()) { | |
249 SendFakeProcessKeyEvent(true); | |
250 GetTextInputClient()->SetCompositionText(composition_); | |
251 } | |
134 } | 252 } |
135 | 253 |
136 void InputMethodAuraLinux::OnPreeditEnd() { | 254 void InputMethodAuraLinux::OnPreeditEnd() { |
137 MaybeFireProcessKey(); | 255 if (composition_.text.empty() || 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 composition_changed_ = true; |
144 MaybeFireProcessKey(); | 259 composition_.Clear(); |
260 | |
261 if (!handling_key_event_) { | |
262 TextInputClient* client = GetTextInputClient(); | |
263 if (client && client->HasCompositionText()) | |
264 client->ClearCompositionText(); | |
265 } | |
145 } | 266 } |
146 | 267 |
147 // Overridden from InputMethodBase. | 268 // Overridden from InputMethodBase. |
148 | 269 |
270 void InputMethodAuraLinux::OnFocus() { | |
271 InputMethodBase::OnFocus(); | |
272 UpdateContextFocusState(); | |
273 } | |
274 | |
275 void InputMethodAuraLinux::OnBlur() { | |
276 ConfirmCompositionText(); | |
277 InputMethodBase::OnBlur(); | |
278 UpdateContextFocusState(); | |
279 } | |
280 | |
281 void InputMethodAuraLinux::OnWillChangeFocusedClient( | |
282 TextInputClient* focused_before, | |
283 TextInputClient* focused) { | |
284 ConfirmCompositionText(); | |
285 } | |
286 | |
149 void InputMethodAuraLinux::OnDidChangeFocusedClient( | 287 void InputMethodAuraLinux::OnDidChangeFocusedClient( |
150 TextInputClient* focused_before, | 288 TextInputClient* focused_before, |
151 TextInputClient* focused) { | 289 TextInputClient* focused) { |
152 input_method_context_->Reset(); | 290 UpdateContextFocusState(); |
153 input_method_context_->OnTextInputTypeChanged( | 291 |
154 focused ? focused->GetTextInputType() : TEXT_INPUT_TYPE_NONE); | 292 // Force to update caret bounds, in case the View thinks that the caret |
293 // bounds has not changed. | |
294 if (context_focused_) | |
295 OnCaretBoundsChanged(GetTextInputClient()); | |
155 | 296 |
156 InputMethodBase::OnDidChangeFocusedClient(focused_before, focused); | 297 InputMethodBase::OnDidChangeFocusedClient(focused_before, focused); |
157 } | 298 } |
158 | 299 |
159 // Helper functions to support VKEY_PROCESSKEY. | 300 // private |
160 | 301 |
161 void InputMethodAuraLinux::AllowToFireProcessKey(const ui::KeyEvent& event) { | 302 void InputMethodAuraLinux::ProcessInputMethodResult(const KeyEvent& key, |
162 allowed_to_fire_vkey_process_key_ = true; | 303 bool filtered) { |
163 vkey_processkey_flags_ = event.flags(); | 304 TextInputClient* client = GetTextInputClient(); |
305 DCHECK(client); | |
306 | |
307 if (result_text_.length()) { | |
yukawa
2015/04/08 22:26:48
I slightly prefer string::empty(). How about this?
Shu Chen
2015/04/09 01:16:32
Done.
| |
308 if (filtered && NeedInsertChar()) { | |
309 for (base::string16::const_iterator i = result_text_.begin(); | |
yukawa
2015/04/08 22:26:48
How about using range-based for?
for (const auto
Shu Chen
2015/04/09 01:16:32
Done.
| |
310 i != result_text_.end(); ++i) { | |
311 client->InsertChar(*i, key.flags()); | |
312 } | |
313 } else { | |
314 client->InsertText(result_text_); | |
315 composing_text_ = false; | |
316 } | |
317 } | |
318 | |
319 if (composition_changed_ && !IsTextInputTypeNone()) { | |
320 if (composition_.text.length()) { | |
yukawa
2015/04/08 22:26:49
I slightly prefer string::empty(). How about this?
Shu Chen
2015/04/09 01:16:32
Done.
| |
321 composing_text_ = true; | |
322 client->SetCompositionText(composition_); | |
323 } else if (result_text_.empty()) { | |
324 client->ClearCompositionText(); | |
325 } | |
326 } | |
164 } | 327 } |
165 | 328 |
166 void InputMethodAuraLinux::MaybeFireProcessKey() { | 329 bool InputMethodAuraLinux::NeedInsertChar() const { |
167 if (!allowed_to_fire_vkey_process_key_) | 330 return IsTextInputTypeNone() || |
168 return; | 331 (!composing_text_ && result_text_.length() == 1); |
169 | |
170 const ui::KeyEvent fabricated_event(ET_KEY_PRESSED, | |
171 VKEY_PROCESSKEY, | |
172 vkey_processkey_flags_); | |
173 DispatchKeyEventPostIME(fabricated_event); | |
174 StopFiringProcessKey(); | |
175 } | 332 } |
176 | 333 |
177 void InputMethodAuraLinux::StopFiringProcessKey() { | 334 void InputMethodAuraLinux::SendFakeProcessKeyEvent(bool pressed) const { |
178 allowed_to_fire_vkey_process_key_ = false; | 335 KeyEvent key(pressed ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED, |
179 vkey_processkey_flags_ = 0; | 336 ui::VKEY_PROCESSKEY, 0); |
337 DispatchKeyEventPostIME(key); | |
338 } | |
339 | |
340 void InputMethodAuraLinux::ConfirmCompositionText() { | |
341 TextInputClient* client = GetTextInputClient(); | |
342 if (client && client->HasCompositionText()) | |
343 client->ConfirmCompositionText(); | |
344 | |
345 ResetContext(); | |
180 } | 346 } |
181 | 347 |
182 } // namespace ui | 348 } // namespace ui |
OLD | NEW |