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

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

Issue 1068093002: Refactoring for InputMethodAuraLinux. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed a bug and add tests. Created 5 years, 8 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/environment.h" 8 #include "base/environment.h"
8 #include "ui/base/ime/linux/linux_input_method_context_factory.h" 9 #include "ui/base/ime/linux/linux_input_method_context_factory.h"
9 #include "ui/base/ime/text_input_client.h" 10 #include "ui/base/ime/text_input_client.h"
10 #include "ui/events/event.h" 11 #include "ui/events/event.h"
11 12
12 namespace ui { 13 namespace ui {
13 14
14 InputMethodAuraLinux::InputMethodAuraLinux( 15 InputMethodAuraLinux::InputMethodAuraLinux(
15 internal::InputMethodDelegate* delegate) 16 internal::InputMethodDelegate* delegate)
16 : allowed_to_fire_vkey_process_key_(false), vkey_processkey_flags_(0) { 17 : text_input_type_(TEXT_INPUT_TYPE_NONE),
18 is_sync_mode_(false),
19 composition_changed_(false),
20 suppress_next_result_(false) {
17 SetDelegate(delegate); 21 SetDelegate(delegate);
22 context_ =
23 LinuxInputMethodContextFactory::instance()->CreateInputMethodContext(
24 this, false);
25 context_simple_ =
26 LinuxInputMethodContextFactory::instance()->CreateInputMethodContext(
27 this, true);
18 } 28 }
19 29
20 InputMethodAuraLinux::~InputMethodAuraLinux() {} 30 InputMethodAuraLinux::~InputMethodAuraLinux() {}
21 31
32 LinuxInputMethodContext* InputMethodAuraLinux::GetContextForTesting(
33 bool is_simple) {
34 return is_simple ? context_simple_.get() : context_.get();
35 }
36
22 // Overriden from InputMethod. 37 // Overriden from InputMethod.
23 38
24 void InputMethodAuraLinux::Init(bool focused) { 39 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); 40 InputMethodBase::Init(focused);
35 41
36 if (focused) { 42 UpdateContextFocusState();
37 input_method_context_->OnTextInputTypeChanged(
38 GetTextInputClient() ?
39 GetTextInputClient()->GetTextInputType() :
40 TEXT_INPUT_TYPE_TEXT);
41 }
42 } 43 }
43 44
44 bool InputMethodAuraLinux::OnUntranslatedIMEMessage( 45 bool InputMethodAuraLinux::OnUntranslatedIMEMessage(
45 const base::NativeEvent& event, 46 const base::NativeEvent& event,
46 NativeEventResult* result) { 47 NativeEventResult* result) {
47 return false; 48 return false;
48 } 49 }
49 50
50 bool InputMethodAuraLinux::DispatchKeyEvent(const ui::KeyEvent& event) { 51 bool InputMethodAuraLinux::DispatchKeyEvent(const ui::KeyEvent& event) {
51 DCHECK(event.type() == ET_KEY_PRESSED || event.type() == ET_KEY_RELEASED); 52 DCHECK(event.type() == ET_KEY_PRESSED || event.type() == ET_KEY_RELEASED);
52 DCHECK(system_toplevel_window_focused()); 53 DCHECK(system_toplevel_window_focused());
53 54
55 TextInputClient* client = GetTextInputClient();
54 // If no text input client, do nothing. 56 // If no text input client, do nothing.
55 if (!GetTextInputClient()) 57 if (!client)
56 return DispatchKeyEventPostIME(event); 58 return DispatchKeyEventPostIME(event);
57 59
58 // Let an IME handle the key event first, and allow to fire a VKEY_PROCESSKEY 60 suppress_next_result_ = false;
59 // event for keydown events. Note that DOM Level 3 Events Sepc requires that 61 composition_changed_ = false;
60 // only keydown events fire keyCode=229 events and not for keyup events. 62 result_text_.clear();
James Su 2015/04/10 09:25:16 clear composition_ here.
Shu Chen 2015/04/10 16:17:38 should not do this per offline discussion.
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 63
68 // Otherwise, insert the character. 64 bool filtered = false;
69 const bool handled = DispatchKeyEventPostIME(event); 65 {
70 if (event.type() == ET_KEY_PRESSED && GetTextInputClient()) { 66 base::AutoReset<bool> flipper(&is_sync_mode_, true);
71 const uint16 ch = event.GetCharacter(); 67 if (text_input_type_ != TEXT_INPUT_TYPE_NONE &&
72 if (ch) { 68 text_input_type_ != TEXT_INPUT_TYPE_PASSWORD) {
73 GetTextInputClient()->InsertChar(ch, event.flags()); 69 filtered = context_->DispatchKeyEvent(event);
74 return true; 70 } else {
71 filtered = context_simple_->DispatchKeyEvent(event);
75 } 72 }
76 } 73 }
77 return handled; 74
75 if (event.type() == ui::ET_KEY_PRESSED && filtered) {
76 if (NeedInsertChar())
77 DispatchKeyEventPostIME(event);
78 else if (HasInputMethodResult())
79 SendFakeProcessKeyEvent(event.flags());
80
81 // Don't send VKEY_PROCESSKEY event if there is no result text or
82 // composition. This is to workaround the wierd behavior of IBus with US
James Su 2015/04/10 09:25:16 wierd -> weird
Shu Chen 2015/04/10 16:17:38 Done.
83 // keyboard, which mutes the keydown and later fake a new keydown with IME
84 // result in sync mode. In that case, user would expect only
85 // keydown/keypress/keyup event without an initial 229 keydown event.
86 }
87
88 // Processes the result text before composition for sync mode.
89 if (!result_text_.empty()) {
90 if (filtered && NeedInsertChar()) {
91 for (const auto ch : result_text_)
92 client->InsertChar(ch, event.flags());
93 } else {
94 client->InsertText(result_text_);
James Su 2015/04/10 09:25:17 add comment to explain the logic of this block. Es
Shu Chen 2015/04/10 16:17:38 Done.
95 }
96 }
97
98 if (composition_changed_ && !IsTextInputTypeNone()) {
99 if (!composition_.text.empty())
100 client->SetCompositionText(composition_);
101 else if (result_text_.empty())
102 client->ClearCompositionText();
James Su 2015/04/10 09:25:17 add comment to explain this part of logic. Especia
Shu Chen 2015/04/10 16:17:38 Done.
103 }
104
105 if (!result_text_.empty() && !composition_.text.empty())
106 composition_.Clear();
James Su 2015/04/10 09:25:17 I don't understand this if block. I don't think th
Shu Chen 2015/04/10 16:17:38 Comment is added.
107
James Su 2015/04/10 09:25:16 I'm wondering if it's more clear to return true he
Shu Chen 2015/04/10 16:17:39 I would prefer to avoid returning in the middle of
108 if (event.type() == ui::ET_KEY_PRESSED && !filtered) {
109 DispatchKeyEventPostIME(event);
110
111 // If a key event was not filtered by |context_| or |context_simple_|, then
112 // it means the key event didn't generate any result text. For some cases,
113 // the key event may still generate a valid character, eg. a control-key
114 // event (ctrl-a, return, tab, etc.). We need to send the character to the
115 // focused text input client by calling TextInputClient::InsertChar().
116 base::char16 ch = event.GetCharacter();
117 if (ch && client)
118 client->InsertChar(ch, event.flags());
119 } else if (event.type() == ui::ET_KEY_RELEASED && !filtered) {
120 DispatchKeyEventPostIME(event);
121 }
122
123 return true;
124 }
125
126 void InputMethodAuraLinux::UpdateContextFocusState() {
127 bool old_text_input_type = text_input_type_;
128 text_input_type_ = GetTextInputType();
129
130 // We only focus in |context_| when the focus is in a textfield.
131 if (old_text_input_type != TEXT_INPUT_TYPE_NONE &&
132 text_input_type_ == TEXT_INPUT_TYPE_NONE) {
133 context_->Blur();
134 } else if (old_text_input_type == TEXT_INPUT_TYPE_NONE &&
135 text_input_type_ != TEXT_INPUT_TYPE_NONE) {
136 context_->Focus();
137 }
138
139 // |context_simple_| can be used in any textfield, including password box, and
140 // even if the focused text input client's text input type is
141 // ui::TEXT_INPUT_TYPE_NONE.
142 if (GetTextInputClient())
143 context_simple_->Focus();
144 else
145 context_simple_->Blur();
78 } 146 }
79 147
80 void InputMethodAuraLinux::OnTextInputTypeChanged( 148 void InputMethodAuraLinux::OnTextInputTypeChanged(
81 const TextInputClient* client) { 149 const TextInputClient* client) {
82 if (!IsTextInputClientFocused(client)) 150 UpdateContextFocusState();
83 return;
84 input_method_context_->Reset();
85 // TODO(yoichio): Support inputmode HTML attribute. 151 // TODO(yoichio): Support inputmode HTML attribute.
86 input_method_context_->OnTextInputTypeChanged(client->GetTextInputType());
87 } 152 }
88 153
89 void InputMethodAuraLinux::OnCaretBoundsChanged(const TextInputClient* client) { 154 void InputMethodAuraLinux::OnCaretBoundsChanged(const TextInputClient* client) {
90 if (!IsTextInputClientFocused(client)) 155 if (!IsTextInputClientFocused(client))
91 return; 156 return;
92 input_method_context_->OnCaretBoundsChanged( 157 context_->SetCursorLocation(GetTextInputClient()->GetCaretBounds());
93 GetTextInputClient()->GetCaretBounds());
94 } 158 }
95 159
96 void InputMethodAuraLinux::CancelComposition(const TextInputClient* client) { 160 void InputMethodAuraLinux::CancelComposition(const TextInputClient* client) {
97 if (!IsTextInputClientFocused(client)) 161 if (!IsTextInputClientFocused(client))
98 return; 162 return;
99 input_method_context_->Reset(); 163 ResetContext();
100 input_method_context_->OnTextInputTypeChanged(client->GetTextInputType()); 164 }
165
166 void InputMethodAuraLinux::ResetContext() {
167 if (!GetTextInputClient())
168 return;
169
170 // To prevent any text from being committed when resetting the |context_|;
171 is_sync_mode_ = true;
172 suppress_next_result_ = true;
173
174 context_->Reset();
175 context_simple_->Reset();
176
177 // Some input methods may not honour the reset call. Focusing out/in the
178 // |context_| to make sure it gets reset correctly.
179 if (text_input_type_ != TEXT_INPUT_TYPE_NONE) {
180 context_->Blur();
181 context_->Focus();
182 }
183
184 composition_.Clear();
185 result_text_.clear();
186 is_sync_mode_ = false;
187 composition_changed_ = false;
101 } 188 }
102 189
103 void InputMethodAuraLinux::OnInputLocaleChanged() { 190 void InputMethodAuraLinux::OnInputLocaleChanged() {
104 } 191 }
105 192
106 std::string InputMethodAuraLinux::GetInputLocale() { 193 std::string InputMethodAuraLinux::GetInputLocale() {
107 return ""; 194 return "";
108 } 195 }
109 196
110 bool InputMethodAuraLinux::IsActive() { 197 bool InputMethodAuraLinux::IsActive() {
111 // InputMethodAuraLinux is always ready and up. 198 // InputMethodAuraLinux is always ready and up.
112 return true; 199 return true;
113 } 200 }
114 201
115 bool InputMethodAuraLinux::IsCandidatePopupOpen() const { 202 bool InputMethodAuraLinux::IsCandidatePopupOpen() const {
116 // There seems no way to detect candidate windows or any popups. 203 // There seems no way to detect candidate windows or any popups.
117 return false; 204 return false;
118 } 205 }
119 206
120 // Overriden from ui::LinuxInputMethodContextDelegate 207 // Overriden from ui::LinuxInputMethodContextDelegate
121 208
122 void InputMethodAuraLinux::OnCommit(const base::string16& text) { 209 void InputMethodAuraLinux::OnCommit(const base::string16& text) {
123 MaybeFireProcessKey(); 210 if (suppress_next_result_ || !GetTextInputClient()) {
James Su 2015/04/10 09:25:16 Why not check IsTextInputTypeNone() instead?
Shu Chen 2015/04/10 16:17:38 When IsTextInputTypeNone() returns true, here cann
124 if (!IsTextInputTypeNone()) 211 suppress_next_result_ = false;
212 return;
213 }
214
215 if (is_sync_mode_) {
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 } else if (!IsTextInputTypeNone()) {
220 // If we are not handling key event, do not bother sending text result if
221 // the focused text input client does not support text input.
222 SendFakeProcessKeyEvent(0);
125 GetTextInputClient()->InsertText(text); 223 GetTextInputClient()->InsertText(text);
224 composition_.Clear();
225 }
126 } 226 }
127 227
128 void InputMethodAuraLinux::OnPreeditChanged( 228 void InputMethodAuraLinux::OnPreeditChanged(
129 const CompositionText& composition_text) { 229 const CompositionText& composition_text) {
130 MaybeFireProcessKey(); 230 if (suppress_next_result_ || IsTextInputTypeNone())
131 TextInputClient* text_input_client = GetTextInputClient(); 231 return;
132 if (text_input_client) 232
133 text_input_client->SetCompositionText(composition_text); 233 composition_ = composition_text;
James Su 2015/04/10 09:25:16 move this line into if (is_sync_mode_) block.
Shu Chen 2015/04/10 16:17:38 Invalid comment per our offline discussion.
234
235 if (is_sync_mode_) {
236 if (!composition_.text.empty() || !composition_text.text.empty())
237 composition_changed_ = true;
238 } else {
239 SendFakeProcessKeyEvent(0);
240 GetTextInputClient()->SetCompositionText(composition_text);
241 }
134 } 242 }
135 243
136 void InputMethodAuraLinux::OnPreeditEnd() { 244 void InputMethodAuraLinux::OnPreeditEnd() {
137 MaybeFireProcessKey(); 245 if (suppress_next_result_ || IsTextInputTypeNone())
138 TextInputClient* text_input_client = GetTextInputClient(); 246 return;
139 if (text_input_client && text_input_client->HasCompositionText())
140 text_input_client->ClearCompositionText();
141 }
142 247
143 void InputMethodAuraLinux::OnPreeditStart() { 248 if (is_sync_mode_) {
144 MaybeFireProcessKey(); 249 if (!composition_.text.empty()) {
250 composition_.Clear();
251 composition_changed_ = true;
252 }
253 } else {
254 TextInputClient* client = GetTextInputClient();
255 if (client && client->HasCompositionText())
256 client->ClearCompositionText();
257 composition_.Clear();
James Su 2015/04/10 09:25:16 remove this line.
Shu Chen 2015/04/10 16:17:38 Invalid comment per our offline discussion.
258 }
145 } 259 }
146 260
147 // Overridden from InputMethodBase. 261 // Overridden from InputMethodBase.
148 262
263 void InputMethodAuraLinux::OnFocus() {
264 InputMethodBase::OnFocus();
265 UpdateContextFocusState();
266 }
267
268 void InputMethodAuraLinux::OnBlur() {
269 ConfirmCompositionText();
270 InputMethodBase::OnBlur();
271 UpdateContextFocusState();
272 }
273
274 void InputMethodAuraLinux::OnWillChangeFocusedClient(
275 TextInputClient* focused_before,
276 TextInputClient* focused) {
277 ConfirmCompositionText();
278 }
279
149 void InputMethodAuraLinux::OnDidChangeFocusedClient( 280 void InputMethodAuraLinux::OnDidChangeFocusedClient(
150 TextInputClient* focused_before, 281 TextInputClient* focused_before,
151 TextInputClient* focused) { 282 TextInputClient* focused) {
152 input_method_context_->Reset(); 283 UpdateContextFocusState();
153 input_method_context_->OnTextInputTypeChanged( 284
154 focused ? focused->GetTextInputType() : TEXT_INPUT_TYPE_NONE); 285 // Force to update caret bounds, in case the View thinks that the caret
286 // bounds has not changed.
287 if (text_input_type_ != TEXT_INPUT_TYPE_NONE)
288 OnCaretBoundsChanged(GetTextInputClient());
155 289
156 InputMethodBase::OnDidChangeFocusedClient(focused_before, focused); 290 InputMethodBase::OnDidChangeFocusedClient(focused_before, focused);
157 } 291 }
158 292
159 // Helper functions to support VKEY_PROCESSKEY. 293 // private
160 294
161 void InputMethodAuraLinux::AllowToFireProcessKey(const ui::KeyEvent& event) { 295 bool InputMethodAuraLinux::HasInputMethodResult() {
162 allowed_to_fire_vkey_process_key_ = true; 296 return !result_text_.empty() || composition_changed_;
163 vkey_processkey_flags_ = event.flags();
164 } 297 }
165 298
166 void InputMethodAuraLinux::MaybeFireProcessKey() { 299 bool InputMethodAuraLinux::NeedInsertChar() const {
167 if (!allowed_to_fire_vkey_process_key_) 300 return IsTextInputTypeNone() ||
168 return; 301 (!composition_changed_ && composition_.text.empty() &&
169 302 result_text_.length() == 1);
170 const ui::KeyEvent fabricated_event(ET_KEY_PRESSED,
171 VKEY_PROCESSKEY,
172 vkey_processkey_flags_);
173 DispatchKeyEventPostIME(fabricated_event);
174 StopFiringProcessKey();
175 } 303 }
176 304
177 void InputMethodAuraLinux::StopFiringProcessKey() { 305 void InputMethodAuraLinux::SendFakeProcessKeyEvent(int flags) const {
178 allowed_to_fire_vkey_process_key_ = false; 306 DispatchKeyEventPostIME(
179 vkey_processkey_flags_ = 0; 307 KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, flags));
308 }
309
310 void InputMethodAuraLinux::ConfirmCompositionText() {
311 TextInputClient* client = GetTextInputClient();
312 if (client && client->HasCompositionText())
313 client->ConfirmCompositionText();
314
315 ResetContext();
180 } 316 }
181 317
182 } // namespace ui 318 } // 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