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