| 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 text_range_ = text_range; |
| 243 text_in_range_ = text_in_range; |
| 244 selection_range_ = selection_range; |
| 245 |
| 246 if (cursor_rect_ == rect) |
| 247 return; |
| 248 cursor_rect_ = rect; |
| 249 |
| 250 ui::InputMethod* const input_method = GetInputMethod(); |
| 251 if (input_method) |
| 252 input_method->OnCaretBoundsChanged(this); |
| 253 } |
| 254 |
| 234 //////////////////////////////////////////////////////////////////////////////// | 255 //////////////////////////////////////////////////////////////////////////////// |
| 235 // Overridden from keyboard::KeyboardControllerObserver | 256 // Overridden from keyboard::KeyboardControllerObserver |
| 236 void ArcImeService::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) { | 257 void ArcImeService::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) { |
| 237 if (!focused_arc_window_) | 258 if (!focused_arc_window_) |
| 238 return; | 259 return; |
| 239 aura::Window* window = focused_arc_window_; | 260 aura::Window* window = focused_arc_window_; |
| 240 // Multiply by the scale factor. To convert from DPI to physical pixels. | 261 // Multiply by the scale factor. To convert from DPI to physical pixels. |
| 241 gfx::Rect bounds_in_px = gfx::ScaleToEnclosingRect( | 262 gfx::Rect bounds_in_px = gfx::ScaleToEnclosingRect( |
| 242 new_bounds, window->layer()->device_scale_factor()); | 263 new_bounds, window->layer()->device_scale_factor()); |
| 243 ime_bridge_->SendOnKeyboardBoundsChanging(bounds_in_px); | 264 ime_bridge_->SendOnKeyboardBoundsChanging(bounds_in_px); |
| 244 } | 265 } |
| 245 | 266 |
| 246 void ArcImeService::OnKeyboardClosed() {} | 267 void ArcImeService::OnKeyboardClosed() {} |
| 247 | 268 |
| 248 //////////////////////////////////////////////////////////////////////////////// | 269 //////////////////////////////////////////////////////////////////////////////// |
| 249 // Overridden from ui::TextInputClient: | 270 // Overridden from ui::TextInputClient: |
| 250 | 271 |
| 251 void ArcImeService::SetCompositionText( | 272 void ArcImeService::SetCompositionText( |
| 252 const ui::CompositionText& composition) { | 273 const ui::CompositionText& composition) { |
| 274 InvalidateSurroundingTextAndSelectionRange(); |
| 253 has_composition_text_ = !composition.text.empty(); | 275 has_composition_text_ = !composition.text.empty(); |
| 254 ime_bridge_->SendSetCompositionText(composition); | 276 ime_bridge_->SendSetCompositionText(composition); |
| 255 } | 277 } |
| 256 | 278 |
| 257 void ArcImeService::ConfirmCompositionText() { | 279 void ArcImeService::ConfirmCompositionText() { |
| 280 InvalidateSurroundingTextAndSelectionRange(); |
| 258 has_composition_text_ = false; | 281 has_composition_text_ = false; |
| 259 ime_bridge_->SendConfirmCompositionText(); | 282 ime_bridge_->SendConfirmCompositionText(); |
| 260 } | 283 } |
| 261 | 284 |
| 262 void ArcImeService::ClearCompositionText() { | 285 void ArcImeService::ClearCompositionText() { |
| 286 InvalidateSurroundingTextAndSelectionRange(); |
| 263 if (has_composition_text_) { | 287 if (has_composition_text_) { |
| 264 has_composition_text_ = false; | 288 has_composition_text_ = false; |
| 265 ime_bridge_->SendInsertText(base::string16()); | 289 ime_bridge_->SendInsertText(base::string16()); |
| 266 } | 290 } |
| 267 } | 291 } |
| 268 | 292 |
| 269 void ArcImeService::InsertText(const base::string16& text) { | 293 void ArcImeService::InsertText(const base::string16& text) { |
| 294 InvalidateSurroundingTextAndSelectionRange(); |
| 270 has_composition_text_ = false; | 295 has_composition_text_ = false; |
| 271 ime_bridge_->SendInsertText(text); | 296 ime_bridge_->SendInsertText(text); |
| 272 } | 297 } |
| 273 | 298 |
| 274 void ArcImeService::InsertChar(const ui::KeyEvent& event) { | 299 void ArcImeService::InsertChar(const ui::KeyEvent& event) { |
| 275 // According to the document in text_input_client.h, InsertChar() is called | 300 // 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 | 301 // 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. | 302 // ARC we are only interested in the event as a method of text input. |
| 278 if (ime_type_ == ui::TEXT_INPUT_TYPE_NONE) | 303 if (ime_type_ == ui::TEXT_INPUT_TYPE_NONE) |
| 279 return; | 304 return; |
| 280 | 305 |
| 306 InvalidateSurroundingTextAndSelectionRange(); |
| 307 |
| 281 // For apps that doesn't handle hardware keyboard events well, keys that are | 308 // 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, | 309 // typically on software keyboard and lack of them are fatal, namely, |
| 283 // unmodified enter and backspace keys are sent through IME. | 310 // unmodified enter and backspace keys are sent through IME. |
| 284 constexpr int kModifierMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | | 311 constexpr int kModifierMask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
| 285 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN | | 312 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN | |
| 286 ui::EF_ALTGR_DOWN | ui::EF_MOD3_DOWN; | 313 ui::EF_ALTGR_DOWN | ui::EF_MOD3_DOWN; |
| 287 if ((event.flags() & kModifierMask) == 0) { | 314 if ((event.flags() & kModifierMask) == 0) { |
| 288 if (event.key_code() == ui::VKEY_RETURN) { | 315 if (event.key_code() == ui::VKEY_RETURN) { |
| 289 has_composition_text_ = false; | 316 has_composition_text_ = false; |
| 290 ime_bridge_->SendInsertText(base::ASCIIToUTF16("\n")); | 317 ime_bridge_->SendInsertText(base::ASCIIToUTF16("\n")); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 | 357 |
| 331 // Add the offset of the window showing the ARC app. | 358 // Add the offset of the window showing the ARC app. |
| 332 // TODO(yoshiki): Support for non-arc toplevel window. The following code do | 359 // 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. | 360 // not work correctly with arc windows inside non-arc toplevel window (eg. |
| 334 // notification). | 361 // notification). |
| 335 converted.Offset( | 362 converted.Offset( |
| 336 window->GetToplevelWindow()->GetBoundsInScreen().OffsetFromOrigin()); | 363 window->GetToplevelWindow()->GetBoundsInScreen().OffsetFromOrigin()); |
| 337 return converted; | 364 return converted; |
| 338 } | 365 } |
| 339 | 366 |
| 367 bool ArcImeService::GetTextRange(gfx::Range* range) const { |
| 368 if (!text_range_.IsValid()) |
| 369 return false; |
| 370 *range = text_range_; |
| 371 return true; |
| 372 } |
| 373 |
| 374 bool ArcImeService::GetSelectionRange(gfx::Range* range) const { |
| 375 if (!selection_range_.IsValid()) |
| 376 return false; |
| 377 *range = selection_range_; |
| 378 return true; |
| 379 } |
| 380 |
| 381 bool ArcImeService::GetTextFromRange(const gfx::Range& range, |
| 382 base::string16* text) const { |
| 383 // It's supposed that this method is called only from |
| 384 // InputMethod::OnCaretBoundsChanged(). In that method, the range obtained |
| 385 // from GetTextRange() is used as the argument of this method. To prevent an |
| 386 // unexpected usage, the check, |range != text_range_|, is added. |
| 387 if (!text_range_.IsValid() || range != text_range_) |
| 388 return false; |
| 389 *text = text_in_range_; |
| 390 return true; |
| 391 } |
| 392 |
| 340 ui::TextInputMode ArcImeService::GetTextInputMode() const { | 393 ui::TextInputMode ArcImeService::GetTextInputMode() const { |
| 341 return ui::TEXT_INPUT_MODE_DEFAULT; | 394 return ui::TEXT_INPUT_MODE_DEFAULT; |
| 342 } | 395 } |
| 343 | 396 |
| 344 base::i18n::TextDirection ArcImeService::GetTextDirection() const { | 397 base::i18n::TextDirection ArcImeService::GetTextDirection() const { |
| 345 return base::i18n::UNKNOWN_DIRECTION; | 398 return base::i18n::UNKNOWN_DIRECTION; |
| 346 } | 399 } |
| 347 | 400 |
| 348 void ArcImeService::ExtendSelectionAndDelete(size_t before, size_t after) { | 401 void ArcImeService::ExtendSelectionAndDelete(size_t before, size_t after) { |
| 402 InvalidateSurroundingTextAndSelectionRange(); |
| 349 ime_bridge_->SendExtendSelectionAndDelete(before, after); | 403 ime_bridge_->SendExtendSelectionAndDelete(before, after); |
| 350 } | 404 } |
| 351 | 405 |
| 352 int ArcImeService::GetTextInputFlags() const { | 406 int ArcImeService::GetTextInputFlags() const { |
| 353 return ui::TEXT_INPUT_FLAG_NONE; | 407 return ui::TEXT_INPUT_FLAG_NONE; |
| 354 } | 408 } |
| 355 | 409 |
| 356 bool ArcImeService::CanComposeInline() const { | 410 bool ArcImeService::CanComposeInline() const { |
| 357 return true; | 411 return true; |
| 358 } | 412 } |
| 359 | 413 |
| 360 bool ArcImeService::GetCompositionCharacterBounds( | 414 bool ArcImeService::GetCompositionCharacterBounds( |
| 361 uint32_t index, gfx::Rect* rect) const { | 415 uint32_t index, gfx::Rect* rect) const { |
| 362 return false; | 416 return false; |
| 363 } | 417 } |
| 364 | 418 |
| 365 bool ArcImeService::HasCompositionText() const { | 419 bool ArcImeService::HasCompositionText() const { |
| 366 return has_composition_text_; | 420 return has_composition_text_; |
| 367 } | 421 } |
| 368 | 422 |
| 369 bool ArcImeService::GetTextRange(gfx::Range* range) const { | |
| 370 return false; | |
| 371 } | |
| 372 | |
| 373 bool ArcImeService::GetCompositionTextRange(gfx::Range* range) const { | 423 bool ArcImeService::GetCompositionTextRange(gfx::Range* range) const { |
| 374 return false; | 424 return false; |
| 375 } | 425 } |
| 376 | 426 |
| 377 bool ArcImeService::GetSelectionRange(gfx::Range* range) const { | |
| 378 return false; | |
| 379 } | |
| 380 | |
| 381 bool ArcImeService::SetSelectionRange(const gfx::Range& range) { | 427 bool ArcImeService::SetSelectionRange(const gfx::Range& range) { |
| 382 return false; | 428 return false; |
| 383 } | 429 } |
| 384 | 430 |
| 385 bool ArcImeService::DeleteRange(const gfx::Range& range) { | 431 bool ArcImeService::DeleteRange(const gfx::Range& range) { |
| 386 return false; | 432 return false; |
| 387 } | 433 } |
| 388 | 434 |
| 389 bool ArcImeService::GetTextFromRange( | |
| 390 const gfx::Range& range, base::string16* text) const { | |
| 391 return false; | |
| 392 } | |
| 393 | |
| 394 bool ArcImeService::ChangeTextDirectionAndLayoutAlignment( | 435 bool ArcImeService::ChangeTextDirectionAndLayoutAlignment( |
| 395 base::i18n::TextDirection direction) { | 436 base::i18n::TextDirection direction) { |
| 396 return false; | 437 return false; |
| 397 } | 438 } |
| 398 | 439 |
| 399 bool ArcImeService::IsTextEditCommandEnabled( | 440 bool ArcImeService::IsTextEditCommandEnabled( |
| 400 ui::TextEditCommand command) const { | 441 ui::TextEditCommand command) const { |
| 401 return false; | 442 return false; |
| 402 } | 443 } |
| 403 | 444 |
| 445 void ArcImeService::InvalidateSurroundingTextAndSelectionRange() { |
| 446 text_range_ = gfx::Range::InvalidRange(); |
| 447 text_in_range_ = base::string16(); |
| 448 selection_range_ = gfx::Range::InvalidRange(); |
| 449 } |
| 450 |
| 404 } // namespace arc | 451 } // namespace arc |
| OLD | NEW |