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/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 // Provides control of the virtual keyboard, including providing a container
35 // and controlling visibility.
36 class KEYBOARD_EXPORT KeyboardController
: public ui::InputMethodObserver
,
37 public aura::WindowObserver
{
39 // Different ways to hide the keyboard.
42 HIDE_REASON_AUTOMATIC
,
47 // Takes ownership of |proxy|.
48 explicit KeyboardController(KeyboardControllerProxy
* proxy
);
49 virtual ~KeyboardController();
51 // Returns the container for the keyboard, which is owned by
52 // KeyboardController.
53 aura::Window
* GetContainerWindow();
55 // Whether the container window for the keyboard has been initialized.
56 bool keyboard_container_initialized() const {
57 return container_
.get() != NULL
;
60 // Reloads the content of the keyboard. No-op if the keyboard content is not
64 // Hides virtual keyboard and notifies observer bounds change.
65 // This function should be called with a delay to avoid layout flicker
66 // when the focus of input field quickly change. |automatic| is true when the
67 // call is made by the system rather than initiated by the user.
68 void HideKeyboard(HideReason reason
);
70 // Notifies the keyboard observer for keyboard bounds changed.
71 void NotifyKeyboardBoundsChanging(const gfx::Rect
& new_bounds
);
73 // Management of the observer list.
74 virtual void AddObserver(KeyboardControllerObserver
* observer
);
75 virtual void RemoveObserver(KeyboardControllerObserver
* observer
);
77 KeyboardControllerProxy
* proxy() { return proxy_
.get(); }
79 void set_lock_keyboard(bool lock
) { lock_keyboard_
= lock
; }
81 // Force the keyboard to show up if not showing and lock the keyboard if
83 void ShowKeyboard(bool lock
);
85 // Sets the active keyboard controller. KeyboardController takes ownership of
86 // the instance. Calling ResetIntance with a new instance destroys the
87 // previous one. May be called with NULL to clear the instance.
88 static void ResetInstance(KeyboardController
* controller
);
90 // Retrieve the active keyboard controller.
91 static KeyboardController
* GetInstance();
93 // Returns true if keyboard is currently visible.
94 bool keyboard_visible() { return keyboard_visible_
; }
96 bool show_on_resize() { return show_on_resize_
; }
98 // Returns the current keyboard bounds. When the keyboard is not shown,
99 // an empty rectangle will get returned.
100 const gfx::Rect
& current_keyboard_bounds() {
101 return current_keyboard_bounds_
;
104 // Updates insets on web content window
105 void UpdateWindowInsets(aura::Window
* window
);
108 // For access to Observer methods for simulation.
109 friend class KeyboardControllerTest
;
111 // aura::WindowObserver overrides
112 virtual void OnWindowHierarchyChanged(
113 const HierarchyChangeParams
& params
) OVERRIDE
;
115 // InputMethodObserver overrides
116 virtual void OnTextInputTypeChanged(
117 const ui::TextInputClient
* client
) OVERRIDE
{}
118 virtual void OnFocus() OVERRIDE
{}
119 virtual void OnBlur() OVERRIDE
{}
120 virtual void OnCaretBoundsChanged(
121 const ui::TextInputClient
* client
) OVERRIDE
{}
122 virtual void OnTextInputStateChanged(
123 const ui::TextInputClient
* client
) OVERRIDE
;
124 virtual void OnInputMethodDestroyed(
125 const ui::InputMethod
* input_method
) OVERRIDE
;
126 virtual void OnShowImeIfNeeded() OVERRIDE
;
128 // Show virtual keyboard immediately with animation.
129 void ShowKeyboardInternal();
131 // Clears any insets on web content windows.
132 void ResetWindowInsets();
134 // Returns true if keyboard is scheduled to hide.
135 bool WillHideKeyboard() const;
137 // Called when show and hide animation finished successfully. If the animation
138 // is aborted, it won't be called.
139 void ShowAnimationFinished();
140 void HideAnimationFinished();
142 // Adds or removes an observer for tracking changes to a window size or
143 // position while the keyboard is displayed. Any window repositioning
144 // invalidates insets for overscrolling.
145 void AddBoundsChangedObserver(aura::Window
* window
);
146 void RemoveBoundsChangedObserver(aura::Window
* window
);
148 scoped_ptr
<KeyboardControllerProxy
> proxy_
;
149 scoped_ptr
<aura::Window
> container_
;
150 // CallbackAnimationObserver should destructed before container_ because it
151 // uses container_'s animator.
152 scoped_ptr
<CallbackAnimationObserver
> animation_observer_
;
154 scoped_ptr
<WindowBoundsChangeObserver
> window_bounds_observer_
;
156 ui::InputMethod
* input_method_
;
157 bool keyboard_visible_
;
158 bool show_on_resize_
;
160 ui::TextInputType type_
;
162 ObserverList
<KeyboardControllerObserver
> observer_list_
;
164 base::WeakPtrFactory
<KeyboardController
> weak_factory_
;
166 // The currently used keyboard position.
167 gfx::Rect current_keyboard_bounds_
;
169 static KeyboardController
* instance_
;
171 DISALLOW_COPY_AND_ASSIGN(KeyboardController
);
174 } // namespace keyboard
176 #endif // UI_KEYBOARD_KEYBOARD_CONTROLLER_H_