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

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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 } 66 }
67 virtual bool CanFocus() OVERRIDE { return false; } 67 virtual bool CanFocus() OVERRIDE { return false; }
68 virtual void OnCaptureLost() OVERRIDE {} 68 virtual void OnCaptureLost() OVERRIDE {}
69 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {} 69 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {}
70 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {} 70 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
71 virtual void OnWindowDestroying() OVERRIDE {} 71 virtual void OnWindowDestroying() OVERRIDE {}
72 virtual void OnWindowDestroyed() OVERRIDE { delete this; } 72 virtual void OnWindowDestroyed() OVERRIDE { delete this; }
73 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {} 73 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
74 virtual bool HasHitTestMask() const OVERRIDE { return true; } 74 virtual bool HasHitTestMask() const OVERRIDE { return true; }
75 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE { 75 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {
76 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(bounds_); 76 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(bounds_);
kevers 2013/12/03 00:08:12 Won't bounds_ be properly updated by OnBoundsChang
Shu Chen 2013/12/03 01:54:42 Actually with proxy_, bounds_ is not needed.
77 if (keyboard::KeyboardControllerProxy::keyboard_height >= 0) {
78 // The keyboard height is variable, use the recorded keyboard height to be
79 // the hit test mask.
80 keyboard_bounds.set_height(
81 keyboard::KeyboardControllerProxy::keyboard_height);
82 }
77 mask->addRect(RectToSkRect(keyboard_bounds)); 83 mask->addRect(RectToSkRect(keyboard_bounds));
78 } 84 }
79 virtual void DidRecreateLayer(ui::Layer* old_layer, 85 virtual void DidRecreateLayer(ui::Layer* old_layer,
80 ui::Layer* new_layer) OVERRIDE {} 86 ui::Layer* new_layer) OVERRIDE {}
81 87
82 gfx::Rect bounds_; 88 gfx::Rect bounds_;
83 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate); 89 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate);
84 }; 90 };
85 91
86 } // namespace 92 } // namespace
87 93
88 namespace keyboard { 94 namespace keyboard {
89 95
90 // LayoutManager for the virtual keyboard container. Manages a single window 96 // LayoutManager for the virtual keyboard container. Manages a single window
91 // (the virtual keyboard) and keeps it positioned at the bottom of the 97 // (the virtual keyboard) and keeps it positioned at the bottom of the
92 // owner window. 98 // owner window.
93 class KeyboardLayoutManager : public aura::LayoutManager { 99 class KeyboardLayoutManager : public aura::LayoutManager {
94 public: 100 public:
95 KeyboardLayoutManager(aura::Window* container) 101 KeyboardLayoutManager(KeyboardController* controller)
96 : container_(container), keyboard_(NULL) { 102 : controller_(controller), keyboard_(NULL) {
97 CHECK(container_);
98 } 103 }
99 104
100 // Overridden from aura::LayoutManager 105 // Overridden from aura::LayoutManager
101 virtual void OnWindowResized() OVERRIDE { 106 virtual void OnWindowResized() OVERRIDE {
102 if (!keyboard_) 107 if (!keyboard_)
103 return; 108 return;
104 SetChildBoundsDirect(keyboard_, 109 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(
105 KeyboardBoundsFromWindowBounds(container_->bounds())); 110 controller_->GetContainerWindow()->bounds());
111 // Records the keyboard new height. So that GetHitTestMask() can set the
112 // same keyboard bounds as the hit test mask.
113 KeyboardControllerProxy::keyboard_height = keyboard_bounds.height();
114 SetChildBoundsDirect(keyboard_, keyboard_bounds);
115 controller_->NotifyKeyboardBoundsChanged(keyboard_bounds);
106 } 116 }
107 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { 117 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
108 DCHECK(!keyboard_); 118 DCHECK(!keyboard_);
109 keyboard_ = child; 119 keyboard_ = child;
110 } 120 }
111 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} 121 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
112 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} 122 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
113 virtual void OnChildWindowVisibilityChanged(aura::Window* child, 123 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
114 bool visible) OVERRIDE {} 124 bool visible) OVERRIDE {}
115 virtual void SetChildBounds(aura::Window* child, 125 virtual void SetChildBounds(aura::Window* child,
116 const gfx::Rect& requested_bounds) OVERRIDE { 126 const gfx::Rect& requested_bounds) OVERRIDE {
kevers 2013/12/03 00:08:12 Chatted with Sadrul, and the main reason we had th
Shu Chen 2013/12/03 01:54:42 Done. I've changed it to non-static. What do you m
117 // Drop these: the size should only be set in OnWindowResized. 127 // SetChildBounds can be invoked by resizing from the container or by
128 // resizing from the contents (through window.resizeTo call in JS).
129 // OnWindowResized() can take care of resizing from the container.
130 // While here should only take care of resizing from the contents.
131 if (KeyboardControllerProxy::keyboard_resizing_from_contents) {
132 SetChildBoundsDirect(child, requested_bounds);
133 controller_->NotifyKeyboardBoundsChanged(requested_bounds);
134 }
118 } 135 }
119 136
120 private: 137 private:
121 aura::Window* container_; 138 KeyboardController* controller_;
122 aura::Window* keyboard_; 139 aura::Window* keyboard_;
123 140
124 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager); 141 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager);
125 }; 142 };
126 143
127 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) 144 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
128 : proxy_(proxy), 145 : proxy_(proxy),
129 input_method_(NULL), 146 input_method_(NULL),
130 keyboard_visible_(false), 147 keyboard_visible_(false),
131 weak_factory_(this) { 148 weak_factory_(this) {
132 CHECK(proxy); 149 CHECK(proxy);
133 input_method_ = proxy_->GetInputMethod(); 150 input_method_ = proxy_->GetInputMethod();
134 input_method_->AddObserver(this); 151 input_method_->AddObserver(this);
135 } 152 }
136 153
137 KeyboardController::~KeyboardController() { 154 KeyboardController::~KeyboardController() {
138 if (container_.get()) 155 if (container_.get())
139 container_->RemoveObserver(this); 156 container_->RemoveObserver(this);
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(new KeyboardWindowDelegate()));
147 container_->SetName("KeyboardContainer"); 164 container_->SetName("KeyboardContainer");
148 container_->set_owned_by_parent(false); 165 container_->set_owned_by_parent(false);
149 container_->Init(ui::LAYER_NOT_DRAWN); 166 container_->Init(ui::LAYER_NOT_DRAWN);
150 container_->AddObserver(this); 167 container_->AddObserver(this);
151 container_->SetLayoutManager(new KeyboardLayoutManager(container_.get())); 168 container_->SetLayoutManager(new KeyboardLayoutManager(this));
152 } 169 }
153 return container_.get(); 170 return container_.get();
154 } 171 }
155 172
173 void KeyboardController::NotifyKeyboardBoundsChanged(
174 const gfx::Rect& new_bounds, bool force) {
175 if (keyboard_visible_ || force) {
176 FOR_EACH_OBSERVER(KeyboardControllerObserver,
177 observer_list_,
178 OnKeyboardBoundsChanging(new_bounds));
179 }
180 }
181
156 void KeyboardController::HideKeyboard(HideReason reason) { 182 void KeyboardController::HideKeyboard(HideReason reason) {
157 keyboard_visible_ = false; 183 keyboard_visible_ = false;
158 184
159 keyboard::LogKeyboardControlEvent( 185 keyboard::LogKeyboardControlEvent(
160 reason == HIDE_REASON_AUTOMATIC ? 186 reason == HIDE_REASON_AUTOMATIC ?
161 keyboard::KEYBOARD_CONTROL_HIDE_AUTO : 187 keyboard::KEYBOARD_CONTROL_HIDE_AUTO :
162 keyboard::KEYBOARD_CONTROL_HIDE_USER); 188 keyboard::KEYBOARD_CONTROL_HIDE_USER);
163 189
164 FOR_EACH_OBSERVER(KeyboardControllerObserver, 190 NotifyKeyboardBoundsChanged(gfx::Rect(), true);
165 observer_list_,
166 OnKeyboardBoundsChanging(gfx::Rect()));
167 191
168 proxy_->HideKeyboardContainer(container_.get()); 192 proxy_->HideKeyboardContainer(container_.get());
169 } 193 }
170 194
171 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { 195 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) {
172 observer_list_.AddObserver(observer); 196 observer_list_.AddObserver(observer);
173 } 197 }
174 198
175 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { 199 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) {
176 observer_list_.RemoveObserver(observer); 200 observer_list_.RemoveObserver(observer);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 NotifyKeyboardBoundsChanged(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