Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: ui/keyboard/keyboard_controller.cc

Issue 97013002: [Input View] Makes the input view window support window.resizeTo() and w3c visibility API its web c… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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.
bshe 2013/12/10 21:18:08 It starts to make sense that we move the KeyboardL
Shu Chen 2013/12/11 00:48:32 Thank you!
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/06 12:30:02 Did you verify that the keyboard-window gets resiz
sadrul 2013/12/09 22:47:01 You can test this trivially in a test by changing
bshe 2013/12/10 21:18:08 Looks like the keyboard-window size is now set by
sadrul 2013/12/10 21:21:19 We shouldn't depend on outside code (ash) to maint
Shu Chen 2013/12/11 00:48:32 Test added.
Shu Chen 2013/12/11 00:48:32 I've modified the code to let OnWindowResized hand
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;
sadrul 2013/12/06 12:30:02 Why did you remove this code?
Shu Chen 2013/12/11 00:48:32 Done.
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 } else {
122 ResizeKeyboardToDefault(child);
123 }
118 } 124 }
119 125
120 private: 126 private:
121 aura::Window* container_; 127 void ResizeKeyboardToDefault(aura::Window* child) {
122 aura::Window* keyboard_; 128 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(
129 controller_->GetContainerWindow()->bounds());
130 SetChildBoundsDirect(child, keyboard_bounds);
131 }
132
133 KeyboardController* controller_;
123 134
124 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager); 135 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager);
125 }; 136 };
126 137
127 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) 138 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
128 : proxy_(proxy), 139 : proxy_(proxy),
129 input_method_(NULL), 140 input_method_(NULL),
130 keyboard_visible_(false), 141 keyboard_visible_(false),
131 weak_factory_(this) { 142 weak_factory_(this) {
132 CHECK(proxy); 143 CHECK(proxy);
133 input_method_ = proxy_->GetInputMethod(); 144 input_method_ = proxy_->GetInputMethod();
134 input_method_->AddObserver(this); 145 input_method_->AddObserver(this);
135 } 146 }
136 147
137 KeyboardController::~KeyboardController() { 148 KeyboardController::~KeyboardController() {
138 if (container_.get()) 149 if (container_.get()) {
139 container_->RemoveObserver(this); 150 container_->RemoveObserver(this);
151 // Remove the keyboard window from the children because the keyboard window
152 // is owned by proxy and it should be destroyed by proxy.
153 if (container_->Contains(proxy_->GetKeyboardWindow())) {
154 container_->RemoveChild(proxy_->GetKeyboardWindow());
155 }
156 }
140 if (input_method_) 157 if (input_method_)
141 input_method_->RemoveObserver(this); 158 input_method_->RemoveObserver(this);
142 } 159 }
143 160
144 aura::Window* KeyboardController::GetContainerWindow() { 161 aura::Window* KeyboardController::GetContainerWindow() {
145 if (!container_.get()) { 162 if (!container_.get()) {
146 container_.reset(new aura::Window(new KeyboardWindowDelegate())); 163 container_.reset(new aura::Window(
164 new KeyboardWindowDelegate(proxy_.get())));
147 container_->SetName("KeyboardContainer"); 165 container_->SetName("KeyboardContainer");
148 container_->set_owned_by_parent(false); 166 container_->set_owned_by_parent(false);
149 container_->Init(ui::LAYER_NOT_DRAWN); 167 container_->Init(ui::LAYER_NOT_DRAWN);
150 container_->AddObserver(this); 168 container_->AddObserver(this);
151 container_->SetLayoutManager(new KeyboardLayoutManager(container_.get())); 169 container_->SetLayoutManager(new KeyboardLayoutManager(this));
152 } 170 }
153 return container_.get(); 171 return container_.get();
154 } 172 }
155 173
174 void KeyboardController::NotifyKeyboardBoundsChanging(
175 const gfx::Rect& new_bounds) {
176 if (proxy_ && proxy_->GetKeyboardWindow()->IsVisible()) {
sadrul 2013/12/06 12:30:02 DCHECK(proxy_)
Shu Chen 2013/12/11 00:48:32 Done.
177 FOR_EACH_OBSERVER(KeyboardControllerObserver,
178 observer_list_,
179 OnKeyboardBoundsChanging(new_bounds));
180 }
181 }
182
156 void KeyboardController::HideKeyboard(HideReason reason) { 183 void KeyboardController::HideKeyboard(HideReason reason) {
157 keyboard_visible_ = false; 184 keyboard_visible_ = false;
158 185
159 keyboard::LogKeyboardControlEvent( 186 keyboard::LogKeyboardControlEvent(
160 reason == HIDE_REASON_AUTOMATIC ? 187 reason == HIDE_REASON_AUTOMATIC ?
161 keyboard::KEYBOARD_CONTROL_HIDE_AUTO : 188 keyboard::KEYBOARD_CONTROL_HIDE_AUTO :
162 keyboard::KEYBOARD_CONTROL_HIDE_USER); 189 keyboard::KEYBOARD_CONTROL_HIDE_USER);
163 190
164 FOR_EACH_OBSERVER(KeyboardControllerObserver, 191 NotifyKeyboardBoundsChanging(gfx::Rect());
165 observer_list_,
166 OnKeyboardBoundsChanging(gfx::Rect()));
167 192
168 proxy_->HideKeyboardContainer(container_.get()); 193 proxy_->HideKeyboardContainer(container_.get());
169 } 194 }
170 195
171 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { 196 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) {
172 observer_list_.AddObserver(observer); 197 observer_list_.AddObserver(observer);
173 } 198 }
174 199
175 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { 200 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) {
176 observer_list_.RemoveObserver(observer); 201 observer_list_.RemoveObserver(observer);
(...skipping 20 matching lines...) Expand all
197 client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; 222 client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
198 if (type == ui::TEXT_INPUT_TYPE_NONE && 223 if (type == ui::TEXT_INPUT_TYPE_NONE &&
199 !IsKeyboardUsabilityExperimentEnabled()) { 224 !IsKeyboardUsabilityExperimentEnabled()) {
200 should_show = false; 225 should_show = false;
201 } else { 226 } else {
202 if (container_->children().empty()) { 227 if (container_->children().empty()) {
203 keyboard::MarkKeyboardLoadStarted(); 228 keyboard::MarkKeyboardLoadStarted();
204 aura::Window* keyboard = proxy_->GetKeyboardWindow(); 229 aura::Window* keyboard = proxy_->GetKeyboardWindow();
205 keyboard->Show(); 230 keyboard->Show();
206 container_->AddChild(keyboard); 231 container_->AddChild(keyboard);
207 container_->layout_manager()->OnWindowResized();
208 } 232 }
209 if (type != ui::TEXT_INPUT_TYPE_NONE) 233 if (type != ui::TEXT_INPUT_TYPE_NONE)
210 proxy_->SetUpdateInputType(type); 234 proxy_->SetUpdateInputType(type);
211 container_->parent()->StackChildAtTop(container_.get()); 235 container_->parent()->StackChildAtTop(container_.get());
212 should_show = true; 236 should_show = true;
213 } 237 }
214 238
215 if (was_showing != should_show) { 239 if (was_showing != should_show) {
216 if (should_show) { 240 if (should_show) {
217 keyboard_visible_ = true; 241 keyboard_visible_ = true;
218 242
219 // If the controller is in the process of hiding the keyboard, do not log 243 // 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. 244 // the stat here since the keyboard will not actually be shown.
221 if (!WillHideKeyboard()) 245 if (!WillHideKeyboard())
222 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW); 246 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW);
223 247
224 weak_factory_.InvalidateWeakPtrs(); 248 weak_factory_.InvalidateWeakPtrs();
225 if (container_->IsVisible()) 249 if (container_->IsVisible())
226 return; 250 return;
227 251
228 FOR_EACH_OBSERVER( 252 NotifyKeyboardBoundsChanging(container_->children()[0]->bounds());
229 KeyboardControllerObserver, 253
230 observer_list_,
231 OnKeyboardBoundsChanging(container_->children()[0]->bounds()));
232 proxy_->ShowKeyboardContainer(container_.get()); 254 proxy_->ShowKeyboardContainer(container_.get());
233 } else { 255 } else {
234 // Set the visibility state here so that any queries for visibility 256 // Set the visibility state here so that any queries for visibility
235 // before the timer fires returns the correct future value. 257 // before the timer fires returns the correct future value.
236 keyboard_visible_ = false; 258 keyboard_visible_ = false;
237 base::MessageLoop::current()->PostDelayedTask( 259 base::MessageLoop::current()->PostDelayedTask(
238 FROM_HERE, 260 FROM_HERE,
239 base::Bind(&KeyboardController::HideKeyboard, 261 base::Bind(&KeyboardController::HideKeyboard,
240 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC), 262 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC),
241 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs)); 263 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs));
242 } 264 }
243 } 265 }
244 // TODO(bryeung): whenever the TextInputClient changes we need to notify the 266 // 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. 267 // keyboard (with the TextInputType) so that it can reset it's state (e.g.
246 // abandon compositions in progress) 268 // abandon compositions in progress)
247 } 269 }
248 270
249 void KeyboardController::OnInputMethodDestroyed( 271 void KeyboardController::OnInputMethodDestroyed(
250 const ui::InputMethod* input_method) { 272 const ui::InputMethod* input_method) {
251 DCHECK_EQ(input_method_, input_method); 273 DCHECK_EQ(input_method_, input_method);
252 input_method_ = NULL; 274 input_method_ = NULL;
253 } 275 }
254 276
255 bool KeyboardController::WillHideKeyboard() const { 277 bool KeyboardController::WillHideKeyboard() const {
256 return weak_factory_.HasWeakPtrs(); 278 return weak_factory_.HasWeakPtrs();
257 } 279 }
258 280
259 } // namespace keyboard 281 } // namespace keyboard
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698