Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: ui/base/ime/input_method_auralinux.cc

Issue 1257603006: Refactoring for the InputMethod & InputMethodDelegate interfaces. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/ime/input_method_auralinux.h ('k') | ui/base/ime/input_method_auralinux_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/auto_reset.h"
8 #include "base/environment.h" 8 #include "base/environment.h"
9 #include "ui/base/ime/linux/linux_input_method_context_factory.h" 9 #include "ui/base/ime/linux/linux_input_method_context_factory.h"
10 #include "ui/base/ime/text_input_client.h" 10 #include "ui/base/ime/text_input_client.h"
11 #include "ui/events/event.h" 11 #include "ui/events/event.h"
12 12
13 namespace ui { 13 namespace ui {
14 14
15 InputMethodAuraLinux::InputMethodAuraLinux( 15 InputMethodAuraLinux::InputMethodAuraLinux(
16 internal::InputMethodDelegate* delegate) 16 internal::InputMethodDelegate* delegate)
17 : text_input_type_(TEXT_INPUT_TYPE_NONE), 17 : text_input_type_(TEXT_INPUT_TYPE_NONE),
18 is_sync_mode_(false), 18 is_sync_mode_(false),
19 composition_changed_(false), 19 composition_changed_(false),
20 suppress_next_result_(false), 20 suppress_next_result_(false) {
21 destroyed_ptr_(nullptr) {
22 SetDelegate(delegate); 21 SetDelegate(delegate);
23 context_ = 22 context_ =
24 LinuxInputMethodContextFactory::instance()->CreateInputMethodContext( 23 LinuxInputMethodContextFactory::instance()->CreateInputMethodContext(
25 this, false); 24 this, false);
26 context_simple_ = 25 context_simple_ =
27 LinuxInputMethodContextFactory::instance()->CreateInputMethodContext( 26 LinuxInputMethodContextFactory::instance()->CreateInputMethodContext(
28 this, true); 27 this, true);
29 } 28 }
30 29
31 InputMethodAuraLinux::~InputMethodAuraLinux() { 30 InputMethodAuraLinux::~InputMethodAuraLinux() {
32 if (destroyed_ptr_)
33 *destroyed_ptr_ = true;
34 } 31 }
35 32
36 LinuxInputMethodContext* InputMethodAuraLinux::GetContextForTesting( 33 LinuxInputMethodContext* InputMethodAuraLinux::GetContextForTesting(
37 bool is_simple) { 34 bool is_simple) {
38 return is_simple ? context_simple_.get() : context_.get(); 35 return is_simple ? context_simple_.get() : context_.get();
39 } 36 }
40 37
41 // Overriden from InputMethod. 38 // Overriden from InputMethod.
42 39
43 bool InputMethodAuraLinux::OnUntranslatedIMEMessage( 40 bool InputMethodAuraLinux::OnUntranslatedIMEMessage(
44 const base::NativeEvent& event, 41 const base::NativeEvent& event,
45 NativeEventResult* result) { 42 NativeEventResult* result) {
46 return false; 43 return false;
47 } 44 }
48 45
49 bool InputMethodAuraLinux::DispatchKeyEvent(const ui::KeyEvent& event) { 46 void InputMethodAuraLinux::DispatchKeyEvent(ui::KeyEvent* event) {
50 DCHECK(event.type() == ET_KEY_PRESSED || event.type() == ET_KEY_RELEASED); 47 DCHECK(event->type() == ET_KEY_PRESSED || event->type() == ET_KEY_RELEASED);
51 DCHECK(system_toplevel_window_focused()); 48 DCHECK(system_toplevel_window_focused());
52 49
53 // If no text input client, do nothing. 50 // If no text input client, do nothing.
54 if (!GetTextInputClient()) 51 if (!GetTextInputClient()) {
55 return DispatchKeyEventPostIME(event); 52 ignore_result(DispatchKeyEventPostIME(event));
53 return;
54 }
56 55
57 suppress_next_result_ = false; 56 suppress_next_result_ = false;
58 composition_changed_ = false; 57 composition_changed_ = false;
59 result_text_.clear(); 58 result_text_.clear();
60 59
61 bool filtered = false; 60 bool filtered = false;
62 { 61 {
63 base::AutoReset<bool> flipper(&is_sync_mode_, true); 62 base::AutoReset<bool> flipper(&is_sync_mode_, true);
64 if (text_input_type_ != TEXT_INPUT_TYPE_NONE && 63 if (text_input_type_ != TEXT_INPUT_TYPE_NONE &&
65 text_input_type_ != TEXT_INPUT_TYPE_PASSWORD) { 64 text_input_type_ != TEXT_INPUT_TYPE_PASSWORD) {
66 filtered = context_->DispatchKeyEvent(event); 65 filtered = context_->DispatchKeyEvent(*event);
67 } else { 66 } else {
68 filtered = context_simple_->DispatchKeyEvent(event); 67 filtered = context_simple_->DispatchKeyEvent(*event);
69 } 68 }
70 } 69 }
71 70
72 bool destroyed = false; 71 ui::EventDispatchDetails details;
73 bool handled = false; 72 if (event->type() == ui::ET_KEY_PRESSED && filtered) {
74 if (event.type() == ui::ET_KEY_PRESSED && filtered) { 73 if (NeedInsertChar())
75 { 74 details = DispatchKeyEventPostIME(event);
76 base::AutoReset<bool*> auto_reset(&destroyed_ptr_, &destroyed); 75 else if (HasInputMethodResult())
77 if (NeedInsertChar()) 76 details = SendFakeProcessKeyEvent(event);
78 handled = DispatchKeyEventPostIME(event); 77 if (details.dispatcher_destroyed)
79 else if (HasInputMethodResult()) 78 return;
80 handled = SendFakeProcessKeyEvent(event.flags());
81 if (destroyed)
82 return true;
83 }
84 // If the KEYDOWN is stopped propagation (e.g. triggered an accelerator), 79 // If the KEYDOWN is stopped propagation (e.g. triggered an accelerator),
85 // don't InsertChar/InsertText to the input field. 80 // don't InsertChar/InsertText to the input field.
86 if (handled) { 81 if (event->stopped_propagation() || details.target_destroyed) {
87 ResetContext(); 82 ResetContext();
88 return true; 83 return;
89 } 84 }
90 85
91 // Don't send VKEY_PROCESSKEY event if there is no result text or 86 // Don't send VKEY_PROCESSKEY event if there is no result text or
92 // composition. This is to workaround the weird behavior of IBus with US 87 // composition. This is to workaround the weird behavior of IBus with US
93 // keyboard, which mutes the keydown and later fake a new keydown with IME 88 // keyboard, which mutes the keydown and later fake a new keydown with IME
94 // result in sync mode. In that case, user would expect only 89 // result in sync mode. In that case, user would expect only
95 // keydown/keypress/keyup event without an initial 229 keydown event. 90 // keydown/keypress/keyup event without an initial 229 keydown event.
96 } 91 }
97 92
93 bool should_stop_propagation = false;
98 TextInputClient* client = GetTextInputClient(); 94 TextInputClient* client = GetTextInputClient();
99 // Processes the result text before composition for sync mode. 95 // Processes the result text before composition for sync mode.
100 if (!result_text_.empty()) { 96 if (!result_text_.empty()) {
101 if (filtered && NeedInsertChar()) { 97 if (filtered && NeedInsertChar()) {
102 for (const auto ch : result_text_) 98 for (const auto ch : result_text_)
103 client->InsertChar(ch, event.flags()); 99 client->InsertChar(ch, event->flags());
104 } else { 100 } else {
105 // If |filtered| is false, that means the IME wants to commit some text 101 // If |filtered| is false, that means the IME wants to commit some text
106 // but still release the key to the application. For example, Korean IME 102 // but still release the key to the application. For example, Korean IME
107 // handles ENTER key to confirm its composition but still release it for 103 // handles ENTER key to confirm its composition but still release it for
108 // the default behavior (e.g. trigger search, etc.) 104 // the default behavior (e.g. trigger search, etc.)
109 // In such case, don't do InsertChar because a key should only trigger the 105 // In such case, don't do InsertChar because a key should only trigger the
110 // keydown event once. 106 // keydown event once.
111 client->InsertText(result_text_); 107 client->InsertText(result_text_);
112 } 108 }
109 should_stop_propagation = true;
113 } 110 }
114 111
115 if (composition_changed_ && !IsTextInputTypeNone()) { 112 if (composition_changed_ && !IsTextInputTypeNone()) {
116 // If composition changed, does SetComposition if composition is not empty. 113 // If composition changed, does SetComposition if composition is not empty.
117 // And ClearComposition if composition is empty. 114 // And ClearComposition if composition is empty.
118 if (!composition_.text.empty()) 115 if (!composition_.text.empty())
119 client->SetCompositionText(composition_); 116 client->SetCompositionText(composition_);
120 else if (result_text_.empty()) 117 else if (result_text_.empty())
121 client->ClearCompositionText(); 118 client->ClearCompositionText();
119 should_stop_propagation = true;
122 } 120 }
123 121
124 // Makes sure the cached composition is cleared after committing any text or 122 // Makes sure the cached composition is cleared after committing any text or
125 // cleared composition. 123 // cleared composition.
126 if (!client->HasCompositionText()) 124 if (!client->HasCompositionText())
127 composition_.Clear(); 125 composition_.Clear();
128 126
129 if (!filtered) { 127 if (!filtered) {
130 { 128 details = DispatchKeyEventPostIME(event);
131 base::AutoReset<bool*> auto_reset(&destroyed_ptr_, &destroyed); 129 if (details.dispatcher_destroyed) {
132 handled = DispatchKeyEventPostIME(event); 130 // Do nothing when dipatcher is destroyed.
sadrul 2015/08/04 07:15:59 This is fine for now, but can potentially lead to
Shu Chen 2015/08/04 07:36:11 Thanks for the suggestion. I was also trying to re
133 if (destroyed) 131 } else if (event->stopped_propagation() || details.target_destroyed) {
134 return true;
135 }
136 if (handled) {
137 ResetContext(); 132 ResetContext();
138 return true; 133 } else if (event->type() == ui::ET_KEY_PRESSED) {
139 }
140 if (event.type() == ui::ET_KEY_PRESSED) {
141 // If a key event was not filtered by |context_| or |context_simple_|, 134 // If a key event was not filtered by |context_| or |context_simple_|,
142 // then it means the key event didn't generate any result text. For some 135 // then it means the key event didn't generate any result text. For some
143 // cases, the key event may still generate a valid character, eg. a 136 // cases, the key event may still generate a valid character, eg. a
144 // control-key event (ctrl-a, return, tab, etc.). We need to send the 137 // control-key event (ctrl-a, return, tab, etc.). We need to send the
145 // character to the focused text input client by calling 138 // character to the focused text input client by calling
146 // TextInputClient::InsertChar(). 139 // TextInputClient::InsertChar().
147 // Note: don't use |client| and use GetTextInputClient() here because 140 // Note: don't use |client| and use GetTextInputClient() here because
148 // DispatchKeyEventPostIME may cause the current text input client change. 141 // DispatchKeyEventPostIME may cause the current text input client change.
149 base::char16 ch = event.GetCharacter(); 142 base::char16 ch = event->GetCharacter();
150 if (ch && GetTextInputClient()) 143 if (ch && GetTextInputClient())
151 GetTextInputClient()->InsertChar(ch, event.flags()); 144 GetTextInputClient()->InsertChar(ch, event->flags());
145 should_stop_propagation = true;
152 } 146 }
153 } 147 }
154 148
155 return true; 149 if (should_stop_propagation)
150 event->StopPropagation();
156 } 151 }
157 152
158 void InputMethodAuraLinux::UpdateContextFocusState() { 153 void InputMethodAuraLinux::UpdateContextFocusState() {
159 bool old_text_input_type = text_input_type_; 154 bool old_text_input_type = text_input_type_;
160 text_input_type_ = GetTextInputType(); 155 text_input_type_ = GetTextInputType();
161 156
162 // We only focus in |context_| when the focus is in a textfield. 157 // We only focus in |context_| when the focus is in a textfield.
163 if (old_text_input_type != TEXT_INPUT_TYPE_NONE && 158 if (old_text_input_type != TEXT_INPUT_TYPE_NONE &&
164 text_input_type_ == TEXT_INPUT_TYPE_NONE) { 159 text_input_type_ == TEXT_INPUT_TYPE_NONE) {
165 context_->Blur(); 160 context_->Blur();
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 return; 236 return;
242 } 237 }
243 238
244 if (is_sync_mode_) { 239 if (is_sync_mode_) {
245 // Append the text to the buffer, because commit signal might be fired 240 // Append the text to the buffer, because commit signal might be fired
246 // multiple times when processing a key event. 241 // multiple times when processing a key event.
247 result_text_.append(text); 242 result_text_.append(text);
248 } else if (!IsTextInputTypeNone()) { 243 } else if (!IsTextInputTypeNone()) {
249 // If we are not handling key event, do not bother sending text result if 244 // If we are not handling key event, do not bother sending text result if
250 // the focused text input client does not support text input. 245 // the focused text input client does not support text input.
251 SendFakeProcessKeyEvent(0); 246 ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, 0);
252 GetTextInputClient()->InsertText(text); 247 ui::EventDispatchDetails details = SendFakeProcessKeyEvent(&event);
248 if (details.dispatcher_destroyed)
249 return;
250 if (!event.stopped_propagation() && !details.target_destroyed)
251 GetTextInputClient()->InsertText(text);
253 composition_.Clear(); 252 composition_.Clear();
254 } 253 }
255 } 254 }
256 255
257 void InputMethodAuraLinux::OnPreeditChanged( 256 void InputMethodAuraLinux::OnPreeditChanged(
258 const CompositionText& composition_text) { 257 const CompositionText& composition_text) {
259 if (suppress_next_result_ || IsTextInputTypeNone()) 258 if (suppress_next_result_ || IsTextInputTypeNone())
260 return; 259 return;
261 260
262 if (is_sync_mode_) { 261 if (is_sync_mode_) {
263 if (!composition_.text.empty() || !composition_text.text.empty()) 262 if (!composition_.text.empty() || !composition_text.text.empty())
264 composition_changed_ = true; 263 composition_changed_ = true;
265 } else { 264 } else {
266 SendFakeProcessKeyEvent(0); 265 ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, 0);
267 GetTextInputClient()->SetCompositionText(composition_text); 266 ui::EventDispatchDetails details = SendFakeProcessKeyEvent(&event);
267 if (details.dispatcher_destroyed)
268 return;
269 if (!event.stopped_propagation() && !details.target_destroyed)
270 GetTextInputClient()->SetCompositionText(composition_text);
268 } 271 }
269 272
270 composition_ = composition_text; 273 composition_ = composition_text;
271 } 274 }
272 275
273 void InputMethodAuraLinux::OnPreeditEnd() { 276 void InputMethodAuraLinux::OnPreeditEnd() {
274 if (suppress_next_result_ || IsTextInputTypeNone()) 277 if (suppress_next_result_ || IsTextInputTypeNone())
275 return; 278 return;
276 279
277 if (is_sync_mode_) { 280 if (is_sync_mode_) {
278 if (!composition_.text.empty()) { 281 if (!composition_.text.empty()) {
279 composition_.Clear(); 282 composition_.Clear();
280 composition_changed_ = true; 283 composition_changed_ = true;
281 } 284 }
282 } else { 285 } else {
283 TextInputClient* client = GetTextInputClient(); 286 TextInputClient* client = GetTextInputClient();
284 if (client && client->HasCompositionText()) { 287 if (client && client->HasCompositionText()) {
285 SendFakeProcessKeyEvent(0); 288 ui::KeyEvent event(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, 0);
286 client->ClearCompositionText(); 289 ui::EventDispatchDetails details = SendFakeProcessKeyEvent(&event);
290 if (details.dispatcher_destroyed)
291 return;
292 if (!event.stopped_propagation() && !details.target_destroyed)
293 client->ClearCompositionText();
287 } 294 }
288 composition_.Clear(); 295 composition_.Clear();
289 } 296 }
290 } 297 }
291 298
292 // Overridden from InputMethodBase. 299 // Overridden from InputMethodBase.
293 300
294 void InputMethodAuraLinux::OnFocus() { 301 void InputMethodAuraLinux::OnFocus() {
295 InputMethodBase::OnFocus(); 302 InputMethodBase::OnFocus();
296 UpdateContextFocusState(); 303 UpdateContextFocusState();
(...skipping 29 matching lines...) Expand all
326 bool InputMethodAuraLinux::HasInputMethodResult() { 333 bool InputMethodAuraLinux::HasInputMethodResult() {
327 return !result_text_.empty() || composition_changed_; 334 return !result_text_.empty() || composition_changed_;
328 } 335 }
329 336
330 bool InputMethodAuraLinux::NeedInsertChar() const { 337 bool InputMethodAuraLinux::NeedInsertChar() const {
331 return IsTextInputTypeNone() || 338 return IsTextInputTypeNone() ||
332 (!composition_changed_ && composition_.text.empty() && 339 (!composition_changed_ && composition_.text.empty() &&
333 result_text_.length() == 1); 340 result_text_.length() == 1);
334 } 341 }
335 342
336 bool InputMethodAuraLinux::SendFakeProcessKeyEvent(int flags) const { 343 ui::EventDispatchDetails InputMethodAuraLinux::SendFakeProcessKeyEvent(
337 return DispatchKeyEventPostIME( 344 ui::KeyEvent* event) const {
338 KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, flags)); 345 KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, event->flags());
346 ui::EventDispatchDetails details = DispatchKeyEventPostIME(&key_event);
347 if (key_event.stopped_propagation())
348 event->StopPropagation();
349 return details;
339 } 350 }
340 351
341 void InputMethodAuraLinux::ConfirmCompositionText() { 352 void InputMethodAuraLinux::ConfirmCompositionText() {
342 TextInputClient* client = GetTextInputClient(); 353 TextInputClient* client = GetTextInputClient();
343 if (client && client->HasCompositionText()) 354 if (client && client->HasCompositionText())
344 client->ConfirmCompositionText(); 355 client->ConfirmCompositionText();
345 356
346 ResetContext(); 357 ResetContext();
347 } 358 }
348 359
349 } // namespace ui 360 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/ime/input_method_auralinux.h ('k') | ui/base/ime/input_method_auralinux_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698