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.
5 #ifndef UI_KEYBOARD_KEYBOARD_CONTROLLER_H_
6 #define UI_KEYBOARD_KEYBOARD_CONTROLLER_H_
8 #include "base/basictypes.h"
9 #include "base/event_types.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/observer_list.h"
12 #include "ui/aura/window_observer.h"
13 #include "ui/base/ime/input_method_observer.h"
14 #include "ui/base/ime/text_input_type.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/keyboard/keyboard_export.h"
24 class TextInputClient
;
29 class CallbackAnimationObserver
;
30 class WindowBoundsChangeObserver
;
31 class KeyboardControllerObserver
;
32 class KeyboardControllerProxy
;
34 // Animation distance.
35 const int kAnimationDistance
= 30;
40 // Full width virtual keyboard. The virtual keyboard window has the same width
43 // Floating virtual keyboard. The virtual keyboard window has customizable
44 // width and is draggable.
48 // Provides control of the virtual keyboard, including providing a container
49 // and controlling visibility.
50 class KEYBOARD_EXPORT KeyboardController
: public ui::InputMethodObserver
,
51 public aura::WindowObserver
{
53 // Different ways to hide the keyboard.
56 HIDE_REASON_AUTOMATIC
,
61 // Takes ownership of |proxy|.
62 explicit KeyboardController(KeyboardControllerProxy
* proxy
);
63 ~KeyboardController() override
;
65 // Returns the container for the keyboard, which is owned by
66 // KeyboardController.
67 aura::Window
* GetContainerWindow();
69 // Whether the container window for the keyboard has been initialized.
70 bool keyboard_container_initialized() const {
71 return container_
.get() != NULL
;
74 // Reloads the content of the keyboard. No-op if the keyboard content is not
78 // Hides virtual keyboard and notifies observer bounds change.
79 // This function should be called with a delay to avoid layout flicker
80 // when the focus of input field quickly change. |automatic| is true when the
81 // call is made by the system rather than initiated by the user.
82 void HideKeyboard(HideReason reason
);
84 // Notifies the keyboard observer for keyboard bounds changed.
85 void NotifyKeyboardBoundsChanging(const gfx::Rect
& new_bounds
);
87 // Management of the observer list.
88 virtual void AddObserver(KeyboardControllerObserver
* observer
);
89 virtual void RemoveObserver(KeyboardControllerObserver
* observer
);
91 KeyboardControllerProxy
* proxy() { return proxy_
.get(); }
93 void set_lock_keyboard(bool lock
) { lock_keyboard_
= lock
; }
95 KeyboardMode
keyboard_mode() const { return keyboard_mode_
; }
97 void SetKeyboardMode(KeyboardMode mode
);
99 // Force the keyboard to show up if not showing and lock the keyboard if
101 void ShowKeyboard(bool lock
);
103 // Sets the active keyboard controller. KeyboardController takes ownership of
104 // the instance. Calling ResetIntance with a new instance destroys the
105 // previous one. May be called with NULL to clear the instance.
106 static void ResetInstance(KeyboardController
* controller
);
108 // Retrieve the active keyboard controller.
109 static KeyboardController
* GetInstance();
111 // Returns true if keyboard is currently visible.
112 bool keyboard_visible() { return keyboard_visible_
; }
114 bool show_on_resize() { return show_on_resize_
; }
116 // Returns the current keyboard bounds. When the keyboard is not shown,
117 // an empty rectangle will get returned.
118 const gfx::Rect
& current_keyboard_bounds() {
119 return current_keyboard_bounds_
;
122 // Determines whether a particular window should have insets for overscroll.
123 bool ShouldEnableInsets(aura::Window
* window
);
125 // Updates insets on web content window
126 void UpdateWindowInsets(aura::Window
* window
);
129 // For access to Observer methods for simulation.
130 friend class KeyboardControllerTest
;
132 // aura::WindowObserver overrides
133 void OnWindowHierarchyChanged(const HierarchyChangeParams
& params
) override
;
134 void OnWindowAddedToRootWindow(aura::Window
* window
) override
;
135 void OnWindowRemovingFromRootWindow(aura::Window
* window
,
136 aura::Window
* new_root
) override
;
137 void OnWindowBoundsChanged(aura::Window
* window
,
138 const gfx::Rect
& old_bounds
,
139 const gfx::Rect
& new_bounds
) override
;
141 // InputMethodObserver overrides
142 void OnTextInputTypeChanged(const ui::TextInputClient
* client
) override
{}
143 void OnFocus() override
{}
144 void OnBlur() override
{}
145 void OnCaretBoundsChanged(const ui::TextInputClient
* client
) override
{}
146 void OnTextInputStateChanged(const ui::TextInputClient
* client
) override
;
147 void OnInputMethodDestroyed(const ui::InputMethod
* input_method
) override
;
148 void OnShowImeIfNeeded() override
;
150 // Show virtual keyboard immediately with animation.
151 void ShowKeyboardInternal();
153 // Clears any insets on web content windows.
154 void ResetWindowInsets();
156 // Returns true if keyboard is scheduled to hide.
157 bool WillHideKeyboard() const;
159 // Called when show and hide animation finished successfully. If the animation
160 // is aborted, it won't be called.
161 void ShowAnimationFinished();
162 void HideAnimationFinished();
164 // Adds an observer for tracking changes to a window size or
165 // position while the keyboard is displayed. Any window repositioning
166 // invalidates insets for overscrolling.
167 void AddBoundsChangedObserver(aura::Window
* window
);
169 scoped_ptr
<KeyboardControllerProxy
> proxy_
;
170 scoped_ptr
<aura::Window
> container_
;
171 // CallbackAnimationObserver should destructed before container_ because it
172 // uses container_'s animator.
173 scoped_ptr
<CallbackAnimationObserver
> animation_observer_
;
175 scoped_ptr
<WindowBoundsChangeObserver
> window_bounds_observer_
;
177 ui::InputMethod
* input_method_
;
178 bool keyboard_visible_
;
179 bool show_on_resize_
;
181 KeyboardMode keyboard_mode_
;
182 ui::TextInputType type_
;
184 ObserverList
<KeyboardControllerObserver
> observer_list_
;
186 // The currently used keyboard position.
187 gfx::Rect current_keyboard_bounds_
;
189 static KeyboardController
* instance_
;
191 base::WeakPtrFactory
<KeyboardController
> weak_factory_
;
193 DISALLOW_COPY_AND_ASSIGN(KeyboardController
);
196 } // namespace keyboard
198 #endif // UI_KEYBOARD_KEYBOARD_CONTROLLER_H_