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. 4 5 #ifndef UI_KEYBOARD_KEYBOARD_CONTROLLER_H_ 6 #define UI_KEYBOARD_KEYBOARD_CONTROLLER_H_ 7 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" 17 #include "url/gurl.h" 18 19 namespace aura { 20 class Window; 21 } 22 namespace ui { 23 class InputMethod; 24 class TextInputClient; 25 } 26 27 namespace keyboard { 28 29 class CallbackAnimationObserver; 30 class WindowBoundsChangeObserver; 31 class KeyboardControllerObserver; 32 class KeyboardControllerProxy; 33 34 // Animation distance. 35 const int kAnimationDistance = 30; 36 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 { 41 public: 42 // Different ways to hide the keyboard. 43 enum HideReason { 44 // System initiated. 45 HIDE_REASON_AUTOMATIC, 46 // User initiated. 47 HIDE_REASON_MANUAL, 48 }; 49 50 // Takes ownership of |proxy|. 51 explicit KeyboardController(KeyboardControllerProxy* proxy); 52 virtual ~KeyboardController(); 53 54 // Returns the container for the keyboard, which is owned by 55 // KeyboardController. 56 aura::Window* GetContainerWindow(); 57 58 // Whether the container window for the keyboard has been initialized. 59 bool keyboard_container_initialized() const { 60 return container_.get() != NULL; 61 } 62 63 // Reloads the content of the keyboard. No-op if the keyboard content is not 64 // loaded yet. 65 void Reload(); 66 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); 72 73 // Notifies the keyboard observer for keyboard bounds changed. 74 void NotifyKeyboardBoundsChanging(const gfx::Rect& new_bounds); 75 76 // Management of the observer list. 77 virtual void AddObserver(KeyboardControllerObserver* observer); 78 virtual void RemoveObserver(KeyboardControllerObserver* observer); 79 80 KeyboardControllerProxy* proxy() { return proxy_.get(); } 81 82 void set_lock_keyboard(bool lock) { lock_keyboard_ = lock; } 83 84 // Force the keyboard to show up if not showing and lock the keyboard if 85 // |lock| is true. 86 void ShowKeyboard(bool lock); 87 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); 92 93 // Retrieve the active keyboard controller. 94 static KeyboardController* GetInstance(); 95 96 // Returns true if keyboard is currently visible. 97 bool keyboard_visible() { return keyboard_visible_; } 98 99 bool show_on_resize() { return show_on_resize_; } 100 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_; 105 } 106 107 // Determines whether a particular window should have insets for overscroll. 108 bool ShouldEnableInsets(aura::Window* window); 109 110 // Updates insets on web content window 111 void UpdateWindowInsets(aura::Window* window); 112 113 private: 114 // For access to Observer methods for simulation. 115 friend class KeyboardControllerTest; 116 117 // aura::WindowObserver overrides 118 virtual void OnWindowHierarchyChanged( 119 const HierarchyChangeParams& params) OVERRIDE; 120 121 // InputMethodObserver overrides 122 virtual void OnTextInputTypeChanged( 123 const ui::TextInputClient* client) OVERRIDE {} 124 virtual void OnFocus() OVERRIDE {} 125 virtual void OnBlur() OVERRIDE {} 126 virtual void OnCaretBoundsChanged( 127 const ui::TextInputClient* client) OVERRIDE {} 128 virtual void OnTextInputStateChanged( 129 const ui::TextInputClient* client) OVERRIDE; 130 virtual void OnInputMethodDestroyed( 131 const ui::InputMethod* input_method) OVERRIDE; 132 virtual void OnShowImeIfNeeded() OVERRIDE; 133 134 // Show virtual keyboard immediately with animation. 135 void ShowKeyboardInternal(); 136 137 // Clears any insets on web content windows. 138 void ResetWindowInsets(); 139 140 // Returns true if keyboard is scheduled to hide. 141 bool WillHideKeyboard() const; 142 143 // Called when show and hide animation finished successfully. If the animation 144 // is aborted, it won't be called. 145 void ShowAnimationFinished(); 146 void HideAnimationFinished(); 147 148 // Adds an observer for tracking changes to a window size or 149 // position while the keyboard is displayed. Any window repositioning 150 // invalidates insets for overscrolling. 151 void AddBoundsChangedObserver(aura::Window* window); 152 153 scoped_ptr<KeyboardControllerProxy> proxy_; 154 scoped_ptr<aura::Window> container_; 155 // CallbackAnimationObserver should destructed before container_ because it 156 // uses container_'s animator. 157 scoped_ptr<CallbackAnimationObserver> animation_observer_; 158 159 scoped_ptr<WindowBoundsChangeObserver> window_bounds_observer_; 160 161 ui::InputMethod* input_method_; 162 bool keyboard_visible_; 163 bool show_on_resize_; 164 bool lock_keyboard_; 165 ui::TextInputType type_; 166 167 ObserverList<KeyboardControllerObserver> observer_list_; 168 169 // The currently used keyboard position. 170 gfx::Rect current_keyboard_bounds_; 171 172 static KeyboardController* instance_; 173 174 base::WeakPtrFactory<KeyboardController> weak_factory_; 175 176 DISALLOW_COPY_AND_ASSIGN(KeyboardController); 177 }; 178 179 } // namespace keyboard 180 181 #endif // UI_KEYBOARD_KEYBOARD_CONTROLLER_H_ 182