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 explicit 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 explicit KeyboardLayoutManager(KeyboardController* controller) |
96 : container_(container), keyboard_(NULL) { | 100 : controller_(controller) { |
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 {} |
sadrul
2013/12/05 07:54:55
Why did you remove this code? This would still be
Shu Chen
2013/12/05 08:21:04
As I mentioned, SetChildBounds() also gets called
| |
102 if (!keyboard_) | |
103 return; | |
104 SetChildBoundsDirect(keyboard_, | |
105 KeyboardBoundsFromWindowBounds(container_->bounds())); | |
106 } | |
107 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { | 105 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { |
108 DCHECK(!keyboard_); | 106 ResizeKeyboardToDefault(child); |
109 keyboard_ = child; | |
110 } | 107 } |
111 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} | 108 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} |
112 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} | 109 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} |
113 virtual void OnChildWindowVisibilityChanged(aura::Window* child, | 110 virtual void OnChildWindowVisibilityChanged(aura::Window* child, |
114 bool visible) OVERRIDE {} | 111 bool visible) OVERRIDE {} |
115 virtual void SetChildBounds(aura::Window* child, | 112 virtual void SetChildBounds(aura::Window* child, |
116 const gfx::Rect& requested_bounds) OVERRIDE { | 113 const gfx::Rect& requested_bounds) OVERRIDE { |
117 // Drop these: the size should only be set in OnWindowResized. | 114 // SetChildBounds can be invoked by resizing from the container or by |
115 // resizing from the contents (through window.resizeTo call in JS). | |
116 // The flag resizing_from_contents() is used to determine the keyboard is | |
117 // resizing from which. | |
118 if (controller_->proxy()->resizing_from_contents()) { | |
119 controller_->NotifyKeyboardBoundsChanging(requested_bounds); | |
120 SetChildBoundsDirect(child, requested_bounds); | |
121 controller_->proxy()->set_resizing_from_contents(false); | |
sadrul
2013/12/05 07:54:55
Do not reset the state here.
Shu Chen
2013/12/05 08:21:04
Done.
| |
122 } else { | |
123 ResizeKeyboardToDefault(child); | |
124 } | |
118 } | 125 } |
119 | 126 |
120 private: | 127 private: |
121 aura::Window* container_; | 128 void ResizeKeyboardToDefault(aura::Window* child) { |
122 aura::Window* keyboard_; | 129 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds( |
130 controller_->GetContainerWindow()->bounds()); | |
131 SetChildBoundsDirect(child, keyboard_bounds); | |
132 } | |
133 | |
134 KeyboardController* controller_; | |
123 | 135 |
124 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager); | 136 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager); |
125 }; | 137 }; |
126 | 138 |
127 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) | 139 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) |
128 : proxy_(proxy), | 140 : proxy_(proxy), |
129 input_method_(NULL), | 141 input_method_(NULL), |
130 keyboard_visible_(false), | 142 keyboard_visible_(false), |
131 weak_factory_(this) { | 143 weak_factory_(this) { |
132 CHECK(proxy); | 144 CHECK(proxy); |
133 input_method_ = proxy_->GetInputMethod(); | 145 input_method_ = proxy_->GetInputMethod(); |
134 input_method_->AddObserver(this); | 146 input_method_->AddObserver(this); |
135 } | 147 } |
136 | 148 |
137 KeyboardController::~KeyboardController() { | 149 KeyboardController::~KeyboardController() { |
138 if (container_.get()) | 150 if (container_.get()) { |
139 container_->RemoveObserver(this); | 151 container_->RemoveObserver(this); |
152 // Remove the keyboard window from the children because the keyboard window | |
153 // is owned by proxy and it should be destroyed by proxy. | |
154 if (container_->children().size() > 0 && | |
155 container_->children()[0] == proxy_->GetKeyboardWindow()) { | |
sadrul
2013/12/05 07:54:55
if (container_->Contains(GetKeyboardWindow()))
Shu Chen
2013/12/05 08:21:04
Done.
| |
156 container_->RemoveChild(proxy_->GetKeyboardWindow()); | |
157 } | |
158 } | |
140 if (input_method_) | 159 if (input_method_) |
141 input_method_->RemoveObserver(this); | 160 input_method_->RemoveObserver(this); |
142 } | 161 } |
143 | 162 |
144 aura::Window* KeyboardController::GetContainerWindow() { | 163 aura::Window* KeyboardController::GetContainerWindow() { |
145 if (!container_.get()) { | 164 if (!container_.get()) { |
146 container_.reset(new aura::Window(new KeyboardWindowDelegate())); | 165 container_.reset(new aura::Window( |
166 new KeyboardWindowDelegate(proxy_.get()))); | |
147 container_->SetName("KeyboardContainer"); | 167 container_->SetName("KeyboardContainer"); |
148 container_->set_owned_by_parent(false); | 168 container_->set_owned_by_parent(false); |
149 container_->Init(ui::LAYER_NOT_DRAWN); | 169 container_->Init(ui::LAYER_NOT_DRAWN); |
150 container_->AddObserver(this); | 170 container_->AddObserver(this); |
151 container_->SetLayoutManager(new KeyboardLayoutManager(container_.get())); | 171 container_->SetLayoutManager(new KeyboardLayoutManager(this)); |
152 } | 172 } |
153 return container_.get(); | 173 return container_.get(); |
154 } | 174 } |
155 | 175 |
176 void KeyboardController::NotifyKeyboardBoundsChanging( | |
177 const gfx::Rect& new_bounds) { | |
178 if (proxy_ && proxy_->GetKeyboardWindow()->IsVisible()) { | |
sadrul
2013/12/05 07:54:55
Should this be a CHECK?
Shu Chen
2013/12/05 08:21:04
Done.
Shu Chen
2013/12/05 12:30:12
After changing this to CHECK, some tests broken. B
| |
179 FOR_EACH_OBSERVER(KeyboardControllerObserver, | |
180 observer_list_, | |
181 OnKeyboardBoundsChanging(new_bounds)); | |
182 } | |
183 } | |
184 | |
156 void KeyboardController::HideKeyboard(HideReason reason) { | 185 void KeyboardController::HideKeyboard(HideReason reason) { |
157 keyboard_visible_ = false; | 186 keyboard_visible_ = false; |
158 | 187 |
159 keyboard::LogKeyboardControlEvent( | 188 keyboard::LogKeyboardControlEvent( |
160 reason == HIDE_REASON_AUTOMATIC ? | 189 reason == HIDE_REASON_AUTOMATIC ? |
161 keyboard::KEYBOARD_CONTROL_HIDE_AUTO : | 190 keyboard::KEYBOARD_CONTROL_HIDE_AUTO : |
162 keyboard::KEYBOARD_CONTROL_HIDE_USER); | 191 keyboard::KEYBOARD_CONTROL_HIDE_USER); |
163 | 192 |
164 FOR_EACH_OBSERVER(KeyboardControllerObserver, | 193 NotifyKeyboardBoundsChanging(gfx::Rect()); |
165 observer_list_, | |
166 OnKeyboardBoundsChanging(gfx::Rect())); | |
167 | 194 |
168 proxy_->HideKeyboardContainer(container_.get()); | 195 proxy_->HideKeyboardContainer(container_.get()); |
169 } | 196 } |
170 | 197 |
171 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { | 198 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { |
172 observer_list_.AddObserver(observer); | 199 observer_list_.AddObserver(observer); |
173 } | 200 } |
174 | 201 |
175 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { | 202 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { |
176 observer_list_.RemoveObserver(observer); | 203 observer_list_.RemoveObserver(observer); |
(...skipping 20 matching lines...) Expand all Loading... | |
197 client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; | 224 client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; |
198 if (type == ui::TEXT_INPUT_TYPE_NONE && | 225 if (type == ui::TEXT_INPUT_TYPE_NONE && |
199 !IsKeyboardUsabilityExperimentEnabled()) { | 226 !IsKeyboardUsabilityExperimentEnabled()) { |
200 should_show = false; | 227 should_show = false; |
201 } else { | 228 } else { |
202 if (container_->children().empty()) { | 229 if (container_->children().empty()) { |
203 keyboard::MarkKeyboardLoadStarted(); | 230 keyboard::MarkKeyboardLoadStarted(); |
204 aura::Window* keyboard = proxy_->GetKeyboardWindow(); | 231 aura::Window* keyboard = proxy_->GetKeyboardWindow(); |
205 keyboard->Show(); | 232 keyboard->Show(); |
206 container_->AddChild(keyboard); | 233 container_->AddChild(keyboard); |
207 container_->layout_manager()->OnWindowResized(); | |
208 } | 234 } |
209 if (type != ui::TEXT_INPUT_TYPE_NONE) | 235 if (type != ui::TEXT_INPUT_TYPE_NONE) |
210 proxy_->SetUpdateInputType(type); | 236 proxy_->SetUpdateInputType(type); |
211 container_->parent()->StackChildAtTop(container_.get()); | 237 container_->parent()->StackChildAtTop(container_.get()); |
212 should_show = true; | 238 should_show = true; |
213 } | 239 } |
214 | 240 |
215 if (was_showing != should_show) { | 241 if (was_showing != should_show) { |
216 if (should_show) { | 242 if (should_show) { |
217 keyboard_visible_ = true; | 243 keyboard_visible_ = true; |
218 | 244 |
219 // If the controller is in the process of hiding the keyboard, do not log | 245 // 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. | 246 // the stat here since the keyboard will not actually be shown. |
221 if (!WillHideKeyboard()) | 247 if (!WillHideKeyboard()) |
222 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW); | 248 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW); |
223 | 249 |
224 weak_factory_.InvalidateWeakPtrs(); | 250 weak_factory_.InvalidateWeakPtrs(); |
225 if (container_->IsVisible()) | 251 if (container_->IsVisible()) |
226 return; | 252 return; |
227 | 253 |
228 FOR_EACH_OBSERVER( | 254 NotifyKeyboardBoundsChanging(container_->children()[0]->bounds()); |
229 KeyboardControllerObserver, | 255 |
230 observer_list_, | |
231 OnKeyboardBoundsChanging(container_->children()[0]->bounds())); | |
232 proxy_->ShowKeyboardContainer(container_.get()); | 256 proxy_->ShowKeyboardContainer(container_.get()); |
233 } else { | 257 } else { |
234 // Set the visibility state here so that any queries for visibility | 258 // Set the visibility state here so that any queries for visibility |
235 // before the timer fires returns the correct future value. | 259 // before the timer fires returns the correct future value. |
236 keyboard_visible_ = false; | 260 keyboard_visible_ = false; |
237 base::MessageLoop::current()->PostDelayedTask( | 261 base::MessageLoop::current()->PostDelayedTask( |
238 FROM_HERE, | 262 FROM_HERE, |
239 base::Bind(&KeyboardController::HideKeyboard, | 263 base::Bind(&KeyboardController::HideKeyboard, |
240 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC), | 264 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC), |
241 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs)); | 265 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs)); |
242 } | 266 } |
243 } | 267 } |
244 // TODO(bryeung): whenever the TextInputClient changes we need to notify the | 268 // 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. | 269 // keyboard (with the TextInputType) so that it can reset it's state (e.g. |
246 // abandon compositions in progress) | 270 // abandon compositions in progress) |
247 } | 271 } |
248 | 272 |
249 void KeyboardController::OnInputMethodDestroyed( | 273 void KeyboardController::OnInputMethodDestroyed( |
250 const ui::InputMethod* input_method) { | 274 const ui::InputMethod* input_method) { |
251 DCHECK_EQ(input_method_, input_method); | 275 DCHECK_EQ(input_method_, input_method); |
252 input_method_ = NULL; | 276 input_method_ = NULL; |
253 } | 277 } |
254 | 278 |
255 bool KeyboardController::WillHideKeyboard() const { | 279 bool KeyboardController::WillHideKeyboard() const { |
256 return weak_factory_.HasWeakPtrs(); | 280 return weak_factory_.HasWeakPtrs(); |
257 } | 281 } |
258 | 282 |
259 } // namespace keyboard | 283 } // namespace keyboard |
OLD | NEW |