keyboard_ui_controller.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // Copyright (c) 2013 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_KEYBOARD_UI_KEYBOARD_UI_CONTROLLER_H_
  5. #define ASH_KEYBOARD_UI_KEYBOARD_UI_CONTROLLER_H_
  6. #include <memory>
  7. #include <set>
  8. #include <utility>
  9. #include <vector>
  10. #include "ash/keyboard/ui/container_behavior.h"
  11. #include "ash/keyboard/ui/display_util.h"
  12. #include "ash/keyboard/ui/keyboard_event_handler.h"
  13. #include "ash/keyboard/ui/keyboard_export.h"
  14. #include "ash/keyboard/ui/keyboard_layout_delegate.h"
  15. #include "ash/keyboard/ui/keyboard_ui_model.h"
  16. #include "ash/keyboard/ui/keyboard_ukm_recorder.h"
  17. #include "ash/keyboard/ui/notification_manager.h"
  18. #include "ash/keyboard/ui/queued_container_type.h"
  19. #include "ash/keyboard/ui/queued_display_change.h"
  20. #include "ash/public/cpp/keyboard/keyboard_config.h"
  21. #include "ash/public/cpp/keyboard/keyboard_types.h"
  22. #include "base/observer_list.h"
  23. #include "base/scoped_observation.h"
  24. #include "base/time/time.h"
  25. #include "ui/aura/window_observer.h"
  26. #include "ui/base/ime/input_method.h"
  27. #include "ui/base/ime/input_method_observer.h"
  28. #include "ui/base/ime/text_input_type.h"
  29. #include "ui/base/ime/virtual_keyboard_controller.h"
  30. #include "ui/events/event.h"
  31. #include "ui/events/gestures/gesture_types.h"
  32. #include "ui/gfx/geometry/rect.h"
  33. #include "ui/gfx/geometry/vector2d.h"
  34. namespace aura {
  35. class Window;
  36. }
  37. namespace ash {
  38. class KeyboardControllerObserver;
  39. }
  40. namespace ui {
  41. class TextInputClient;
  42. } // namespace ui
  43. namespace keyboard {
  44. class CallbackAnimationObserver;
  45. class KeyboardUI;
  46. class KeyboardUIFactory;
  47. // Provides control of the virtual keyboard, including enabling/disabling the
  48. // keyboard and controlling its visibility.
  49. // TODO(shend): Move non-UI related logic to KeyboardControllerImpl.
  50. class KEYBOARD_EXPORT KeyboardUIController
  51. : public ui::InputMethodObserver,
  52. public aura::WindowObserver,
  53. public ContainerBehavior::Delegate {
  54. public:
  55. KeyboardUIController();
  56. KeyboardUIController(const KeyboardUIController&) = delete;
  57. KeyboardUIController& operator=(const KeyboardUIController&) = delete;
  58. ~KeyboardUIController() override;
  59. // Initialize the virtual keyboard controller with two delegates:
  60. // - ui_factory: Responsible for keyboard window loading.
  61. // - layout_delegate: Responsible for moving keyboard window across displays.
  62. void Initialize(std::unique_ptr<KeyboardUIFactory> ui_factory,
  63. KeyboardLayoutDelegate* layout_delegate);
  64. // Resets all the flags and disables the virtual keyboard.
  65. void Shutdown();
  66. // Retrieves the active keyboard controller. Guaranteed to not be null while
  67. // there is an ash::Shell.
  68. // TODO(stevenjb/shuchen/shend): Remove all access from src/chrome.
  69. // https://crbug.com/843332.
  70. // TODO(shend): Make this not a singleton.
  71. static KeyboardUIController* Get();
  72. // Returns true if there is a valid KeyboardUIController instance (e.g. while
  73. // there is an ash::Shell).
  74. static bool HasInstance();
  75. // Returns the keyboard window, or null if the keyboard window has not been
  76. // created yet.
  77. aura::Window* GetKeyboardWindow() const;
  78. // Returns the gesture consumer for the keyboard, or null if the keyboard
  79. // window has not been created yet.
  80. ui::GestureConsumer* GetGestureConsumer() const;
  81. // Returns the root window that this keyboard controller is attached to, or
  82. // null if the keyboard has not been attached to any root window.
  83. aura::Window* GetRootWindow() const;
  84. // Move the keyboard window to a different parent container. |parent| must not
  85. // be null.
  86. void MoveToParentContainer(aura::Window* parent);
  87. // Sets the bounds of the keyboard window, relative to the root window.
  88. void SetKeyboardWindowBounds(const gfx::Rect& new_bounds_in_root);
  89. // Reloads the content of the keyboard. No-op if the keyboard content is not
  90. // loaded yet.
  91. void Reload();
  92. // Rebuilds the keyboard by disabling and enabling it again.
  93. // TODO(https://crbug.com/845780): Can this be replaced with |Reload|?
  94. void RebuildKeyboardIfEnabled();
  95. // Management of the observer list.
  96. void AddObserver(ash::KeyboardControllerObserver* observer);
  97. bool HasObserver(ash::KeyboardControllerObserver* observer) const;
  98. void RemoveObserver(ash::KeyboardControllerObserver* observer);
  99. // Updates |keyboard_config_| with |config|. Returns |false| if there is no
  100. // change, otherwise returns true and notifies observers if this is enabled.
  101. bool UpdateKeyboardConfig(const KeyboardConfig& config);
  102. const KeyboardConfig& keyboard_config() { return keyboard_config_; }
  103. // Sets and clears |keyboard_enable_flags_| entries.
  104. void SetEnableFlag(KeyboardEnableFlag flag);
  105. void ClearEnableFlag(KeyboardEnableFlag flag);
  106. bool IsEnableFlagSet(KeyboardEnableFlag flag) const;
  107. const std::set<KeyboardEnableFlag>& keyboard_enable_flags() const {
  108. return keyboard_enable_flags_;
  109. }
  110. // Returns true if keyboard overscroll is enabled.
  111. bool IsKeyboardOverscrollEnabled() const;
  112. // Hide the keyboard because the user has chosen to specifically hide the
  113. // keyboard, such as pressing the dismiss button.
  114. // TODO(https://crbug.com/845780): Rename this to
  115. // HideKeyboardExplicitlyByUser.
  116. // TODO(https://crbug.com/845780): Audit and switch callers to
  117. // HideKeyboardImplicitlyByUser where appropriate.
  118. void HideKeyboardByUser();
  119. // Hide the keyboard as a secondary effect of a user action, such as tapping
  120. // the shelf. The keyboard should not hide if it's locked.
  121. void HideKeyboardImplicitlyByUser();
  122. // Hide the keyboard due to some internally generated change to change the
  123. // state of the keyboard. For example, moving from the docked keyboard to the
  124. // floating keyboard.
  125. void HideKeyboardTemporarilyForTransition();
  126. // Hide the keyboard as an effect of a system action, such as opening the
  127. // settings page from the keyboard. There should be no reason the keyboard
  128. // should remain open.
  129. void HideKeyboardExplicitlyBySystem();
  130. // Hide the keyboard as a secondary effect of a system action, such as losing
  131. // focus of a text element. If focus is returned to any text element, it is
  132. // desirable to re-show the keyboard in this case.
  133. void HideKeyboardImplicitlyBySystem();
  134. // Force the keyboard to show up if not showing and lock the keyboard if
  135. // |lock| is true.
  136. void ShowKeyboard(bool lock);
  137. // Force the keyboard to show up in the specific display if not showing and
  138. // lock the keyboard
  139. void ShowKeyboardInDisplay(const display::Display& display);
  140. // Returns the bounds in root window for the visible portion of the keyboard.
  141. // An empty rectangle will get returned when the keyboard is hidden.
  142. const gfx::Rect& visual_bounds_in_root() const {
  143. return visual_bounds_in_root_;
  144. }
  145. // Returns the bounds in screen for the visible portion of the keyboard. An
  146. // empty rectangle will get returned when the keyboard is hidden.
  147. gfx::Rect GetVisualBoundsInScreen() const;
  148. // Returns the current bounds that affect the workspace layout. If the
  149. // keyboard is not shown or if the keyboard mode should not affect the usable
  150. // region of the screen, an empty rectangle will be returned. Bounds are
  151. // relative to the screen.
  152. gfx::Rect GetWorkspaceOccludedBoundsInScreen() const;
  153. // Returns the current bounds that affect the window layout of the various
  154. // lock screens. Bounds are relative to the root window.
  155. gfx::Rect GetKeyboardLockScreenOffsetBounds() const;
  156. // Set the area on the keyboard window that occlude whatever is behind it.
  157. void SetOccludedBounds(const gfx::Rect& bounds_in_window);
  158. // Set the areas on the keyboard window where events should be handled.
  159. // Does not do anything if there is no keyboard window. Bounds are relative to
  160. // the keyboard window.
  161. void SetHitTestBounds(const std::vector<gfx::Rect>& bounds_in_window);
  162. // Sets the area of the keyboard window that should not move off screen. Any
  163. // area outside of this can be moved off the user's screen. Note the bounds
  164. // here are relative to the window's origin.
  165. bool SetAreaToRemainOnScreen(const gfx::Rect& bounds_in_window);
  166. // Sets the bounds of the keyboard window in screen coordinates.
  167. bool SetKeyboardWindowBoundsInScreen(const gfx::Rect& bounds_in_screen);
  168. ContainerType GetActiveContainerType() const {
  169. return container_behavior_->GetType();
  170. }
  171. // Adjusts |requested_bounds| according to the current container behavior.
  172. // (e.g. prevent the keyboard from moving off screen).
  173. gfx::Rect AdjustSetBoundsRequest(
  174. const gfx::Rect& display_bounds,
  175. const gfx::Rect& requested_bounds_in_screen) const;
  176. // Returns true if overscroll is currently allowed by the active keyboard
  177. // container behavior.
  178. bool IsOverscrollAllowed() const;
  179. // Whether the keyboard has been enabled, i.e. EnableKeyboard() has been
  180. // called.
  181. bool IsEnabled() const { return ui_ != nullptr; }
  182. // Handle mouse and touch events on the keyboard. The effects of this method
  183. // will not stop propagation to the keyboard extension.
  184. bool HandlePointerEvent(const ui::LocatedEvent& event);
  185. bool HandleGestureEvent(const ui::GestureEvent& event);
  186. // Sets the active container type. If the keyboard is currently shown, this
  187. // will trigger a hide animation and a subsequent show animation. Otherwise
  188. // the ContainerBehavior change is synchronous.
  189. void SetContainerType(ContainerType type,
  190. const gfx::Rect& target_bounds_in_root,
  191. base::OnceCallback<void(bool)> callback);
  192. // Sets floating keyboard draggable rect.
  193. void SetDraggableArea(const gfx::Rect& rect);
  194. bool IsKeyboardVisible();
  195. void KeyboardContentsLoaded();
  196. aura::Window* parent_container() { return parent_container_; }
  197. ui::VirtualKeyboardController* virtual_keyboard_controller() {
  198. return virtual_keyboard_controller_.get();
  199. }
  200. bool keyboard_locked() const { return keyboard_locked_; }
  201. void set_keyboard_locked(bool lock) { keyboard_locked_ = lock; }
  202. void set_container_behavior_for_test(
  203. std::unique_ptr<ContainerBehavior> container_behavior) {
  204. container_behavior_ = std::move(container_behavior);
  205. }
  206. KeyboardUIState GetStateForTest() const { return model_.state(); }
  207. ui::InputMethod* GetInputMethodForTest();
  208. void EnsureCaretInWorkAreaForTest(const gfx::Rect& occluded_bounds_in_screen);
  209. private:
  210. // For access to Observer methods for simulation.
  211. friend class KeyboardUIControllerTest;
  212. // Different ways to hide the keyboard.
  213. enum HideReason {
  214. // System initiated due to an active event, where the user does not want
  215. // to maintain any association with the previous text entry session.
  216. HIDE_REASON_SYSTEM_EXPLICIT,
  217. // System initiated due to a passive event, such as clicking on a non-text
  218. // control in a web page. Implicit hide events can be treated as passive
  219. // and can possibly be a transient loss of focus. This will generally cause
  220. // the keyboard to stay open for a brief moment and then hide, and possibly
  221. // come back if focus is regained within a short amount of time (transient
  222. // blur).
  223. HIDE_REASON_SYSTEM_IMPLICIT,
  224. // Keyboard is hidden temporarily for transitional reasons. Examples include
  225. // moving the keyboard to a different display (which closes it and re-opens
  226. // it on the new screen) or changing the container type (e.g. full-width to
  227. // floating)
  228. HIDE_REASON_SYSTEM_TEMPORARY,
  229. // User explicitly hiding the keyboard via the close button. Also hides
  230. // locked keyboards.
  231. HIDE_REASON_USER_EXPLICIT,
  232. // Keyboard is hidden as an indirect consequence of some user action.
  233. // Examples include opening the window overview mode, or tapping on the
  234. // shelf status area. Does not hide locked keyboards.
  235. HIDE_REASON_USER_IMPLICIT,
  236. };
  237. // ContainerBehavior::Delegate overrides
  238. bool IsKeyboardLocked() const override;
  239. gfx::Rect GetBoundsInScreen() const override;
  240. void MoveKeyboardWindow(const gfx::Rect& new_bounds) override;
  241. void MoveKeyboardWindowToDisplay(const display::Display& display,
  242. const gfx::Rect& new_bounds) override;
  243. void TransferGestureEventToShelf(const ui::GestureEvent& e) override;
  244. // aura::WindowObserver overrides
  245. void OnWindowAddedToRootWindow(aura::Window* window) override;
  246. void OnWindowBoundsChanged(aura::Window* window,
  247. const gfx::Rect& old_bounds,
  248. const gfx::Rect& new_bounds,
  249. ui::PropertyChangeReason reason) override;
  250. // InputMethodObserver overrides
  251. void OnBlur() override {}
  252. void OnCaretBoundsChanged(const ui::TextInputClient* client) override {}
  253. void OnFocus() override {}
  254. void OnInputMethodDestroyed(const ui::InputMethod* input_method) override;
  255. void OnTextInputStateChanged(const ui::TextInputClient* client) override;
  256. void OnVirtualKeyboardVisibilityChangedIfEnabled(bool should_show) override;
  257. // Enables the virtual keyboard.
  258. // Immediately starts pre-loading the keyboard window in the background.
  259. // Does nothing if the keyboard is already enabled.
  260. void EnableKeyboard();
  261. // Disables the virtual keyboard. Resets the keyboard to its initial disabled
  262. // state and destroys the keyboard window.
  263. // Does nothing if the keyboard is already disabled.
  264. void DisableKeyboard();
  265. // Returns true if the keyboard should be enabled, i.e. the current result
  266. // of Set/ClearEnableFlag should cause the keyboard to be enabled.
  267. bool IsKeyboardEnableRequested() const;
  268. // Enables or disables the keyboard based on |IsKeyboardEnableRequested|,
  269. // as requested by |flag|.
  270. void UpdateKeyboardAsRequestedBy(KeyboardEnableFlag flag);
  271. // Attach the keyboard window as a child of the given parent window.
  272. // Can only be called when the keyboard is not activated. |parent| must not
  273. // have any children.
  274. void ActivateKeyboardInContainer(aura::Window* parent);
  275. // Detach the keyboard window from its parent container window.
  276. // Can only be called when the keyboard is activated. Explicitly hides the
  277. // keyboard if it is currently visible.
  278. void DeactivateKeyboard();
  279. // Loads the keyboard window in the background, but does not display
  280. // the keyboard.
  281. void LoadKeyboardWindowInBackground();
  282. // Show virtual keyboard immediately with animation.
  283. void ShowKeyboardInternal(aura::Window* target_container);
  284. void PopulateKeyboardContent(aura::Window* target_container);
  285. // Returns true if keyboard is scheduled to hide.
  286. bool WillHideKeyboard() const;
  287. // Immediately starts hiding animation of virtual keyboard and notifies
  288. // observers bounds change. This method forcibly sets keyboard_locked_
  289. // false while closing the keyboard.
  290. void HideKeyboard(HideReason reason);
  291. // Called when the hide animation finished.
  292. void HideAnimationFinished();
  293. // Called when the show animation finished.
  294. void ShowAnimationFinished();
  295. // Notifies keyboard config change to the observers.
  296. // Only called from |UpdateKeyboardConfig| in keyboard_util.
  297. void NotifyKeyboardConfigChanged();
  298. // Notifies observers that the visual or occluded bounds of the keyboard
  299. // window are changing.
  300. void NotifyKeyboardBoundsChanging(const gfx::Rect& new_bounds_in_root);
  301. // Called when the keyboard window has loaded. Shows the keyboard if
  302. // |show_on_keyboard_window_load_| is true.
  303. void NotifyKeyboardWindowLoaded();
  304. // Validates the state transition. Called from ChangeState.
  305. void CheckStateTransition(KeyboardUIState prev, KeyboardUIState next);
  306. // Changes the current state and validates the transition.
  307. void ChangeState(KeyboardUIState state);
  308. // Reports error histogram in case lingering in an intermediate state.
  309. void ReportLingeringState();
  310. // Shows the keyboard if the last time the keyboard was hidden was a small
  311. // time ago.
  312. void ShowKeyboardIfWithinTransientBlurThreshold();
  313. void SetContainerBehaviorInternal(ContainerType type);
  314. // Records that keyboard was shown on the currently focused UKM source.
  315. void RecordUkmKeyboardShown();
  316. // Gets the currently focused text input client.
  317. ui::TextInputClient* GetTextInputClient();
  318. // Ensures that the current IME is observed if it is changed.
  319. void UpdateInputMethodObserver();
  320. // Ensures caret in current work area (not occluded by virtual keyboard
  321. // window).
  322. void EnsureCaretInWorkArea(const gfx::Rect& occluded_bounds_in_screen);
  323. // Marks that the keyboard load has started. This is used to measure the time
  324. // it takes to fully load the keyboard. This should be called before
  325. // MarkKeyboardLoadFinished.
  326. void MarkKeyboardLoadStarted();
  327. // Marks that the keyboard load has ended. This finishes measuring that the
  328. // keyboard is loaded.
  329. void MarkKeyboardLoadFinished();
  330. // Called when the enable flags change. Notifies observers of the change.
  331. void EnableFlagsChanged();
  332. std::unique_ptr<KeyboardUIFactory> ui_factory_;
  333. std::unique_ptr<KeyboardUI> ui_;
  334. std::unique_ptr<ui::VirtualKeyboardController> virtual_keyboard_controller_;
  335. KeyboardLayoutDelegate* layout_delegate_ = nullptr;
  336. base::ScopedObservation<ui::InputMethod, ui::InputMethodObserver>
  337. ime_observation_{this};
  338. // Container window that the keyboard window is a child of.
  339. aura::Window* parent_container_ = nullptr;
  340. // CallbackAnimationObserver should be destroyed before |ui_| because it uses
  341. // |ui_|'s animator.
  342. std::unique_ptr<CallbackAnimationObserver> animation_observer_;
  343. // Current active visual behavior for the keyboard container.
  344. std::unique_ptr<ContainerBehavior> container_behavior_;
  345. std::unique_ptr<QueuedContainerType> queued_container_type_;
  346. std::unique_ptr<QueuedDisplayChange> queued_display_change_;
  347. // If true, show the keyboard window when it loads.
  348. bool show_on_keyboard_window_load_ = false;
  349. // If true, the keyboard is always visible even if no window has input focus.
  350. // TODO(https://crbug.com/964191): Move this to the UI model.
  351. bool keyboard_locked_ = false;
  352. KeyboardEventHandler event_handler_;
  353. base::ObserverList<ash::KeyboardControllerObserver>::Unchecked observer_list_;
  354. KeyboardUIModel model_;
  355. // The bounds for the visible portion of the keyboard, relative to the root
  356. // window. If the keyboard window is visible, this should be the same size as
  357. // the keyboard window. If not, this should be empty.
  358. gfx::Rect visual_bounds_in_root_;
  359. // Keyboard configuration associated with the controller.
  360. KeyboardConfig keyboard_config_;
  361. // Set of active enabled request flags. Used to determine whether the keyboard
  362. // should be enabled.
  363. std::set<KeyboardEnableFlag> keyboard_enable_flags_;
  364. NotificationManager notification_manager_;
  365. base::Time time_of_last_blur_ = base::Time::UnixEpoch();
  366. DisplayUtil display_util_;
  367. bool keyboard_load_time_logged_ = false;
  368. base::Time keyboard_load_time_start_;
  369. base::WeakPtrFactory<KeyboardUIController>
  370. weak_factory_report_lingering_state_{this};
  371. base::WeakPtrFactory<KeyboardUIController> weak_factory_will_hide_{this};
  372. };
  373. } // namespace keyboard
  374. #endif // ASH_KEYBOARD_UI_KEYBOARD_UI_CONTROLLER_H_