OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/keyboard/keyboard_controller.h" | 5 #include "ui/keyboard/keyboard_controller.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "ui/aura/layout_manager.h" | 9 #include "ui/aura/layout_manager.h" |
10 #include "ui/aura/window.h" | 10 #include "ui/aura/window.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 window_bounds.width(), | 35 window_bounds.width(), |
36 window_bounds.height() * kKeyboardHeightRatio); | 36 window_bounds.height() * kKeyboardHeightRatio); |
37 } | 37 } |
38 | 38 |
39 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus. | 39 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus. |
40 // This is necessary to make sure that the synthetic key-events reach the target | 40 // This is necessary to make sure that the synthetic key-events reach the target |
41 // window. | 41 // window. |
42 // The delegate deletes itself when the window is destroyed. | 42 // The delegate deletes itself when the window is destroyed. |
43 class KeyboardWindowDelegate : public aura::WindowDelegate { | 43 class KeyboardWindowDelegate : public aura::WindowDelegate { |
44 public: | 44 public: |
45 KeyboardWindowDelegate() {} | 45 KeyboardWindowDelegate(keyboard::KeyboardControllerProxy* proxy) |
| 46 : proxy_(proxy) {} |
46 virtual ~KeyboardWindowDelegate() {} | 47 virtual ~KeyboardWindowDelegate() {} |
47 | 48 |
48 private: | 49 private: |
49 // Overridden from aura::WindowDelegate: | 50 // Overridden from aura::WindowDelegate: |
50 virtual gfx::Size GetMinimumSize() const OVERRIDE { return gfx::Size(); } | 51 virtual gfx::Size GetMinimumSize() const OVERRIDE { return gfx::Size(); } |
51 virtual gfx::Size GetMaximumSize() const OVERRIDE { return gfx::Size(); } | 52 virtual gfx::Size GetMaximumSize() const OVERRIDE { return gfx::Size(); } |
52 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, | 53 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, |
53 const gfx::Rect& new_bounds) OVERRIDE { | 54 const gfx::Rect& new_bounds) OVERRIDE { |
54 bounds_ = new_bounds; | 55 bounds_ = new_bounds; |
55 } | 56 } |
(...skipping 10 matching lines...) Expand all Loading... |
66 } | 67 } |
67 virtual bool CanFocus() OVERRIDE { return false; } | 68 virtual bool CanFocus() OVERRIDE { return false; } |
68 virtual void OnCaptureLost() OVERRIDE {} | 69 virtual void OnCaptureLost() OVERRIDE {} |
69 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {} | 70 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {} |
70 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {} | 71 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {} |
71 virtual void OnWindowDestroying() OVERRIDE {} | 72 virtual void OnWindowDestroying() OVERRIDE {} |
72 virtual void OnWindowDestroyed() OVERRIDE { delete this; } | 73 virtual void OnWindowDestroyed() OVERRIDE { delete this; } |
73 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {} | 74 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {} |
74 virtual bool HasHitTestMask() const OVERRIDE { return true; } | 75 virtual bool HasHitTestMask() const OVERRIDE { return true; } |
75 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE { | 76 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE { |
76 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(bounds_); | 77 gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() |
| 78 : KeyboardBoundsFromWindowBounds(bounds_); |
77 mask->addRect(RectToSkRect(keyboard_bounds)); | 79 mask->addRect(RectToSkRect(keyboard_bounds)); |
78 } | 80 } |
79 virtual void DidRecreateLayer(ui::Layer* old_layer, | 81 virtual void DidRecreateLayer(ui::Layer* old_layer, |
80 ui::Layer* new_layer) OVERRIDE {} | 82 ui::Layer* new_layer) OVERRIDE {} |
81 | 83 |
82 gfx::Rect bounds_; | 84 gfx::Rect bounds_; |
| 85 keyboard::KeyboardControllerProxy* proxy_; |
| 86 |
83 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate); | 87 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate); |
84 }; | 88 }; |
85 | 89 |
86 } // namespace | 90 } // namespace |
87 | 91 |
88 namespace keyboard { | 92 namespace keyboard { |
89 | 93 |
90 // LayoutManager for the virtual keyboard container. Manages a single window | 94 // LayoutManager for the virtual keyboard container. Manages a single window |
91 // (the virtual keyboard) and keeps it positioned at the bottom of the | 95 // (the virtual keyboard) and keeps it positioned at the bottom of the |
92 // owner window. | 96 // owner window. |
93 class KeyboardLayoutManager : public aura::LayoutManager { | 97 class KeyboardLayoutManager : public aura::LayoutManager { |
94 public: | 98 public: |
95 KeyboardLayoutManager(aura::Window* container) | 99 KeyboardLayoutManager(KeyboardController* controller) |
96 : container_(container), keyboard_(NULL) { | 100 : controller_(controller), keyboard_(NULL) { |
97 CHECK(container_); | |
98 } | 101 } |
99 | 102 |
100 // Overridden from aura::LayoutManager | 103 // Overridden from aura::LayoutManager |
101 virtual void OnWindowResized() OVERRIDE { | 104 virtual void OnWindowResized() OVERRIDE { |
102 if (!keyboard_) | 105 if (!keyboard_) |
103 return; | 106 return; |
104 SetChildBoundsDirect(keyboard_, | 107 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds( |
105 KeyboardBoundsFromWindowBounds(container_->bounds())); | 108 controller_->GetContainerWindow()->bounds()); |
| 109 // Don't notify keyboard bounds changing, because the bounds has been |
| 110 // changed here. |
| 111 SetChildBoundsDirect(keyboard_, keyboard_bounds); |
106 } | 112 } |
107 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { | 113 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { |
108 DCHECK(!keyboard_); | 114 DCHECK(!keyboard_); |
109 keyboard_ = child; | 115 keyboard_ = child; |
110 } | 116 } |
111 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} | 117 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} |
112 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} | 118 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} |
113 virtual void OnChildWindowVisibilityChanged(aura::Window* child, | 119 virtual void OnChildWindowVisibilityChanged(aura::Window* child, |
114 bool visible) OVERRIDE {} | 120 bool visible) OVERRIDE {} |
115 virtual void SetChildBounds(aura::Window* child, | 121 virtual void SetChildBounds(aura::Window* child, |
116 const gfx::Rect& requested_bounds) OVERRIDE { | 122 const gfx::Rect& requested_bounds) OVERRIDE { |
117 // Drop these: the size should only be set in OnWindowResized. | 123 // SetChildBounds can be invoked by resizing from the container or by |
| 124 // resizing from the contents (through window.resizeTo call in JS). |
| 125 // OnWindowResized() can take care of resizing from the container. |
| 126 // While here should only take care of resizing from the contents. |
| 127 if (controller_->proxy()->resizing_from_contents()) { |
| 128 controller_->NotifyKeyboardBoundsChanging(requested_bounds); |
| 129 SetChildBoundsDirect(child, requested_bounds); |
| 130 controller_->proxy()->set_resizing_from_contents(false); |
| 131 } |
118 } | 132 } |
119 | 133 |
120 private: | 134 private: |
121 aura::Window* container_; | 135 KeyboardController* controller_; |
122 aura::Window* keyboard_; | 136 aura::Window* keyboard_; |
123 | 137 |
124 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager); | 138 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager); |
125 }; | 139 }; |
126 | 140 |
127 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) | 141 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) |
128 : proxy_(proxy), | 142 : proxy_(proxy), |
129 input_method_(NULL), | 143 input_method_(NULL), |
130 keyboard_visible_(false), | 144 keyboard_visible_(false), |
131 weak_factory_(this) { | 145 weak_factory_(this) { |
132 CHECK(proxy); | 146 CHECK(proxy); |
133 input_method_ = proxy_->GetInputMethod(); | 147 input_method_ = proxy_->GetInputMethod(); |
134 input_method_->AddObserver(this); | 148 input_method_->AddObserver(this); |
135 } | 149 } |
136 | 150 |
137 KeyboardController::~KeyboardController() { | 151 KeyboardController::~KeyboardController() { |
138 if (container_.get()) | 152 if (container_.get()) |
139 container_->RemoveObserver(this); | 153 container_->RemoveObserver(this); |
140 if (input_method_) | 154 if (input_method_) |
141 input_method_->RemoveObserver(this); | 155 input_method_->RemoveObserver(this); |
142 } | 156 } |
143 | 157 |
144 aura::Window* KeyboardController::GetContainerWindow() { | 158 aura::Window* KeyboardController::GetContainerWindow() { |
145 if (!container_.get()) { | 159 if (!container_.get()) { |
146 container_.reset(new aura::Window(new KeyboardWindowDelegate())); | 160 container_.reset(new aura::Window( |
| 161 new KeyboardWindowDelegate(proxy_.get()))); |
147 container_->SetName("KeyboardContainer"); | 162 container_->SetName("KeyboardContainer"); |
148 container_->set_owned_by_parent(false); | 163 container_->set_owned_by_parent(false); |
149 container_->Init(ui::LAYER_NOT_DRAWN); | 164 container_->Init(ui::LAYER_NOT_DRAWN); |
150 container_->AddObserver(this); | 165 container_->AddObserver(this); |
151 container_->SetLayoutManager(new KeyboardLayoutManager(container_.get())); | 166 container_->SetLayoutManager(new KeyboardLayoutManager(this)); |
152 } | 167 } |
153 return container_.get(); | 168 return container_.get(); |
154 } | 169 } |
155 | 170 |
| 171 void KeyboardController::NotifyKeyboardBoundsChanging( |
| 172 const gfx::Rect& new_bounds) { |
| 173 if (proxy_ && proxy_->GetKeyboardWindow()->IsVisible()) { |
| 174 FOR_EACH_OBSERVER(KeyboardControllerObserver, |
| 175 observer_list_, |
| 176 OnKeyboardBoundsChanging(new_bounds)); |
| 177 } |
| 178 } |
| 179 |
156 void KeyboardController::HideKeyboard(HideReason reason) { | 180 void KeyboardController::HideKeyboard(HideReason reason) { |
157 keyboard_visible_ = false; | 181 keyboard_visible_ = false; |
158 | 182 |
159 keyboard::LogKeyboardControlEvent( | 183 keyboard::LogKeyboardControlEvent( |
160 reason == HIDE_REASON_AUTOMATIC ? | 184 reason == HIDE_REASON_AUTOMATIC ? |
161 keyboard::KEYBOARD_CONTROL_HIDE_AUTO : | 185 keyboard::KEYBOARD_CONTROL_HIDE_AUTO : |
162 keyboard::KEYBOARD_CONTROL_HIDE_USER); | 186 keyboard::KEYBOARD_CONTROL_HIDE_USER); |
163 | 187 |
164 FOR_EACH_OBSERVER(KeyboardControllerObserver, | 188 NotifyKeyboardBoundsChanging(gfx::Rect()); |
165 observer_list_, | |
166 OnKeyboardBoundsChanging(gfx::Rect())); | |
167 | 189 |
168 proxy_->HideKeyboardContainer(container_.get()); | 190 proxy_->HideKeyboardContainer(container_.get()); |
169 } | 191 } |
170 | 192 |
171 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { | 193 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { |
172 observer_list_.AddObserver(observer); | 194 observer_list_.AddObserver(observer); |
173 } | 195 } |
174 | 196 |
175 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { | 197 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { |
176 observer_list_.RemoveObserver(observer); | 198 observer_list_.RemoveObserver(observer); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 | 240 |
219 // If the controller is in the process of hiding the keyboard, do not log | 241 // If the controller is in the process of hiding the keyboard, do not log |
220 // the stat here since the keyboard will not actually be shown. | 242 // the stat here since the keyboard will not actually be shown. |
221 if (!WillHideKeyboard()) | 243 if (!WillHideKeyboard()) |
222 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW); | 244 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW); |
223 | 245 |
224 weak_factory_.InvalidateWeakPtrs(); | 246 weak_factory_.InvalidateWeakPtrs(); |
225 if (container_->IsVisible()) | 247 if (container_->IsVisible()) |
226 return; | 248 return; |
227 | 249 |
228 FOR_EACH_OBSERVER( | 250 NotifyKeyboardBoundsChanging(container_->children()[0]->bounds()); |
229 KeyboardControllerObserver, | 251 |
230 observer_list_, | |
231 OnKeyboardBoundsChanging(container_->children()[0]->bounds())); | |
232 proxy_->ShowKeyboardContainer(container_.get()); | 252 proxy_->ShowKeyboardContainer(container_.get()); |
233 } else { | 253 } else { |
234 // Set the visibility state here so that any queries for visibility | 254 // Set the visibility state here so that any queries for visibility |
235 // before the timer fires returns the correct future value. | 255 // before the timer fires returns the correct future value. |
236 keyboard_visible_ = false; | 256 keyboard_visible_ = false; |
237 base::MessageLoop::current()->PostDelayedTask( | 257 base::MessageLoop::current()->PostDelayedTask( |
238 FROM_HERE, | 258 FROM_HERE, |
239 base::Bind(&KeyboardController::HideKeyboard, | 259 base::Bind(&KeyboardController::HideKeyboard, |
240 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC), | 260 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC), |
241 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs)); | 261 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs)); |
242 } | 262 } |
243 } | 263 } |
244 // TODO(bryeung): whenever the TextInputClient changes we need to notify the | 264 // TODO(bryeung): whenever the TextInputClient changes we need to notify the |
245 // keyboard (with the TextInputType) so that it can reset it's state (e.g. | 265 // keyboard (with the TextInputType) so that it can reset it's state (e.g. |
246 // abandon compositions in progress) | 266 // abandon compositions in progress) |
247 } | 267 } |
248 | 268 |
249 void KeyboardController::OnInputMethodDestroyed( | 269 void KeyboardController::OnInputMethodDestroyed( |
250 const ui::InputMethod* input_method) { | 270 const ui::InputMethod* input_method) { |
251 DCHECK_EQ(input_method_, input_method); | 271 DCHECK_EQ(input_method_, input_method); |
252 input_method_ = NULL; | 272 input_method_ = NULL; |
253 } | 273 } |
254 | 274 |
255 bool KeyboardController::WillHideKeyboard() const { | 275 bool KeyboardController::WillHideKeyboard() const { |
256 return weak_factory_.HasWeakPtrs(); | 276 return weak_factory_.HasWeakPtrs(); |
257 } | 277 } |
258 | 278 |
259 } // namespace keyboard | 279 } // namespace keyboard |
OLD | NEW |