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 KeyboardControllerObserver
;
31 class KeyboardControllerProxy
;
33 // Provides control of the virtual keyboard, including providing a container
34 // and controlling visibility.
35 class KEYBOARD_EXPORT KeyboardController
: public ui::InputMethodObserver
,
36 public aura::WindowObserver
{
38 // Different ways to hide the keyboard.
41 HIDE_REASON_AUTOMATIC
,
46 // Takes ownership of |proxy|.
47 explicit KeyboardController(KeyboardControllerProxy
* proxy
);
48 virtual ~KeyboardController();
50 // Returns the container for the keyboard, which is owned by
51 // KeyboardController.
52 aura::Window
* GetContainerWindow();
54 // Whether the container window for the keyboard has been initialized.
55 bool keyboard_container_initialized() const {
56 return container_
.get() != NULL
;
59 // Reloads the content of the keyboard. No-op if the keyboard content is not
63 // Hides virtual keyboard and notifies observer bounds change.
64 // This function should be called with a delay to avoid layout flicker
65 // when the focus of input field quickly change. |automatic| is true when the
66 // call is made by the system rather than initiated by the user.
67 void HideKeyboard(HideReason reason
);
69 // Notifies the keyboard observer for keyboard bounds changed.
70 void NotifyKeyboardBoundsChanging(const gfx::Rect
& new_bounds
);
72 // Management of the observer list.
73 virtual void AddObserver(KeyboardControllerObserver
* observer
);
74 virtual void RemoveObserver(KeyboardControllerObserver
* observer
);
76 KeyboardControllerProxy
* proxy() { return proxy_
.get(); }
78 void set_lock_keyboard(bool lock
) { lock_keyboard_
= lock
; }
80 // Force the keyboard to show up if not showing and lock the keyboard if
82 void ShowKeyboard(bool lock
);
84 // Sets the active keyboard controller. KeyboardController takes ownership of
85 // the instance. Calling ResetIntance with a new instance destroys the
86 // previous one. May be called with NULL to clear the instance.
87 static void ResetInstance(KeyboardController
* controller
);
89 // Retrieve the active keyboard controller.
90 static KeyboardController
* GetInstance();
92 // Returns true if keyboard is currently visible.
93 bool keyboard_visible() { return keyboard_visible_
; }
95 // Returns the current keyboard bounds. When the keyboard is not shown,
96 // an empty rectangle will get returned.
97 const gfx::Rect
& current_keyboard_bounds() {
98 return current_keyboard_bounds_
;
102 // For access to Observer methods for simulation.
103 friend class KeyboardControllerTest
;
105 // aura::WindowObserver overrides
106 virtual void OnWindowHierarchyChanged(
107 const HierarchyChangeParams
& params
) OVERRIDE
;
109 // InputMethodObserver overrides
110 virtual void OnTextInputTypeChanged(
111 const ui::TextInputClient
* client
) OVERRIDE
{}
112 virtual void OnFocus() OVERRIDE
{}
113 virtual void OnBlur() OVERRIDE
{}
114 virtual void OnCaretBoundsChanged(
115 const ui::TextInputClient
* client
) OVERRIDE
{}
116 virtual void OnTextInputStateChanged(
117 const ui::TextInputClient
* client
) OVERRIDE
;
118 virtual void OnInputMethodDestroyed(
119 const ui::InputMethod
* input_method
) OVERRIDE
;
120 virtual void OnShowImeIfNeeded() OVERRIDE
;
122 // Show virtual keyboard immediately with animation.
123 void ShowKeyboardInternal();
125 // Clears any insets on web content windows.
126 void ResetWindowInsets();
128 // Returns true if keyboard is scheduled to hide.
129 bool WillHideKeyboard() const;
131 // Called when show and hide animation finished successfully. If the animation
132 // is aborted, it won't be called.
133 void ShowAnimationFinished();
134 void HideAnimationFinished();
136 scoped_ptr
<KeyboardControllerProxy
> proxy_
;
137 scoped_ptr
<aura::Window
> container_
;
138 // CallbackAnimationObserver should destructed before container_ because it
139 // uses container_'s animator.
140 scoped_ptr
<CallbackAnimationObserver
> animation_observer_
;
142 ui::InputMethod
* input_method_
;
143 bool keyboard_visible_
;
145 ui::TextInputType type_
;
147 ObserverList
<KeyboardControllerObserver
> observer_list_
;
149 base::WeakPtrFactory
<KeyboardController
> weak_factory_
;
151 // The currently used keyboard position.
152 gfx::Rect current_keyboard_bounds_
;
154 static KeyboardController
* instance_
;
156 DISALLOW_COPY_AND_ASSIGN(KeyboardController
);
159 } // namespace keyboard
161 #endif // UI_KEYBOARD_KEYBOARD_CONTROLLER_H_