| 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/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)); |
| 56 | 53 |
| 57 suppress_next_result_ = false; | 54 suppress_next_result_ = false; |
| 58 composition_changed_ = false; | 55 composition_changed_ = false; |
| 59 result_text_.clear(); | 56 result_text_.clear(); |
| 60 | 57 |
| 61 bool filtered = false; | 58 bool filtered = false; |
| 62 { | 59 { |
| 63 base::AutoReset<bool> flipper(&is_sync_mode_, true); | 60 base::AutoReset<bool> flipper(&is_sync_mode_, true); |
| 64 if (text_input_type_ != TEXT_INPUT_TYPE_NONE && | 61 if (text_input_type_ != TEXT_INPUT_TYPE_NONE && |
| 65 text_input_type_ != TEXT_INPUT_TYPE_PASSWORD) { | 62 text_input_type_ != TEXT_INPUT_TYPE_PASSWORD) { |
| 66 filtered = context_->DispatchKeyEvent(event); | 63 filtered = context_->DispatchKeyEvent(*event); |
| 67 } else { | 64 } else { |
| 68 filtered = context_simple_->DispatchKeyEvent(event); | 65 filtered = context_simple_->DispatchKeyEvent(*event); |
| 69 } | 66 } |
| 70 } | 67 } |
| 71 | 68 |
| 72 bool destroyed = false; | 69 ui::EventDispatchDetails details; |
| 73 bool handled = false; | 70 if (event->type() == ui::ET_KEY_PRESSED && filtered) { |
| 74 if (event.type() == ui::ET_KEY_PRESSED && filtered) { | 71 if (NeedInsertChar()) |
| 75 { | 72 details = DispatchKeyEventPostIME(event); |
| 76 base::AutoReset<bool*> auto_reset(&destroyed_ptr_, &destroyed); | 73 else if (HasInputMethodResult()) |
| 77 if (NeedInsertChar()) | 74 SendFakeProcessKeyEvent(event->flags()); |
| 78 handled = DispatchKeyEventPostIME(event); | 75 if (details.dispatcher_destroyed) |
| 79 else if (HasInputMethodResult()) | 76 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), | 77 // If the KEYDOWN is stopped propagation (e.g. triggered an accelerator), |
| 85 // don't InsertChar/InsertText to the input field. | 78 // don't InsertChar/InsertText to the input field. |
| 86 if (handled) { | 79 if (event->stopped_propagation()) { |
| 87 ResetContext(); | 80 ResetContext(); |
| 88 return true; | 81 return; |
| 89 } | 82 } |
| 90 | 83 |
| 91 // Don't send VKEY_PROCESSKEY event if there is no result text or | 84 // 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 | 85 // 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 | 86 // 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 | 87 // result in sync mode. In that case, user would expect only |
| 95 // keydown/keypress/keyup event without an initial 229 keydown event. | 88 // keydown/keypress/keyup event without an initial 229 keydown event. |
| 96 } | 89 } |
| 97 | 90 |
| 98 TextInputClient* client = GetTextInputClient(); | 91 TextInputClient* client = GetTextInputClient(); |
| 99 // Processes the result text before composition for sync mode. | 92 // Processes the result text before composition for sync mode. |
| 100 if (!result_text_.empty()) { | 93 if (!result_text_.empty()) { |
| 101 if (filtered && NeedInsertChar()) { | 94 if (filtered && NeedInsertChar()) { |
| 102 for (const auto ch : result_text_) | 95 for (const auto ch : result_text_) |
| 103 client->InsertChar(ch, event.flags()); | 96 client->InsertChar(ch, event->flags()); |
| 104 } else { | 97 } else { |
| 105 // If |filtered| is false, that means the IME wants to commit some text | 98 // 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 | 99 // 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 | 100 // handles ENTER key to confirm its composition but still release it for |
| 108 // the default behavior (e.g. trigger search, etc.) | 101 // the default behavior (e.g. trigger search, etc.) |
| 109 // In such case, don't do InsertChar because a key should only trigger the | 102 // In such case, don't do InsertChar because a key should only trigger the |
| 110 // keydown event once. | 103 // keydown event once. |
| 111 client->InsertText(result_text_); | 104 client->InsertText(result_text_); |
| 112 } | 105 } |
| 113 } | 106 } |
| 114 | 107 |
| 115 if (composition_changed_ && !IsTextInputTypeNone()) { | 108 if (composition_changed_ && !IsTextInputTypeNone()) { |
| 116 // If composition changed, does SetComposition if composition is not empty. | 109 // If composition changed, does SetComposition if composition is not empty. |
| 117 // And ClearComposition if composition is empty. | 110 // And ClearComposition if composition is empty. |
| 118 if (!composition_.text.empty()) | 111 if (!composition_.text.empty()) |
| 119 client->SetCompositionText(composition_); | 112 client->SetCompositionText(composition_); |
| 120 else if (result_text_.empty()) | 113 else if (result_text_.empty()) |
| 121 client->ClearCompositionText(); | 114 client->ClearCompositionText(); |
| 122 } | 115 } |
| 123 | 116 |
| 124 // Makes sure the cached composition is cleared after committing any text or | 117 // Makes sure the cached composition is cleared after committing any text or |
| 125 // cleared composition. | 118 // cleared composition. |
| 126 if (!client->HasCompositionText()) | 119 if (!client->HasCompositionText()) |
| 127 composition_.Clear(); | 120 composition_.Clear(); |
| 128 | 121 |
| 129 if (!filtered) { | 122 if (!filtered) { |
| 130 { | 123 details = DispatchKeyEventPostIME(event); |
| 131 base::AutoReset<bool*> auto_reset(&destroyed_ptr_, &destroyed); | 124 if (details.dispatcher_destroyed) |
| 132 handled = DispatchKeyEventPostIME(event); | 125 return; |
| 133 if (destroyed) | 126 if (event->stopped_propagation()) { |
| 134 return true; | 127 ResetContext(); |
| 128 return; |
| 135 } | 129 } |
| 136 if (handled) { | 130 if (event->type() == ui::ET_KEY_PRESSED) { |
| 137 ResetContext(); | |
| 138 return true; | |
| 139 } | |
| 140 if (event.type() == ui::ET_KEY_PRESSED) { | |
| 141 // If a key event was not filtered by |context_| or |context_simple_|, | 131 // 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 | 132 // 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 | 133 // 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 | 134 // control-key event (ctrl-a, return, tab, etc.). We need to send the |
| 145 // character to the focused text input client by calling | 135 // character to the focused text input client by calling |
| 146 // TextInputClient::InsertChar(). | 136 // TextInputClient::InsertChar(). |
| 147 // Note: don't use |client| and use GetTextInputClient() here because | 137 // Note: don't use |client| and use GetTextInputClient() here because |
| 148 // DispatchKeyEventPostIME may cause the current text input client change. | 138 // DispatchKeyEventPostIME may cause the current text input client change. |
| 149 base::char16 ch = event.GetCharacter(); | 139 base::char16 ch = event->GetCharacter(); |
| 150 if (ch && GetTextInputClient()) | 140 if (ch && GetTextInputClient()) |
| 151 GetTextInputClient()->InsertChar(ch, event.flags()); | 141 GetTextInputClient()->InsertChar(ch, event->flags()); |
| 152 } | 142 } |
| 153 } | 143 } |
| 154 | |
| 155 return true; | |
| 156 } | 144 } |
| 157 | 145 |
| 158 void InputMethodAuraLinux::UpdateContextFocusState() { | 146 void InputMethodAuraLinux::UpdateContextFocusState() { |
| 159 bool old_text_input_type = text_input_type_; | 147 bool old_text_input_type = text_input_type_; |
| 160 text_input_type_ = GetTextInputType(); | 148 text_input_type_ = GetTextInputType(); |
| 161 | 149 |
| 162 // We only focus in |context_| when the focus is in a textfield. | 150 // We only focus in |context_| when the focus is in a textfield. |
| 163 if (old_text_input_type != TEXT_INPUT_TYPE_NONE && | 151 if (old_text_input_type != TEXT_INPUT_TYPE_NONE && |
| 164 text_input_type_ == TEXT_INPUT_TYPE_NONE) { | 152 text_input_type_ == TEXT_INPUT_TYPE_NONE) { |
| 165 context_->Blur(); | 153 context_->Blur(); |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 bool InputMethodAuraLinux::HasInputMethodResult() { | 314 bool InputMethodAuraLinux::HasInputMethodResult() { |
| 327 return !result_text_.empty() || composition_changed_; | 315 return !result_text_.empty() || composition_changed_; |
| 328 } | 316 } |
| 329 | 317 |
| 330 bool InputMethodAuraLinux::NeedInsertChar() const { | 318 bool InputMethodAuraLinux::NeedInsertChar() const { |
| 331 return IsTextInputTypeNone() || | 319 return IsTextInputTypeNone() || |
| 332 (!composition_changed_ && composition_.text.empty() && | 320 (!composition_changed_ && composition_.text.empty() && |
| 333 result_text_.length() == 1); | 321 result_text_.length() == 1); |
| 334 } | 322 } |
| 335 | 323 |
| 336 bool InputMethodAuraLinux::SendFakeProcessKeyEvent(int flags) const { | 324 void InputMethodAuraLinux::SendFakeProcessKeyEvent(int flags) const { |
| 337 return DispatchKeyEventPostIME( | 325 KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, flags); |
| 338 KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, flags)); | 326 ignore_result(DispatchKeyEventPostIME(&key_event)); |
| 339 } | 327 } |
| 340 | 328 |
| 341 void InputMethodAuraLinux::ConfirmCompositionText() { | 329 void InputMethodAuraLinux::ConfirmCompositionText() { |
| 342 TextInputClient* client = GetTextInputClient(); | 330 TextInputClient* client = GetTextInputClient(); |
| 343 if (client && client->HasCompositionText()) | 331 if (client && client->HasCompositionText()) |
| 344 client->ConfirmCompositionText(); | 332 client->ConfirmCompositionText(); |
| 345 | 333 |
| 346 ResetContext(); | 334 ResetContext(); |
| 347 } | 335 } |
| 348 | 336 |
| 349 } // namespace ui | 337 } // namespace ui |
| OLD | NEW |