Chromium Code Reviews| 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 } | |
| 56 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { | 55 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { |
| 57 return gfx::kNullCursor; | 56 return gfx::kNullCursor; |
| 58 } | 57 } |
| 59 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { | 58 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { |
| 60 return HTNOWHERE; | 59 return HTNOWHERE; |
| 61 } | 60 } |
| 62 virtual bool ShouldDescendIntoChildForEventHandling( | 61 virtual bool ShouldDescendIntoChildForEventHandling( |
| 63 aura::Window* child, | 62 aura::Window* child, |
| 64 const gfx::Point& location) OVERRIDE { | 63 const gfx::Point& location) OVERRIDE { |
| 65 return true; | 64 return true; |
| 66 } | 65 } |
| 67 virtual bool CanFocus() OVERRIDE { return false; } | 66 virtual bool CanFocus() OVERRIDE { return false; } |
| 68 virtual void OnCaptureLost() OVERRIDE {} | 67 virtual void OnCaptureLost() OVERRIDE {} |
| 69 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {} | 68 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {} |
| 70 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {} | 69 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {} |
| 71 virtual void OnWindowDestroying() OVERRIDE {} | 70 virtual void OnWindowDestroying() OVERRIDE {} |
| 72 virtual void OnWindowDestroyed() OVERRIDE { delete this; } | 71 virtual void OnWindowDestroyed() OVERRIDE { delete this; } |
| 73 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {} | 72 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {} |
| 74 virtual bool HasHitTestMask() const OVERRIDE { return true; } | 73 virtual bool HasHitTestMask() const OVERRIDE { return true; } |
| 75 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE { | 74 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE { |
| 76 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(bounds_); | 75 gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() |
| 76 : gfx::Rect(); | |
| 77 mask->addRect(RectToSkRect(keyboard_bounds)); | 77 mask->addRect(RectToSkRect(keyboard_bounds)); |
| 78 } | 78 } |
| 79 virtual void DidRecreateLayer(ui::Layer* old_layer, | 79 virtual void DidRecreateLayer(ui::Layer* old_layer, |
| 80 ui::Layer* new_layer) OVERRIDE {} | 80 ui::Layer* new_layer) OVERRIDE {} |
| 81 | 81 |
| 82 gfx::Rect bounds_; | 82 keyboard::KeyboardControllerProxy* proxy_; |
| 83 | |
| 83 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate); | 84 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate); |
| 84 }; | 85 }; |
| 85 | 86 |
| 86 } // namespace | 87 } // namespace |
| 87 | 88 |
| 88 namespace keyboard { | 89 namespace keyboard { |
| 89 | 90 |
| 90 // LayoutManager for the virtual keyboard container. Manages a single window | 91 // LayoutManager for the virtual keyboard container. Manages a single window |
| 91 // (the virtual keyboard) and keeps it positioned at the bottom of the | 92 // (the virtual keyboard) and keeps it positioned at the bottom of the |
| 92 // owner window. | 93 // owner window. |
| 93 class KeyboardLayoutManager : public aura::LayoutManager { | 94 class KeyboardLayoutManager : public aura::LayoutManager { |
| 94 public: | 95 public: |
| 95 KeyboardLayoutManager(aura::Window* container) | 96 KeyboardLayoutManager(KeyboardController* controller) |
| 96 : container_(container), keyboard_(NULL) { | 97 : controller_(controller), keyboard_(NULL) { |
| 97 CHECK(container_); | |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Overridden from aura::LayoutManager | 100 // Overridden from aura::LayoutManager |
| 101 virtual void OnWindowResized() OVERRIDE { | 101 virtual void OnWindowResized() OVERRIDE { |
| 102 if (!keyboard_) | 102 if (!keyboard_) |
| 103 return; | 103 return; |
| 104 SetChildBoundsDirect(keyboard_, | 104 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds( |
| 105 KeyboardBoundsFromWindowBounds(container_->bounds())); | 105 controller_->GetContainerWindow()->bounds()); |
| 106 // Records the keyboard new height. So that GetHitTestMask() can set the | |
| 107 // same keyboard bounds as the hit test mask. | |
| 108 SetChildBoundsDirect(keyboard_, keyboard_bounds); | |
| 109 controller_->NotifyKeyboardBoundsChanged(keyboard_bounds); | |
|
kevers
2013/12/03 02:53:07
Is NotifyKeyboardBoundsChanged going to fire twice
Shu Chen
2013/12/03 04:12:25
Done. I've removed this line and add comments.
| |
| 106 } | 110 } |
| 107 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { | 111 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { |
| 108 DCHECK(!keyboard_); | 112 DCHECK(!keyboard_); |
| 109 keyboard_ = child; | 113 keyboard_ = child; |
| 110 } | 114 } |
| 111 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} | 115 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} |
| 112 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} | 116 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} |
| 113 virtual void OnChildWindowVisibilityChanged(aura::Window* child, | 117 virtual void OnChildWindowVisibilityChanged(aura::Window* child, |
| 114 bool visible) OVERRIDE {} | 118 bool visible) OVERRIDE {} |
| 115 virtual void SetChildBounds(aura::Window* child, | 119 virtual void SetChildBounds(aura::Window* child, |
| 116 const gfx::Rect& requested_bounds) OVERRIDE { | 120 const gfx::Rect& requested_bounds) OVERRIDE { |
| 117 // Drop these: the size should only be set in OnWindowResized. | 121 // SetChildBounds can be invoked by resizing from the container or by |
| 122 // resizing from the contents (through window.resizeTo call in JS). | |
| 123 // OnWindowResized() can take care of resizing from the container. | |
| 124 // While here should only take care of resizing from the contents. | |
| 125 if (controller_->proxy()->resizing_from_contents()) { | |
| 126 SetChildBoundsDirect(child, requested_bounds); | |
| 127 controller_->NotifyKeyboardBoundsChanged(requested_bounds); | |
| 128 controller_->proxy()->set_resizing_from_contents(false); | |
| 129 } | |
| 118 } | 130 } |
| 119 | 131 |
| 120 private: | 132 private: |
| 121 aura::Window* container_; | 133 KeyboardController* controller_; |
| 122 aura::Window* keyboard_; | 134 aura::Window* keyboard_; |
| 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); |
| 140 if (input_method_) | 152 if (input_method_) |
| 141 input_method_->RemoveObserver(this); | 153 input_method_->RemoveObserver(this); |
| 142 } | 154 } |
| 143 | 155 |
| 144 aura::Window* KeyboardController::GetContainerWindow() { | 156 aura::Window* KeyboardController::GetContainerWindow() { |
| 145 if (!container_.get()) { | 157 if (!container_.get()) { |
| 146 container_.reset(new aura::Window(new KeyboardWindowDelegate())); | 158 container_.reset(new aura::Window( |
| 159 new KeyboardWindowDelegate(proxy_.get()))); | |
| 147 container_->SetName("KeyboardContainer"); | 160 container_->SetName("KeyboardContainer"); |
| 148 container_->set_owned_by_parent(false); | 161 container_->set_owned_by_parent(false); |
| 149 container_->Init(ui::LAYER_NOT_DRAWN); | 162 container_->Init(ui::LAYER_NOT_DRAWN); |
| 150 container_->AddObserver(this); | 163 container_->AddObserver(this); |
| 151 container_->SetLayoutManager(new KeyboardLayoutManager(container_.get())); | 164 container_->SetLayoutManager(new KeyboardLayoutManager(this)); |
| 152 } | 165 } |
| 153 return container_.get(); | 166 return container_.get(); |
| 154 } | 167 } |
| 155 | 168 |
| 169 void KeyboardController::NotifyKeyboardBoundsChanged( | |
| 170 const gfx::Rect& new_bounds) { | |
| 171 if (proxy_ && proxy_->GetKeyboardWindow()->IsVisible()) { | |
| 172 FOR_EACH_OBSERVER(KeyboardControllerObserver, | |
| 173 observer_list_, | |
| 174 OnKeyboardBoundsChanging(new_bounds)); | |
| 175 } | |
| 176 } | |
| 177 | |
| 156 void KeyboardController::HideKeyboard(HideReason reason) { | 178 void KeyboardController::HideKeyboard(HideReason reason) { |
| 157 keyboard_visible_ = false; | 179 keyboard_visible_ = false; |
| 158 | 180 |
| 159 keyboard::LogKeyboardControlEvent( | 181 keyboard::LogKeyboardControlEvent( |
| 160 reason == HIDE_REASON_AUTOMATIC ? | 182 reason == HIDE_REASON_AUTOMATIC ? |
| 161 keyboard::KEYBOARD_CONTROL_HIDE_AUTO : | 183 keyboard::KEYBOARD_CONTROL_HIDE_AUTO : |
| 162 keyboard::KEYBOARD_CONTROL_HIDE_USER); | 184 keyboard::KEYBOARD_CONTROL_HIDE_USER); |
| 163 | 185 |
| 164 FOR_EACH_OBSERVER(KeyboardControllerObserver, | 186 NotifyKeyboardBoundsChanged(gfx::Rect()); |
|
kevers
2013/12/03 02:53:07
Will we now get extra notifications: here and when
Shu Chen
2013/12/03 04:12:25
Done. I've renamed the method to NotifyKeyboardBou
| |
| 165 observer_list_, | |
| 166 OnKeyboardBoundsChanging(gfx::Rect())); | |
| 167 | 187 |
| 168 proxy_->HideKeyboardContainer(container_.get()); | 188 proxy_->HideKeyboardContainer(container_.get()); |
| 169 } | 189 } |
| 170 | 190 |
| 171 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { | 191 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) { |
| 172 observer_list_.AddObserver(observer); | 192 observer_list_.AddObserver(observer); |
| 173 } | 193 } |
| 174 | 194 |
| 175 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { | 195 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) { |
| 176 observer_list_.RemoveObserver(observer); | 196 observer_list_.RemoveObserver(observer); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 | 238 |
| 219 // If the controller is in the process of hiding the keyboard, do not log | 239 // 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. | 240 // the stat here since the keyboard will not actually be shown. |
| 221 if (!WillHideKeyboard()) | 241 if (!WillHideKeyboard()) |
| 222 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW); | 242 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW); |
| 223 | 243 |
| 224 weak_factory_.InvalidateWeakPtrs(); | 244 weak_factory_.InvalidateWeakPtrs(); |
| 225 if (container_->IsVisible()) | 245 if (container_->IsVisible()) |
| 226 return; | 246 return; |
| 227 | 247 |
| 228 FOR_EACH_OBSERVER( | 248 NotifyKeyboardBoundsChanged(container_->children()[0]->bounds()); |
|
kevers
2013/12/03 02:53:07
See previous comment.
Shu Chen
2013/12/03 04:12:25
Done.
| |
| 229 KeyboardControllerObserver, | 249 |
| 230 observer_list_, | |
| 231 OnKeyboardBoundsChanging(container_->children()[0]->bounds())); | |
| 232 proxy_->ShowKeyboardContainer(container_.get()); | 250 proxy_->ShowKeyboardContainer(container_.get()); |
| 233 } else { | 251 } else { |
| 234 // Set the visibility state here so that any queries for visibility | 252 // Set the visibility state here so that any queries for visibility |
| 235 // before the timer fires returns the correct future value. | 253 // before the timer fires returns the correct future value. |
| 236 keyboard_visible_ = false; | 254 keyboard_visible_ = false; |
| 237 base::MessageLoop::current()->PostDelayedTask( | 255 base::MessageLoop::current()->PostDelayedTask( |
| 238 FROM_HERE, | 256 FROM_HERE, |
| 239 base::Bind(&KeyboardController::HideKeyboard, | 257 base::Bind(&KeyboardController::HideKeyboard, |
| 240 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC), | 258 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC), |
| 241 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs)); | 259 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs)); |
| 242 } | 260 } |
| 243 } | 261 } |
| 244 // TODO(bryeung): whenever the TextInputClient changes we need to notify the | 262 // 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. | 263 // keyboard (with the TextInputType) so that it can reset it's state (e.g. |
| 246 // abandon compositions in progress) | 264 // abandon compositions in progress) |
| 247 } | 265 } |
| 248 | 266 |
| 249 void KeyboardController::OnInputMethodDestroyed( | 267 void KeyboardController::OnInputMethodDestroyed( |
| 250 const ui::InputMethod* input_method) { | 268 const ui::InputMethod* input_method) { |
| 251 DCHECK_EQ(input_method_, input_method); | 269 DCHECK_EQ(input_method_, input_method); |
| 252 input_method_ = NULL; | 270 input_method_ = NULL; |
| 253 } | 271 } |
| 254 | 272 |
| 255 bool KeyboardController::WillHideKeyboard() const { | 273 bool KeyboardController::WillHideKeyboard() const { |
| 256 return weak_factory_.HasWeakPtrs(); | 274 return weak_factory_.HasWeakPtrs(); |
| 257 } | 275 } |
| 258 | 276 |
| 259 } // namespace keyboard | 277 } // namespace keyboard |
| OLD | NEW |