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;
37 // Provides control of the virtual keyboard, including providing a container
38 // and controlling visibility.
39 class KEYBOARD_EXPORT KeyboardController
: public ui::InputMethodObserver
,
40 public aura::WindowObserver
{
42 // Different ways to hide the keyboard.
45 HIDE_REASON_AUTOMATIC
,
50 // Takes ownership of |proxy|.
51 explicit KeyboardController(KeyboardControllerProxy
* proxy
);
52 ~KeyboardController() override
;
54 // Returns the container for the keyboard, which is owned by
55 // KeyboardController.
56 aura::Window
* GetContainerWindow();
58 // Whether the container window for the keyboard has been initialized.
59 bool keyboard_container_initialized() const {
60 return container_
.get() != NULL
;
63 // Reloads the content of the keyboard. No-op if the keyboard content is not
67 // Hides virtual keyboard and notifies observer bounds change.
68 // This function should be called with a delay to avoid layout flicker
69 // when the focus of input field quickly change. |automatic| is true when the
70 // call is made by the system rather than initiated by the user.
71 void HideKeyboard(HideReason reason
);
73 // Notifies the keyboard observer for keyboard bounds changed.
74 void NotifyKeyboardBoundsChanging(const gfx::Rect
& new_bounds
);
76 // Management of the observer list.
77 virtual void AddObserver(KeyboardControllerObserver
* observer
);
78 virtual void RemoveObserver(KeyboardControllerObserver
* observer
);
80 KeyboardControllerProxy
* proxy() { return proxy_
.get(); }
82 void set_lock_keyboard(bool lock
) { lock_keyboard_
= lock
; }
84 // Force the keyboard to show up if not showing and lock the keyboard if
86 void ShowKeyboard(bool lock
);
88 // Sets the active keyboard controller. KeyboardController takes ownership of
89 // the instance. Calling ResetIntance with a new instance destroys the
90 // previous one. May be called with NULL to clear the instance.
91 static void ResetInstance(KeyboardController
* controller
);
93 // Retrieve the active keyboard controller.
94 static KeyboardController
* GetInstance();
96 // Returns true if keyboard is currently visible.
97 bool keyboard_visible() { return keyboard_visible_
; }
99 bool show_on_resize() { return show_on_resize_
; }
101 // Returns the current keyboard bounds. When the keyboard is not shown,
102 // an empty rectangle will get returned.
103 const gfx::Rect
& current_keyboard_bounds() {
104 return current_keyboard_bounds_
;
107 // Determines whether a particular window should have insets for overscroll.
108 bool ShouldEnableInsets(aura::Window
* window
);
110 // Updates insets on web content window
111 void UpdateWindowInsets(aura::Window
* window
);
114 // For access to Observer methods for simulation.
115 friend class KeyboardControllerTest
;
117 // aura::WindowObserver overrides
118 void OnWindowHierarchyChanged(const HierarchyChangeParams
& params
) override
;
120 // InputMethodObserver overrides
121 void OnTextInputTypeChanged(const ui::TextInputClient
* client
) override
{}
122 void OnFocus() override
{}
123 void OnBlur() override
{}
124 void OnCaretBoundsChanged(const ui::TextInputClient
* client
) override
{}
125 void OnTextInputStateChanged(const ui::TextInputClient
* client
) override
;
126 void OnInputMethodDestroyed(const ui::InputMethod
* input_method
) override
;
127 void OnShowImeIfNeeded() override
;
129 // Show virtual keyboard immediately with animation.
130 void ShowKeyboardInternal();
132 // Clears any insets on web content windows.
133 void ResetWindowInsets();
135 // Returns true if keyboard is scheduled to hide.
136 bool WillHideKeyboard() const;
138 // Called when show and hide animation finished successfully. If the animation
139 // is aborted, it won't be called.
140 void ShowAnimationFinished();
141 void HideAnimationFinished();
143 // Adds an observer for tracking changes to a window size or
144 // position while the keyboard is displayed. Any window repositioning
145 // invalidates insets for overscrolling.
146 void AddBoundsChangedObserver(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 // The currently used keyboard position.
165 gfx::Rect current_keyboard_bounds_
;
167 static KeyboardController
* instance_
;
169 base::WeakPtrFactory
<KeyboardController
> weak_factory_
;
171 DISALLOW_COPY_AND_ASSIGN(KeyboardController
);
174 } // namespace keyboard
176 #endif // UI_KEYBOARD_KEYBOARD_CONTROLLER_H_