ime_controller_impl.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_IME_IME_CONTROLLER_IMPL_H_
  5. #define ASH_IME_IME_CONTROLLER_IMPL_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "ash/ash_export.h"
  9. #include "ash/public/cpp/cast_config_controller.h"
  10. #include "ash/public/cpp/ime_controller.h"
  11. #include "ash/public/cpp/ime_controller_client.h"
  12. #include "ash/public/cpp/ime_info.h"
  13. #include "ash/system/screen_security/screen_capture_observer.h"
  14. #include "base/observer_list.h"
  15. #include "mojo/public/cpp/bindings/pending_receiver.h"
  16. #include "mojo/public/cpp/bindings/pending_remote.h"
  17. #include "mojo/public/cpp/bindings/receiver_set.h"
  18. #include "mojo/public/cpp/bindings/remote.h"
  19. #include "ui/base/ime/ash/ime_keyset.h"
  20. #include "ui/display/display_observer.h"
  21. namespace ui {
  22. class Accelerator;
  23. }
  24. namespace ash {
  25. class ModeIndicatorObserver;
  26. // Connects ash IME users (e.g. the system tray) to the IME implementation,
  27. // which might live in Chrome browser or in a separate mojo service.
  28. class ASH_EXPORT ImeControllerImpl : public ImeController,
  29. public display::DisplayObserver,
  30. public CastConfigController::Observer,
  31. public ScreenCaptureObserver {
  32. public:
  33. class Observer {
  34. public:
  35. // Called when the caps lock state has changed.
  36. virtual void OnCapsLockChanged(bool enabled) = 0;
  37. // Called when the keyboard layout name has changed.
  38. virtual void OnKeyboardLayoutNameChanged(
  39. const std::string& layout_name) = 0;
  40. };
  41. ImeControllerImpl();
  42. ImeControllerImpl(const ImeControllerImpl&) = delete;
  43. ImeControllerImpl& operator=(const ImeControllerImpl&) = delete;
  44. ~ImeControllerImpl() override;
  45. void AddObserver(Observer* observer);
  46. void RemoveObserver(Observer* observer);
  47. const std::vector<ImeInfo>& GetVisibleImes() const;
  48. bool IsCurrentImeVisible() const;
  49. const ImeInfo& current_ime() const { return current_ime_; }
  50. bool is_extra_input_options_enabled() const {
  51. return is_extra_input_options_enabled_;
  52. }
  53. bool is_emoji_enabled() const { return is_emoji_enabled_; }
  54. bool is_handwriting_enabled() const { return is_handwriting_enabled_; }
  55. bool is_voice_enabled() const { return is_voice_enabled_; }
  56. bool managed_by_policy() const { return managed_by_policy_; }
  57. bool is_menu_active() const { return is_menu_active_; }
  58. const std::vector<ImeMenuItem>& current_ime_menu_items() const {
  59. return current_ime_menu_items_;
  60. }
  61. // Binds the mojo interface to this object.
  62. void BindReceiver(mojo::PendingReceiver<ImeController> receiver);
  63. // Returns true if switching to next/previous IME is allowed.
  64. bool CanSwitchIme() const;
  65. // Wrappers for ImeControllerClient methods.
  66. void SwitchToNextIme();
  67. void SwitchToLastUsedIme();
  68. void SwitchImeById(const std::string& ime_id, bool show_message);
  69. void ActivateImeMenuItem(const std::string& key);
  70. void SetCapsLockEnabled(bool caps_enabled);
  71. void OverrideKeyboardKeyset(input_method::ImeKeyset keyset);
  72. void OverrideKeyboardKeyset(
  73. input_method::ImeKeyset keyset,
  74. ImeControllerClient::OverrideKeyboardKeysetCallback callback);
  75. // Returns true if the switch is allowed and the keystroke should be
  76. // consumed.
  77. bool CanSwitchImeWithAccelerator(const ui::Accelerator& accelerator) const;
  78. void SwitchImeWithAccelerator(const ui::Accelerator& accelerator);
  79. // ImeController:
  80. void SetClient(ImeControllerClient* client) override;
  81. void RefreshIme(const std::string& current_ime_id,
  82. std::vector<ImeInfo> available_imes,
  83. std::vector<ImeMenuItem> menu_items) override;
  84. void SetImesManagedByPolicy(bool managed) override;
  85. void ShowImeMenuOnShelf(bool show) override;
  86. void UpdateCapsLockState(bool caps_enabled) override;
  87. void OnKeyboardLayoutNameChanged(const std::string& layout_name) override;
  88. void SetExtraInputOptionsEnabledState(bool is_extra_input_options_enabled,
  89. bool is_emoji_enabled,
  90. bool is_handwriting_enabled,
  91. bool is_voice_enabled) override;
  92. // Show the mode indicator UI with the given text at the anchor bounds.
  93. // The anchor bounds is in the universal screen coordinates in DIP.
  94. void ShowModeIndicator(const gfx::Rect& anchor_bounds,
  95. const std::u16string& ime_short_name) override;
  96. // display::DisplayObserver:
  97. void OnDisplayMetricsChanged(const display::Display& display,
  98. uint32_t changed_metrics) override;
  99. // CastConfigController::Observer:
  100. void OnDevicesUpdated(const std::vector<SinkAndRoute>& devices) override;
  101. // ScreenCaptureObserver:
  102. void OnScreenCaptureStart(
  103. const base::RepeatingClosure& stop_callback,
  104. const base::RepeatingClosure& source_callback,
  105. const std::u16string& screen_capture_status) override;
  106. void OnScreenCaptureStop() override;
  107. // Synchronously returns the cached caps lock state.
  108. bool IsCapsLockEnabled() const;
  109. // Synchronously returns the cached keyboard layout name
  110. const std::string& keyboard_layout_name() const {
  111. return keyboard_layout_name_;
  112. }
  113. ModeIndicatorObserver* mode_indicator_observer() const {
  114. return mode_indicator_observer_.get();
  115. }
  116. private:
  117. // Returns the IDs of the subset of input methods which are active and are
  118. // associated with |accelerator|. For example, two Japanese IMEs can be
  119. // returned for ui::VKEY_DBE_SBCSCHAR if both are active.
  120. std::vector<std::string> GetCandidateImesForAccelerator(
  121. const ui::Accelerator& accelerator) const;
  122. // Client interface back to IME code in chrome.
  123. ImeControllerClient* client_ = nullptr;
  124. // Copy of the current IME so we can return it by reference.
  125. ImeInfo current_ime_;
  126. // "Available" IMEs are both installed and enabled by the user in settings.
  127. std::vector<ImeInfo> available_imes_;
  128. // "Visible" IMEs are installed, enabled, and don't include built-in IMEs that
  129. // shouldn't be shown to the user, like Dictation.
  130. std::vector<ImeInfo> visible_imes_;
  131. // True if the available IMEs are currently managed by enterprise policy.
  132. // For example, can occur at the login screen with device-level policy.
  133. bool managed_by_policy_ = false;
  134. // Additional menu items for properties of the currently selected IME.
  135. std::vector<ImeMenuItem> current_ime_menu_items_;
  136. // A slightly delayed state value that is updated by asynchronously reported
  137. // changes from the ImeControllerClient client (source of truth) which is in
  138. // another process. This is required for synchronous method calls in ash.
  139. bool is_caps_lock_enabled_ = false;
  140. // A slightly delayed state value that is updated by asynchronously reported
  141. // changes from the ImeControllerClient client (source of truth) which is in
  142. // another process. This is required for synchronous method calls in ash.
  143. std::string keyboard_layout_name_;
  144. // True if the extended inputs should be available in general (emoji,
  145. // handwriting, voice).
  146. bool is_extra_input_options_enabled_ = false;
  147. // True if emoji input should be available from the IME menu.
  148. bool is_emoji_enabled_ = false;
  149. // True if handwriting input should be available from the IME menu.
  150. bool is_handwriting_enabled_ = false;
  151. // True if voice input should be available from the IME menu.
  152. bool is_voice_enabled_ = false;
  153. // True if the IME menu is active. IME related items in system tray should be
  154. // removed if |is_menu_active_| is true.
  155. bool is_menu_active_ = false;
  156. base::ObserverList<Observer>::Unchecked observers_;
  157. std::unique_ptr<ModeIndicatorObserver> mode_indicator_observer_;
  158. };
  159. } // namespace ash
  160. #endif // ASH_IME_IME_CONTROLLER_IMPL_H_