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 void SetKeyboardMode(KeyboardMode mode
);
97 // Force the keyboard to show up if not showing and lock the keyboard if
99 void ShowKeyboard(bool lock
);
101 // Sets the active keyboard controller. KeyboardController takes ownership of
102 // the instance. Calling ResetIntance with a new instance destroys the
103 // previous one. May be called with NULL to clear the instance.
104 static void ResetInstance(KeyboardController
* controller
);
106 // Retrieve the active keyboard controller.
107 static KeyboardController
* GetInstance();
109 // Returns true if keyboard is currently visible.
110 bool keyboard_visible() { return keyboard_visible_
; }
112 bool show_on_resize() { return show_on_resize_
; }
114 // Returns the current keyboard bounds. When the keyboard is not shown,
115 // an empty rectangle will get returned.
116 const gfx::Rect
& current_keyboard_bounds() {
117 return current_keyboard_bounds_
;
120 // Determines whether a particular window should have insets for overscroll.
121 bool ShouldEnableInsets(aura::Window
* window
);
123 // Updates insets on web content window
124 void UpdateWindowInsets(aura::Window
* window
);
127 // For access to Observer methods for simulation.
128 friend class KeyboardControllerTest
;
130 // aura::WindowObserver overrides
131 void OnWindowHierarchyChanged(const HierarchyChangeParams
& params
) override
;
133 // InputMethodObserver overrides
134 void OnTextInputTypeChanged(const ui::TextInputClient
* client
) override
{}
135 void OnFocus() override
{}
136 void OnBlur() override
{}
137 void OnCaretBoundsChanged(const ui::TextInputClient
* client
) override
{}
138 void OnTextInputStateChanged(const ui::TextInputClient
* client
) override
;
139 void OnInputMethodDestroyed(const ui::InputMethod
* input_method
) override
;
140 void OnShowImeIfNeeded() override
;
142 // Show virtual keyboard immediately with animation.
143 void ShowKeyboardInternal();
145 // Clears any insets on web content windows.
146 void ResetWindowInsets();
148 // Returns true if keyboard is scheduled to hide.
149 bool WillHideKeyboard() const;
151 // Called when show and hide animation finished successfully. If the animation
152 // is aborted, it won't be called.
153 void ShowAnimationFinished();
154 void HideAnimationFinished();
156 // Adds an observer for tracking changes to a window size or
157 // position while the keyboard is displayed. Any window repositioning
158 // invalidates insets for overscrolling.
159 void AddBoundsChangedObserver(aura::Window
* window
);
161 scoped_ptr
<KeyboardControllerProxy
> proxy_
;
162 scoped_ptr
<aura::Window
> container_
;
163 // CallbackAnimationObserver should destructed before container_ because it
164 // uses container_'s animator.
165 scoped_ptr
<CallbackAnimationObserver
> animation_observer_
;
167 scoped_ptr
<WindowBoundsChangeObserver
> window_bounds_observer_
;
169 ui::InputMethod
* input_method_
;
170 bool keyboard_visible_
;
171 bool show_on_resize_
;
173 KeyboardMode keyboard_mode_
;
174 ui::TextInputType type_
;
176 ObserverList
<KeyboardControllerObserver
> observer_list_
;
178 // The currently used keyboard position.
179 gfx::Rect current_keyboard_bounds_
;
181 static KeyboardController
* instance_
;
183 base::WeakPtrFactory
<KeyboardController
> weak_factory_
;
185 DISALLOW_COPY_AND_ASSIGN(KeyboardController
);
188 } // namespace keyboard
190 #endif // UI_KEYBOARD_KEYBOARD_CONTROLLER_H_