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

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

Powered by Google App Engine
This is Rietveld 408576698