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