| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/aura/mus/input_method_mus.h" | 5 #include "ui/aura/mus/input_method_mus.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "services/ui/public/interfaces/constants.mojom.h" | 10 #include "services/ui/public/interfaces/constants.mojom.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 //////////////////////////////////////////////////////////////////////////////// | 25 //////////////////////////////////////////////////////////////////////////////// |
| 26 // InputMethodMus, public: | 26 // InputMethodMus, public: |
| 27 | 27 |
| 28 InputMethodMus::InputMethodMus(ui::internal::InputMethodDelegate* delegate, | 28 InputMethodMus::InputMethodMus(ui::internal::InputMethodDelegate* delegate, |
| 29 Window* window) | 29 Window* window) |
| 30 : window_(window) { | 30 : window_(window) { |
| 31 SetDelegate(delegate); | 31 SetDelegate(delegate); |
| 32 } | 32 } |
| 33 | 33 |
| 34 InputMethodMus::~InputMethodMus() {} | 34 InputMethodMus::~InputMethodMus() { |
| 35 // Mus won't dispatch the next key event until the existing one is acked. We |
| 36 // may have KeyEvents sent to IME and awaiting the result, we need to ack |
| 37 // them otherwise mus won't process the next event until it times out. |
| 38 for (auto& callback_ptr : pending_callbacks_) { |
| 39 if (callback_ptr) |
| 40 callback_ptr->Run(EventResult::UNHANDLED); |
| 41 } |
| 42 } |
| 35 | 43 |
| 36 void InputMethodMus::Init(service_manager::Connector* connector) { | 44 void InputMethodMus::Init(service_manager::Connector* connector) { |
| 37 if (connector) | 45 if (connector) |
| 38 connector->BindInterface(ui::mojom::kServiceName, &ime_server_); | 46 connector->BindInterface(ui::mojom::kServiceName, &ime_server_); |
| 39 } | 47 } |
| 40 | 48 |
| 41 void InputMethodMus::DispatchKeyEvent( | 49 void InputMethodMus::DispatchKeyEvent( |
| 42 ui::KeyEvent* event, | 50 ui::KeyEvent* event, |
| 43 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback) { | 51 std::unique_ptr<EventResultCallback> ack_callback) { |
| 44 DCHECK(event->type() == ui::ET_KEY_PRESSED || | 52 DCHECK(event->type() == ui::ET_KEY_PRESSED || |
| 45 event->type() == ui::ET_KEY_RELEASED); | 53 event->type() == ui::ET_KEY_RELEASED); |
| 46 | 54 |
| 47 // If no text input client, do nothing. | 55 // If no text input client, do nothing. |
| 48 if (!GetTextInputClient()) { | 56 if (!GetTextInputClient()) { |
| 49 ignore_result(DispatchKeyEventPostIME(event)); | 57 ignore_result(DispatchKeyEventPostIME(event)); |
| 50 if (ack_callback) { | 58 if (ack_callback) { |
| 51 ack_callback->Run(event->handled() ? EventResult::HANDLED | 59 ack_callback->Run(event->handled() ? EventResult::HANDLED |
| 52 : EventResult::UNHANDLED); | 60 : EventResult::UNHANDLED); |
| 53 } | 61 } |
| 54 return; | 62 return; |
| 55 } | 63 } |
| 56 | 64 |
| 57 // IME driver will notify us whether it handled the event or not by calling | 65 SendKeyEventToInputMethod(*event, std::move(ack_callback)); |
| 58 // ProcessKeyEventCallback(), in which we will run the |ack_callback| to tell | |
| 59 // the window server if client handled the event or not. | |
| 60 input_method_->ProcessKeyEvent( | |
| 61 ui::Event::Clone(*event), | |
| 62 base::Bind(&InputMethodMus::ProcessKeyEventCallback, | |
| 63 base::Unretained(this), *event, Passed(&ack_callback))); | |
| 64 } | 66 } |
| 65 | 67 |
| 66 //////////////////////////////////////////////////////////////////////////////// | 68 //////////////////////////////////////////////////////////////////////////////// |
| 67 // InputMethodMus, ui::InputMethod implementation: | 69 // InputMethodMus, ui::InputMethod implementation: |
| 68 | 70 |
| 69 void InputMethodMus::OnFocus() { | 71 void InputMethodMus::OnFocus() { |
| 70 InputMethodBase::OnFocus(); | 72 InputMethodBase::OnFocus(); |
| 71 UpdateTextInputType(); | 73 UpdateTextInputType(); |
| 72 } | 74 } |
| 73 | 75 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 // TODO(moshayedi): crbug.com/637418. Not supported in ChromeOS. Investigate | 112 // TODO(moshayedi): crbug.com/637418. Not supported in ChromeOS. Investigate |
| 111 // whether we want to support this or not. | 113 // whether we want to support this or not. |
| 112 } | 114 } |
| 113 | 115 |
| 114 bool InputMethodMus::IsCandidatePopupOpen() const { | 116 bool InputMethodMus::IsCandidatePopupOpen() const { |
| 115 // TODO(moshayedi): crbug.com/637416. Implement this properly when we have a | 117 // TODO(moshayedi): crbug.com/637416. Implement this properly when we have a |
| 116 // mean for displaying candidate list popup. | 118 // mean for displaying candidate list popup. |
| 117 return false; | 119 return false; |
| 118 } | 120 } |
| 119 | 121 |
| 122 void InputMethodMus::SendKeyEventToInputMethod( |
| 123 const ui::KeyEvent& event, |
| 124 std::unique_ptr<EventResultCallback> ack_callback) { |
| 125 // IME driver will notify us whether it handled the event or not by calling |
| 126 // ProcessKeyEventCallback(), in which we will run the |ack_callback| to tell |
| 127 // the window server if client handled the event or not. |
| 128 pending_callbacks_.push_back(std::move(ack_callback)); |
| 129 input_method_->ProcessKeyEvent( |
| 130 ui::Event::Clone(event), |
| 131 base::Bind(&InputMethodMus::ProcessKeyEventCallback, |
| 132 base::Unretained(this), event)); |
| 133 } |
| 134 |
| 120 void InputMethodMus::OnDidChangeFocusedClient( | 135 void InputMethodMus::OnDidChangeFocusedClient( |
| 121 ui::TextInputClient* focused_before, | 136 ui::TextInputClient* focused_before, |
| 122 ui::TextInputClient* focused) { | 137 ui::TextInputClient* focused) { |
| 123 InputMethodBase::OnDidChangeFocusedClient(focused_before, focused); | 138 InputMethodBase::OnDidChangeFocusedClient(focused_before, focused); |
| 124 UpdateTextInputType(); | 139 UpdateTextInputType(); |
| 125 | 140 |
| 126 // TODO(moshayedi): crbug.com/681563. Handle when there is no focused clients. | 141 // TODO(moshayedi): crbug.com/681563. Handle when there is no focused clients. |
| 127 if (!focused) | 142 if (!focused) |
| 128 return; | 143 return; |
| 129 | 144 |
| 130 text_input_client_ = base::MakeUnique<TextInputClientImpl>(focused); | 145 text_input_client_ = base::MakeUnique<TextInputClientImpl>(focused); |
| 131 if (ime_server_) { | 146 if (ime_server_) { |
| 132 ui::mojom::StartSessionDetailsPtr details = | 147 ui::mojom::StartSessionDetailsPtr details = |
| 133 ui::mojom::StartSessionDetails::New(); | 148 ui::mojom::StartSessionDetails::New(); |
| 134 details->client = text_input_client_->CreateInterfacePtrAndBind(); | 149 details->client = text_input_client_->CreateInterfacePtrAndBind(); |
| 135 details->input_method_request = MakeRequest(&input_method_); | 150 details->input_method_request = MakeRequest(&input_method_ptr_); |
| 151 input_method_ = input_method_ptr_.get(); |
| 136 details->text_input_type = focused->GetTextInputType(); | 152 details->text_input_type = focused->GetTextInputType(); |
| 137 details->text_input_mode = focused->GetTextInputMode(); | 153 details->text_input_mode = focused->GetTextInputMode(); |
| 138 details->text_direction = focused->GetTextDirection(); | 154 details->text_direction = focused->GetTextDirection(); |
| 139 details->text_input_flags = focused->GetTextInputFlags(); | 155 details->text_input_flags = focused->GetTextInputFlags(); |
| 140 details->caret_bounds = focused->GetCaretBounds(); | 156 details->caret_bounds = focused->GetCaretBounds(); |
| 141 ime_server_->StartSession(std::move(details)); | 157 ime_server_->StartSession(std::move(details)); |
| 142 } | 158 } |
| 143 } | 159 } |
| 144 | 160 |
| 145 void InputMethodMus::UpdateTextInputType() { | 161 void InputMethodMus::UpdateTextInputType() { |
| 146 ui::TextInputType type = GetTextInputType(); | 162 ui::TextInputType type = GetTextInputType(); |
| 147 mojo::TextInputStatePtr state = mojo::TextInputState::New(); | 163 mojo::TextInputStatePtr state = mojo::TextInputState::New(); |
| 148 state->type = mojo::ConvertTo<mojo::TextInputType>(type); | 164 state->type = mojo::ConvertTo<mojo::TextInputType>(type); |
| 149 if (window_) { | 165 if (window_) { |
| 150 WindowPortMus* window_impl_mus = WindowPortMus::Get(window_); | 166 WindowPortMus* window_impl_mus = WindowPortMus::Get(window_); |
| 151 if (type != ui::TEXT_INPUT_TYPE_NONE) | 167 if (type != ui::TEXT_INPUT_TYPE_NONE) |
| 152 window_impl_mus->SetImeVisibility(true, std::move(state)); | 168 window_impl_mus->SetImeVisibility(true, std::move(state)); |
| 153 else | 169 else |
| 154 window_impl_mus->SetTextInputState(std::move(state)); | 170 window_impl_mus->SetTextInputState(std::move(state)); |
| 155 } | 171 } |
| 156 } | 172 } |
| 157 | 173 |
| 158 void InputMethodMus::ProcessKeyEventCallback( | 174 void InputMethodMus::ProcessKeyEventCallback( |
| 159 const ui::KeyEvent& event, | 175 const ui::KeyEvent& event, |
| 160 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback, | |
| 161 bool handled) { | 176 bool handled) { |
| 162 EventResult event_result; | 177 EventResult event_result; |
| 163 if (!handled) { | 178 if (!handled) { |
| 164 // If not handled by IME, try dispatching the event to delegate to see if | 179 // If not handled by IME, try dispatching the event to delegate to see if |
| 165 // any client-side post-ime processing needs to be done. This includes cases | 180 // any client-side post-ime processing needs to be done. This includes cases |
| 166 // like backspace, return key, etc. | 181 // like backspace, return key, etc. |
| 167 std::unique_ptr<ui::Event> event_clone = ui::Event::Clone(event); | 182 std::unique_ptr<ui::Event> event_clone = ui::Event::Clone(event); |
| 168 ignore_result(DispatchKeyEventPostIME(event_clone->AsKeyEvent())); | 183 ignore_result(DispatchKeyEventPostIME(event_clone->AsKeyEvent())); |
| 169 event_result = | 184 event_result = |
| 170 event_clone->handled() ? EventResult::HANDLED : EventResult::UNHANDLED; | 185 event_clone->handled() ? EventResult::HANDLED : EventResult::UNHANDLED; |
| 171 } else { | 186 } else { |
| 172 event_result = EventResult::HANDLED; | 187 event_result = EventResult::HANDLED; |
| 173 } | 188 } |
| 189 DCHECK(!pending_callbacks_.empty()); |
| 190 std::unique_ptr<EventResultCallback> ack_callback = |
| 191 std::move(pending_callbacks_.front()); |
| 192 pending_callbacks_.pop_front(); |
| 174 // |ack_callback| can be null if the standard form of DispatchKeyEvent() is | 193 // |ack_callback| can be null if the standard form of DispatchKeyEvent() is |
| 175 // called instead of the version which provides a callback. In mus+ash we | 194 // called instead of the version which provides a callback. In mus+ash we |
| 176 // use the version with callback, but some unittests use the standard form. | 195 // use the version with callback, but some unittests use the standard form. |
| 177 if (ack_callback) | 196 if (ack_callback) |
| 178 ack_callback->Run(event_result); | 197 ack_callback->Run(event_result); |
| 179 } | 198 } |
| 180 | 199 |
| 181 } // namespace aura | 200 } // namespace aura |
| OLD | NEW |