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 KeyboardControllerObserver; 31 class KeyboardControllerProxy; 32 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 { 37 public: 38 // Different ways to hide the keyboard. 39 enum HideReason { 40 // System initiated. 41 HIDE_REASON_AUTOMATIC, 42 // User initiated. 43 HIDE_REASON_MANUAL, 44 }; 45 46 // Takes ownership of |proxy|. 47 explicit KeyboardController(KeyboardControllerProxy* proxy); 48 virtual ~KeyboardController(); 49 50 // Returns the container for the keyboard, which is owned by 51 // KeyboardController. 52 aura::Window* GetContainerWindow(); 53 54 // Whether the container window for the keyboard has been initialized. keyboard_container_initialized()55 bool keyboard_container_initialized() const { 56 return container_.get() != NULL; 57 } 58 59 // Reloads the content of the keyboard. No-op if the keyboard content is not 60 // loaded yet. 61 void Reload(); 62 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); 68 69 // Notifies the keyboard observer for keyboard bounds changed. 70 void NotifyKeyboardBoundsChanging(const gfx::Rect& new_bounds); 71 72 // Management of the observer list. 73 virtual void AddObserver(KeyboardControllerObserver* observer); 74 virtual void RemoveObserver(KeyboardControllerObserver* observer); 75 proxy()76 KeyboardControllerProxy* proxy() { return proxy_.get(); } 77 set_lock_keyboard(bool lock)78 void set_lock_keyboard(bool lock) { lock_keyboard_ = lock; } 79 80 // Force the keyboard to show up if not showing and lock the keyboard if 81 // |lock| is true. 82 void ShowKeyboard(bool lock); 83 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); 88 89 // Retrieve the active keyboard controller. 90 static KeyboardController* GetInstance(); 91 92 // Returns true if keyboard is currently visible. keyboard_visible()93 bool keyboard_visible() { return keyboard_visible_; } 94 95 // Returns the current keyboard bounds. When the keyboard is not shown, 96 // an empty rectangle will get returned. current_keyboard_bounds()97 const gfx::Rect& current_keyboard_bounds() { 98 return current_keyboard_bounds_; 99 } 100 101 private: 102 // For access to Observer methods for simulation. 103 friend class KeyboardControllerTest; 104 105 // aura::WindowObserver overrides 106 virtual void OnWindowHierarchyChanged( 107 const HierarchyChangeParams& params) OVERRIDE; 108 109 // InputMethodObserver overrides OnTextInputTypeChanged(const ui::TextInputClient * client)110 virtual void OnTextInputTypeChanged( 111 const ui::TextInputClient* client) OVERRIDE {} OnFocus()112 virtual void OnFocus() OVERRIDE {} OnBlur()113 virtual void OnBlur() OVERRIDE {} OnCaretBoundsChanged(const ui::TextInputClient * client)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; 121 122 // Show virtual keyboard immediately with animation. 123 void ShowKeyboardInternal(); 124 125 // Clears any insets on web content windows. 126 void ResetWindowInsets(); 127 128 // Returns true if keyboard is scheduled to hide. 129 bool WillHideKeyboard() const; 130 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(); 135 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_; 141 142 ui::InputMethod* input_method_; 143 bool keyboard_visible_; 144 bool lock_keyboard_; 145 ui::TextInputType type_; 146 147 ObserverList<KeyboardControllerObserver> observer_list_; 148 149 base::WeakPtrFactory<KeyboardController> weak_factory_; 150 151 // The currently used keyboard position. 152 gfx::Rect current_keyboard_bounds_; 153 154 static KeyboardController* instance_; 155 156 DISALLOW_COPY_AND_ASSIGN(KeyboardController); 157 }; 158 159 } // namespace keyboard 160 161 #endif // UI_KEYBOARD_KEYBOARD_CONTROLLER_H_ 162