OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/arc/ime/arc_ime_service.h" | 5 #include "components/arc/ime/arc_ime_service.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "components/arc/ime/arc_ime_bridge_impl.h" | 11 #include "components/arc/ime/arc_ime_bridge_impl.h" |
12 #include "components/exo/shell_surface.h" | 12 #include "components/exo/shell_surface.h" |
13 #include "components/exo/surface.h" | 13 #include "components/exo/surface.h" |
14 #include "ui/aura/env.h" | 14 #include "ui/aura/env.h" |
15 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
16 #include "ui/aura/window_tree_host.h" | 16 #include "ui/aura/window_tree_host.h" |
17 #include "ui/base/ime/input_method.h" | 17 #include "ui/base/ime/input_method.h" |
18 #include "ui/events/base_event_utils.h" | 18 #include "ui/events/base_event_utils.h" |
19 #include "ui/events/event.h" | 19 #include "ui/events/event.h" |
20 #include "ui/events/keycodes/keyboard_codes.h" | 20 #include "ui/events/keycodes/keyboard_codes.h" |
21 #include "ui/gfx/range/range.h" | |
21 | 22 |
22 namespace arc { | 23 namespace arc { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 class ArcWindowDelegateImpl : public ArcImeService::ArcWindowDelegate { | 27 class ArcWindowDelegateImpl : public ArcImeService::ArcWindowDelegate { |
27 public: | 28 public: |
28 explicit ArcWindowDelegateImpl(ArcImeService* ime_service) | 29 explicit ArcWindowDelegateImpl(ArcImeService* ime_service) |
29 : ime_service_(ime_service) {} | 30 : ime_service_(ime_service) {} |
30 | 31 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 if (ime_type_ == type) | 203 if (ime_type_ == type) |
203 return; | 204 return; |
204 ime_type_ = type; | 205 ime_type_ = type; |
205 | 206 |
206 ui::InputMethod* const input_method = GetInputMethod(); | 207 ui::InputMethod* const input_method = GetInputMethod(); |
207 if (input_method) | 208 if (input_method) |
208 input_method->OnTextInputTypeChanged(this); | 209 input_method->OnTextInputTypeChanged(this); |
209 } | 210 } |
210 | 211 |
211 void ArcImeService::OnCursorRectChanged(const gfx::Rect& rect) { | 212 void ArcImeService::OnCursorRectChanged(const gfx::Rect& rect) { |
213 InvalidateSurroundingTextAndSelectionRange(); | |
212 if (cursor_rect_ == rect) | 214 if (cursor_rect_ == rect) |
213 return; | 215 return; |
214 cursor_rect_ = rect; | 216 cursor_rect_ = rect; |
215 | 217 |
216 ui::InputMethod* const input_method = GetInputMethod(); | 218 ui::InputMethod* const input_method = GetInputMethod(); |
217 if (input_method) | 219 if (input_method) |
218 input_method->OnCaretBoundsChanged(this); | 220 input_method->OnCaretBoundsChanged(this); |
219 } | 221 } |
220 | 222 |
221 void ArcImeService::OnCancelComposition() { | 223 void ArcImeService::OnCancelComposition() { |
224 InvalidateSurroundingTextAndSelectionRange(); | |
222 ui::InputMethod* const input_method = GetInputMethod(); | 225 ui::InputMethod* const input_method = GetInputMethod(); |
223 if (input_method) | 226 if (input_method) |
224 input_method->CancelComposition(this); | 227 input_method->CancelComposition(this); |
225 } | 228 } |
226 | 229 |
227 void ArcImeService::ShowImeIfNeeded() { | 230 void ArcImeService::ShowImeIfNeeded() { |
228 ui::InputMethod* const input_method = GetInputMethod(); | 231 ui::InputMethod* const input_method = GetInputMethod(); |
229 if (input_method && input_method->GetTextInputClient() == this) { | 232 if (input_method && input_method->GetTextInputClient() == this) { |
230 input_method->ShowImeIfNeeded(); | 233 input_method->ShowImeIfNeeded(); |
231 } | 234 } |
232 } | 235 } |
233 | 236 |
237 void ArcImeService::OnCursorRectChangedWithSurroundingText( | |
238 const gfx::Rect& rect, | |
239 const gfx::Range& text_range, | |
240 const base::string16& text_in_range, | |
241 const gfx::Range& selection_range) { | |
242 if (cursor_rect_ == rect) | |
243 return; | |
kinaba
2017/05/15 07:55:18
Can't there be a case that the rects are the same
yhanada
2017/05/15 08:06:38
It is possible. I changed to save the text and the
| |
244 cursor_rect_ = rect; | |
245 text_range_ = text_range; | |
246 text_in_range_ = text_in_range; | |
247 selection_range_ = selection_range; | |
248 | |
249 ui::InputMethod* const input_method = GetInputMethod(); | |
250 if (input_method) | |
251 input_method->OnCaretBoundsChanged(this); | |
252 } | |
253 | |
234 //////////////////////////////////////////////////////////////////////////////// | 254 //////////////////////////////////////////////////////////////////////////////// |
235 // Overridden from keyboard::KeyboardControllerObserver | 255 // Overridden from keyboard::KeyboardControllerObserver |
236 void ArcImeService::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) { | 256 void ArcImeService::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) { |
237 if (!focused_arc_window_) | 257 if (!focused_arc_window_) |
238 return; | 258 return; |
239 aura::Window* window = focused_arc_window_; | 259 aura::Window* window = focused_arc_window_; |
240 // Multiply by the scale factor. To convert from DPI to physical pixels. | 260 // Multiply by the scale factor. To convert from DPI to physical pixels. |
241 gfx::Rect bounds_in_px = gfx::ScaleToEnclosingRect( | 261 gfx::Rect bounds_in_px = gfx::ScaleToEnclosingRect( |
242 new_bounds, window->layer()->device_scale_factor()); | 262 new_bounds, window->layer()->device_scale_factor()); |
243 ime_bridge_->SendOnKeyboardBoundsChanging(bounds_in_px); | 263 ime_bridge_->SendOnKeyboardBoundsChanging(bounds_in_px); |
244 } | 264 } |
245 | 265 |
246 void ArcImeService::OnKeyboardClosed() {} | 266 void ArcImeService::OnKeyboardClosed() {} |
247 | 267 |
248 //////////////////////////////////////////////////////////////////////////////// | 268 //////////////////////////////////////////////////////////////////////////////// |
249 // Overridden from ui::TextInputClient: | 269 // Overridden from ui::TextInputClient: |
250 | 270 |
251 void ArcImeService::SetCompositionText( | 271 void ArcImeService::SetCompositionText( |
252 const ui::CompositionText& composition) { | 272 const ui::CompositionText& composition) { |
273 InvalidateSurroundingTextAndSelectionRange(); | |
253 has_composition_text_ = !composition.text.empty(); | 274 has_composition_text_ = !composition.text.empty(); |
254 ime_bridge_->SendSetCompositionText(composition); | 275 ime_bridge_->SendSetCompositionText(composition); |
255 } | 276 } |
256 | 277 |
257 void ArcImeService::ConfirmCompositionText() { | 278 void ArcImeService::ConfirmCompositionText() { |
279 InvalidateSurroundingTextAndSelectionRange(); | |
258 has_composition_text_ = false; | 280 has_composition_text_ = false; |
259 ime_bridge_->SendConfirmCompositionText(); | 281 ime_bridge_->SendConfirmCompositionText(); |
260 } | 282 } |
261 | 283 |
262 void ArcImeService::ClearCompositionText() { | 284 void ArcImeService::ClearCompositionText() { |
285 InvalidateSurroundingTextAndSelectionRange(); | |
263 if (has_composition_text_) { | 286 if (has_composition_text_) { |
264 has_composition_text_ = false; | 287 has_composition_text_ = false; |
265 ime_bridge_->SendInsertText(base::string16()); | 288 ime_bridge_->SendInsertText(base::string16()); |
266 } | 289 } |
267 } | 290 } |
268 | 291 |
269 void ArcImeService::InsertText(const base::string16& text) { | 292 void ArcImeService::InsertText(const base::string16& text) { |
293 InvalidateSurroundingTextAndSelectionRange(); | |
270 has_composition_text_ = false; | 294 has_composition_text_ = false; |
271 ime_bridge_->SendInsertText(text); | 295 ime_bridge_->SendInsertText(text); |
272 } | 296 } |
273 | 297 |
274 void ArcImeService::InsertChar(const ui::KeyEvent& event) { | 298 void ArcImeService::InsertChar(const ui::KeyEvent& event) { |
275 // According to the document in text_input_client.h, InsertChar() is called | 299 // According to the document in text_input_client.h, InsertChar() is called |
276 // even when the text input type is NONE. We ignore such events, since for | 300 // even when the text input type is NONE. We ignore such events, since for |
277 // ARC we are only interested in the event as a method of text input. | 301 // ARC we are only interested in the event as a method of text input. |
278 if (ime_type_ == ui::TEXT_INPUT_TYPE_NONE) | 302 if (ime_type_ == ui::TEXT_INPUT_TYPE_NONE) |
279 return; | 303 return; |
280 | 304 |
305 InvalidateSurroundingTextAndSelectionRange(); | |
306 | |
281 // For apps that doesn't handle hardware keyboard events well, keys that are | 307 // For apps that doesn't handle hardware keyboard events well, keys that are |
282 // typically on software keyboard and lack of them are fatal, namely, | 308 // typically on software keyboard and lack of them are fatal, namely, |
283 // unmodified enter and backspace keys are sent through IME. | 309 // unmodified enter and backspace keys are sent through IME. |
284 constexpr int kModifierMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | | 310 constexpr int kModifierMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
285 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN | | 311 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN | |
286 ui::EF_ALTGR_DOWN | ui::EF_MOD3_DOWN; | 312 ui::EF_ALTGR_DOWN | ui::EF_MOD3_DOWN; |
287 if ((event.flags() & kModifierMask) == 0) { | 313 if ((event.flags() & kModifierMask) == 0) { |
288 if (event.key_code() == ui::VKEY_RETURN) { | 314 if (event.key_code() == ui::VKEY_RETURN) { |
289 has_composition_text_ = false; | 315 has_composition_text_ = false; |
290 ime_bridge_->SendInsertText(base::ASCIIToUTF16("\n")); | 316 ime_bridge_->SendInsertText(base::ASCIIToUTF16("\n")); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
330 | 356 |
331 // Add the offset of the window showing the ARC app. | 357 // Add the offset of the window showing the ARC app. |
332 // TODO(yoshiki): Support for non-arc toplevel window. The following code do | 358 // TODO(yoshiki): Support for non-arc toplevel window. The following code do |
333 // not work correctly with arc windows inside non-arc toplevel window (eg. | 359 // not work correctly with arc windows inside non-arc toplevel window (eg. |
334 // notification). | 360 // notification). |
335 converted.Offset( | 361 converted.Offset( |
336 window->GetToplevelWindow()->GetBoundsInScreen().OffsetFromOrigin()); | 362 window->GetToplevelWindow()->GetBoundsInScreen().OffsetFromOrigin()); |
337 return converted; | 363 return converted; |
338 } | 364 } |
339 | 365 |
366 bool ArcImeService::GetTextRange(gfx::Range* range) const { | |
367 if (!text_range_.IsValid()) | |
368 return false; | |
369 *range = text_range_; | |
370 return true; | |
371 } | |
372 | |
373 bool ArcImeService::GetSelectionRange(gfx::Range* range) const { | |
374 if (!selection_range_.IsValid()) | |
375 return false; | |
376 *range = selection_range_; | |
377 return true; | |
378 } | |
379 | |
380 bool ArcImeService::GetTextFromRange(const gfx::Range& range, | |
381 base::string16* text) const { | |
382 // It's supposed that this method is called only from | |
383 // InputMethod::OnCaretBoundsChanged(). In that method, the range obtained | |
384 // from GetTextRange() is used as the argument of this method. To prevent an | |
385 // unexpected usage, the check, |range != text_range_|, is added. | |
386 if (!text_range_.IsValid() || range != text_range_) | |
387 return false; | |
388 *text = text_in_range_; | |
389 return true; | |
390 } | |
391 | |
340 ui::TextInputMode ArcImeService::GetTextInputMode() const { | 392 ui::TextInputMode ArcImeService::GetTextInputMode() const { |
341 return ui::TEXT_INPUT_MODE_DEFAULT; | 393 return ui::TEXT_INPUT_MODE_DEFAULT; |
342 } | 394 } |
343 | 395 |
344 base::i18n::TextDirection ArcImeService::GetTextDirection() const { | 396 base::i18n::TextDirection ArcImeService::GetTextDirection() const { |
345 return base::i18n::UNKNOWN_DIRECTION; | 397 return base::i18n::UNKNOWN_DIRECTION; |
346 } | 398 } |
347 | 399 |
348 void ArcImeService::ExtendSelectionAndDelete(size_t before, size_t after) { | 400 void ArcImeService::ExtendSelectionAndDelete(size_t before, size_t after) { |
349 ime_bridge_->SendExtendSelectionAndDelete(before, after); | 401 ime_bridge_->SendExtendSelectionAndDelete(before, after); |
kinaba
2017/05/15 07:55:18
InvalidateSurroundingTextAndSelectionRange here to
yhanada
2017/05/15 08:06:38
Done.
| |
350 } | 402 } |
351 | 403 |
352 int ArcImeService::GetTextInputFlags() const { | 404 int ArcImeService::GetTextInputFlags() const { |
353 return ui::TEXT_INPUT_FLAG_NONE; | 405 return ui::TEXT_INPUT_FLAG_NONE; |
354 } | 406 } |
355 | 407 |
356 bool ArcImeService::CanComposeInline() const { | 408 bool ArcImeService::CanComposeInline() const { |
357 return true; | 409 return true; |
358 } | 410 } |
359 | 411 |
360 bool ArcImeService::GetCompositionCharacterBounds( | 412 bool ArcImeService::GetCompositionCharacterBounds( |
361 uint32_t index, gfx::Rect* rect) const { | 413 uint32_t index, gfx::Rect* rect) const { |
362 return false; | 414 return false; |
363 } | 415 } |
364 | 416 |
365 bool ArcImeService::HasCompositionText() const { | 417 bool ArcImeService::HasCompositionText() const { |
366 return has_composition_text_; | 418 return has_composition_text_; |
367 } | 419 } |
368 | 420 |
369 bool ArcImeService::GetTextRange(gfx::Range* range) const { | |
370 return false; | |
371 } | |
372 | |
373 bool ArcImeService::GetCompositionTextRange(gfx::Range* range) const { | 421 bool ArcImeService::GetCompositionTextRange(gfx::Range* range) const { |
374 return false; | 422 return false; |
375 } | 423 } |
376 | 424 |
377 bool ArcImeService::GetSelectionRange(gfx::Range* range) const { | |
378 return false; | |
379 } | |
380 | |
381 bool ArcImeService::SetSelectionRange(const gfx::Range& range) { | 425 bool ArcImeService::SetSelectionRange(const gfx::Range& range) { |
382 return false; | 426 return false; |
383 } | 427 } |
384 | 428 |
385 bool ArcImeService::DeleteRange(const gfx::Range& range) { | 429 bool ArcImeService::DeleteRange(const gfx::Range& range) { |
386 return false; | 430 return false; |
387 } | 431 } |
388 | 432 |
389 bool ArcImeService::GetTextFromRange( | |
390 const gfx::Range& range, base::string16* text) const { | |
391 return false; | |
392 } | |
393 | |
394 bool ArcImeService::ChangeTextDirectionAndLayoutAlignment( | 433 bool ArcImeService::ChangeTextDirectionAndLayoutAlignment( |
395 base::i18n::TextDirection direction) { | 434 base::i18n::TextDirection direction) { |
396 return false; | 435 return false; |
397 } | 436 } |
398 | 437 |
399 bool ArcImeService::IsTextEditCommandEnabled( | 438 bool ArcImeService::IsTextEditCommandEnabled( |
400 ui::TextEditCommand command) const { | 439 ui::TextEditCommand command) const { |
401 return false; | 440 return false; |
402 } | 441 } |
403 | 442 |
443 void ArcImeService::InvalidateSurroundingTextAndSelectionRange() { | |
444 text_range_ = gfx::Range::InvalidRange(); | |
445 text_in_range_ = base::string16(); | |
446 selection_range_ = gfx::Range::InvalidRange(); | |
447 } | |
448 | |
404 } // namespace arc | 449 } // namespace arc |
OLD | NEW |