NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / ui / keyboard / keyboard_controller.cc
blob09fb34b397f99e8a599e890c5437aa8bdfdcf8fc
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
3 // found in the LICENSE file.
5 #include "ui/keyboard/keyboard_controller.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "ui/aura/layout_manager.h"
10 #include "ui/aura/window.h"
11 #include "ui/aura/window_delegate.h"
12 #include "ui/base/cursor/cursor.h"
13 #include "ui/base/hit_test.h"
14 #include "ui/base/ime/input_method.h"
15 #include "ui/base/ime/text_input_client.h"
16 #include "ui/base/ime/text_input_type.h"
17 #include "ui/compositor/layer_animation_observer.h"
18 #include "ui/compositor/scoped_layer_animation_settings.h"
19 #include "ui/gfx/path.h"
20 #include "ui/gfx/rect.h"
21 #include "ui/gfx/skia_util.h"
22 #include "ui/keyboard/keyboard_controller_observer.h"
23 #include "ui/keyboard/keyboard_controller_proxy.h"
24 #include "ui/keyboard/keyboard_switches.h"
25 #include "ui/keyboard/keyboard_util.h"
26 #include "ui/wm/public/masked_window_targeter.h"
28 namespace {
30 const int kHideKeyboardDelayMs = 100;
32 // The virtual keyboard show/hide animation duration.
33 const int kAnimationDurationMs = 200;
35 // The opacity of virtual keyboard container when show animation starts or
36 // hide animation finishes.
37 const float kAnimationStartOrAfterHideOpacity = 0.2f;
39 // The ratio between the height of the keyboard and the screen when using the
40 // usability keyboard.
41 const float kUsabilityKeyboardHeightRatio = 1.0f;
43 // The default ratio between the height of the keyboard and the screen.
44 const float kDefaultKeyboardHeightRatio = 0.3f;
46 // The ratio between the height of the keyboard and the screen when using the
47 // accessibility keyboard.
48 const float kAccessibilityKeyboardHeightRatio = 0.3f;
50 float GetKeyboardHeightRatio(){
51 if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
52 return kUsabilityKeyboardHeightRatio;
53 } else if (keyboard::GetAccessibilityKeyboardEnabled()) {
54 return kAccessibilityKeyboardHeightRatio;
56 return kDefaultKeyboardHeightRatio;
58 gfx::Rect KeyboardBoundsFromWindowBounds(const gfx::Rect& window_bounds) {
59 const float kKeyboardHeightRatio = GetKeyboardHeightRatio();
60 return gfx::Rect(
61 window_bounds.x(),
62 window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio),
63 window_bounds.width(),
64 window_bounds.height() * kKeyboardHeightRatio);
67 // Event targeter for the keyboard container.
68 class KeyboardContainerTargeter : public wm::MaskedWindowTargeter {
69 public:
70 KeyboardContainerTargeter(aura::Window* container,
71 keyboard::KeyboardControllerProxy* proxy)
72 : wm::MaskedWindowTargeter(container),
73 proxy_(proxy) {
76 virtual ~KeyboardContainerTargeter() {}
78 private:
79 // wm::MaskedWindowTargeter:
80 virtual bool GetHitTestMask(aura::Window* window,
81 gfx::Path* mask) const OVERRIDE {
82 gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
83 KeyboardBoundsFromWindowBounds(window->bounds());
84 mask->addRect(RectToSkRect(keyboard_bounds));
85 return true;
88 keyboard::KeyboardControllerProxy* proxy_;
90 DISALLOW_COPY_AND_ASSIGN(KeyboardContainerTargeter);
93 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus.
94 // This is necessary to make sure that the synthetic key-events reach the target
95 // window.
96 // The delegate deletes itself when the window is destroyed.
97 class KeyboardWindowDelegate : public aura::WindowDelegate {
98 public:
99 explicit KeyboardWindowDelegate(keyboard::KeyboardControllerProxy* proxy)
100 : proxy_(proxy) {}
101 virtual ~KeyboardWindowDelegate() {}
103 private:
104 // Overridden from aura::WindowDelegate:
105 virtual gfx::Size GetMinimumSize() const OVERRIDE { return gfx::Size(); }
106 virtual gfx::Size GetMaximumSize() const OVERRIDE { return gfx::Size(); }
107 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
108 const gfx::Rect& new_bounds) OVERRIDE {
109 bounds_ = new_bounds;
111 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
112 return gfx::kNullCursor;
114 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
115 return HTNOWHERE;
117 virtual bool ShouldDescendIntoChildForEventHandling(
118 aura::Window* child,
119 const gfx::Point& location) OVERRIDE {
120 return true;
122 virtual bool CanFocus() OVERRIDE { return false; }
123 virtual void OnCaptureLost() OVERRIDE {}
124 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {}
125 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
126 virtual void OnWindowDestroying() OVERRIDE {}
127 virtual void OnWindowDestroyed() OVERRIDE { delete this; }
128 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
129 virtual bool HasHitTestMask() const OVERRIDE { return true; }
130 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {
131 gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
132 KeyboardBoundsFromWindowBounds(bounds_);
133 mask->addRect(RectToSkRect(keyboard_bounds));
135 virtual void DidRecreateLayer(ui::Layer* old_layer,
136 ui::Layer* new_layer) OVERRIDE {}
138 gfx::Rect bounds_;
139 keyboard::KeyboardControllerProxy* proxy_;
141 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate);
144 } // namespace
146 namespace keyboard {
148 // Observer for both keyboard show and hide animations. It should be owned by
149 // KeyboardController.
150 class CallbackAnimationObserver : public ui::LayerAnimationObserver {
151 public:
152 CallbackAnimationObserver(ui::LayerAnimator* animator,
153 base::Callback<void(void)> callback);
154 virtual ~CallbackAnimationObserver();
156 private:
157 // Overridden from ui::LayerAnimationObserver:
158 virtual void OnLayerAnimationEnded(ui::LayerAnimationSequence* seq) OVERRIDE;
159 virtual void OnLayerAnimationAborted(
160 ui::LayerAnimationSequence* seq) OVERRIDE;
161 virtual void OnLayerAnimationScheduled(
162 ui::LayerAnimationSequence* seq) OVERRIDE {}
164 ui::LayerAnimator* animator_;
165 base::Callback<void(void)> callback_;
167 DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver);
170 CallbackAnimationObserver::CallbackAnimationObserver(
171 ui::LayerAnimator* animator, base::Callback<void(void)> callback)
172 : animator_(animator), callback_(callback) {
175 CallbackAnimationObserver::~CallbackAnimationObserver() {
176 animator_->RemoveObserver(this);
179 void CallbackAnimationObserver::OnLayerAnimationEnded(
180 ui::LayerAnimationSequence* seq) {
181 if (animator_->is_animating())
182 return;
183 animator_->RemoveObserver(this);
184 callback_.Run();
187 void CallbackAnimationObserver::OnLayerAnimationAborted(
188 ui::LayerAnimationSequence* seq) {
189 animator_->RemoveObserver(this);
192 // LayoutManager for the virtual keyboard container. Manages a single window
193 // (the virtual keyboard) and keeps it positioned at the bottom of the
194 // owner window.
195 class KeyboardLayoutManager : public aura::LayoutManager {
196 public:
197 explicit KeyboardLayoutManager(KeyboardController* controller)
198 : controller_(controller), keyboard_(NULL) {
201 // Overridden from aura::LayoutManager
202 virtual void OnWindowResized() OVERRIDE {
203 if (keyboard_ && !controller_->proxy()->resizing_from_contents())
204 ResizeKeyboardToDefault(keyboard_);
206 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
207 DCHECK(!keyboard_);
208 keyboard_ = child;
209 ResizeKeyboardToDefault(keyboard_);
211 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
212 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
213 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
214 bool visible) OVERRIDE {}
215 virtual void SetChildBounds(aura::Window* child,
216 const gfx::Rect& requested_bounds) OVERRIDE {
217 // SetChildBounds can be invoked by resizing from the container or by
218 // resizing from the contents (through window.resizeTo call in JS).
219 // The flag resizing_from_contents() is used to determine the keyboard is
220 // resizing from which.
221 if (controller_->proxy()->resizing_from_contents()) {
222 controller_->NotifyKeyboardBoundsChanging(requested_bounds);
223 SetChildBoundsDirect(child, requested_bounds);
227 private:
228 void ResizeKeyboardToDefault(aura::Window* child) {
229 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(
230 controller_->GetContainerWindow()->bounds());
231 SetChildBoundsDirect(child, keyboard_bounds);
234 KeyboardController* controller_;
235 aura::Window* keyboard_;
237 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager);
240 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
241 : proxy_(proxy),
242 input_method_(NULL),
243 keyboard_visible_(false),
244 lock_keyboard_(false),
245 weak_factory_(this) {
246 CHECK(proxy);
247 input_method_ = proxy_->GetInputMethod();
248 input_method_->AddObserver(this);
251 KeyboardController::~KeyboardController() {
252 if (container_)
253 container_->RemoveObserver(this);
254 if (input_method_)
255 input_method_->RemoveObserver(this);
258 aura::Window* KeyboardController::GetContainerWindow() {
259 if (!container_.get()) {
260 container_.reset(new aura::Window(
261 new KeyboardWindowDelegate(proxy_.get())));
262 container_->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
263 new KeyboardContainerTargeter(container_.get(), proxy_.get())));
264 container_->SetName("KeyboardContainer");
265 container_->set_owned_by_parent(false);
266 container_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
267 container_->AddObserver(this);
268 container_->SetLayoutManager(new KeyboardLayoutManager(this));
270 return container_.get();
273 void KeyboardController::NotifyKeyboardBoundsChanging(
274 const gfx::Rect& new_bounds) {
275 if (proxy_->HasKeyboardWindow() && proxy_->GetKeyboardWindow()->IsVisible()) {
276 FOR_EACH_OBSERVER(KeyboardControllerObserver,
277 observer_list_,
278 OnKeyboardBoundsChanging(new_bounds));
282 void KeyboardController::HideKeyboard(HideReason reason) {
283 keyboard_visible_ = false;
285 keyboard::LogKeyboardControlEvent(
286 reason == HIDE_REASON_AUTOMATIC ?
287 keyboard::KEYBOARD_CONTROL_HIDE_AUTO :
288 keyboard::KEYBOARD_CONTROL_HIDE_USER);
290 NotifyKeyboardBoundsChanging(gfx::Rect());
292 ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
293 animation_observer_.reset(new CallbackAnimationObserver(
294 container_animator,
295 base::Bind(&KeyboardController::HideAnimationFinished,
296 base::Unretained(this))));
297 container_animator->AddObserver(animation_observer_.get());
299 ui::ScopedLayerAnimationSettings settings(container_animator);
300 settings.SetTweenType(gfx::Tween::EASE_OUT);
301 settings.SetTransitionDuration(
302 base::TimeDelta::FromMilliseconds(kAnimationDurationMs));
303 gfx::Transform transform;
304 transform.Translate(0, proxy_->GetKeyboardWindow()->bounds().height());
305 container_->SetTransform(transform);
306 container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
309 void KeyboardController::AddObserver(KeyboardControllerObserver* observer) {
310 observer_list_.AddObserver(observer);
313 void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) {
314 observer_list_.RemoveObserver(observer);
317 void KeyboardController::OnWindowHierarchyChanged(
318 const HierarchyChangeParams& params) {
319 if (params.new_parent && params.target == container_.get())
320 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient());
323 void KeyboardController::SetOverrideContentUrl(const GURL& url) {
324 proxy_->SetOverrideContentUrl(url);
327 void KeyboardController::OnTextInputStateChanged(
328 const ui::TextInputClient* client) {
329 if (!container_.get())
330 return;
332 if (IsKeyboardUsabilityExperimentEnabled()) {
333 OnShowImeIfNeeded();
334 return;
337 ui::TextInputType type =
338 client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
339 if (type == ui::TEXT_INPUT_TYPE_NONE && !lock_keyboard_) {
340 if (keyboard_visible_) {
341 // Set the visibility state here so that any queries for visibility
342 // before the timer fires returns the correct future value.
343 keyboard_visible_ = false;
344 base::MessageLoop::current()->PostDelayedTask(
345 FROM_HERE,
346 base::Bind(&KeyboardController::HideKeyboard,
347 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC),
348 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs));
350 } else {
351 // Abort a pending keyboard hide.
352 if (WillHideKeyboard()) {
353 weak_factory_.InvalidateWeakPtrs();
354 keyboard_visible_ = true;
356 proxy_->SetUpdateInputType(type);
357 // Do not explicitly show the Virtual keyboard unless it is in the process
358 // of hiding. Instead, the virtual keyboard is shown in response to a user
359 // gesture (mouse or touch) that is received while an element has input
360 // focus. Showing the keyboard requires an explicit call to
361 // OnShowImeIfNeeded.
363 // TODO(bryeung): whenever the TextInputClient changes we need to notify the
364 // keyboard (with the TextInputType) so that it can reset it's state (e.g.
365 // abandon compositions in progress)
368 void KeyboardController::OnInputMethodDestroyed(
369 const ui::InputMethod* input_method) {
370 DCHECK_EQ(input_method_, input_method);
371 input_method_ = NULL;
374 void KeyboardController::OnShowImeIfNeeded() {
375 if (!container_.get())
376 return;
378 if (container_->children().empty()) {
379 keyboard::MarkKeyboardLoadStarted();
380 aura::Window* keyboard = proxy_->GetKeyboardWindow();
381 keyboard->Show();
382 container_->AddChild(keyboard);
383 keyboard->set_owned_by_parent(false);
385 if (keyboard_visible_)
386 return;
388 keyboard_visible_ = true;
390 // If the controller is in the process of hiding the keyboard, do not log
391 // the stat here since the keyboard will not actually be shown.
392 if (!WillHideKeyboard())
393 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW);
395 weak_factory_.InvalidateWeakPtrs();
397 // If |container_| has hide animation, its visibility is set to false when
398 // hide animation finished. So even if the container is visible at this
399 // point, it may in the process of hiding. We still need to show keyboard
400 // container in this case.
401 if (container_->IsVisible() &&
402 !container_->layer()->GetAnimator()->is_animating())
403 return;
405 ShowKeyboard();
408 void KeyboardController::ShowKeyboard() {
409 ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
411 // If the container is not animating, makes sure the position and opacity
412 // are at begin states for animation.
413 if (!container_animator->is_animating()) {
414 gfx::Transform transform;
415 transform.Translate(0, proxy_->GetKeyboardWindow()->bounds().height());
416 container_->SetTransform(transform);
417 container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
420 container_animator->set_preemption_strategy(
421 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
422 animation_observer_.reset(new CallbackAnimationObserver(
423 container_animator,
424 base::Bind(&KeyboardController::ShowAnimationFinished,
425 base::Unretained(this))));
426 container_animator->AddObserver(animation_observer_.get());
429 // Scope the following animation settings as we don't want to animate
430 // visibility change that triggered by a call to the base class function
431 // ShowKeyboardContainer with these settings. The container should become
432 // visible immediately.
433 ui::ScopedLayerAnimationSettings settings(container_animator);
434 settings.SetTweenType(gfx::Tween::EASE_IN);
435 settings.SetTransitionDuration(
436 base::TimeDelta::FromMilliseconds(kAnimationDurationMs));
437 container_->SetTransform(gfx::Transform());
438 container_->layer()->SetOpacity(1.0);
441 proxy_->ShowKeyboardContainer(container_.get());
444 bool KeyboardController::WillHideKeyboard() const {
445 return weak_factory_.HasWeakPtrs();
448 void KeyboardController::ShowAnimationFinished() {
449 // Notify observers after animation finished to prevent reveal desktop
450 // background during animation.
451 NotifyKeyboardBoundsChanging(proxy_->GetKeyboardWindow()->bounds());
452 proxy_->EnsureCaretInWorkArea();
455 void KeyboardController::HideAnimationFinished() {
456 proxy_->HideKeyboardContainer(container_.get());
459 } // namespace keyboard