Add a11y virtual keyboard icon when a11y VK is enabled
[chromium-blink-merge.git] / ui / keyboard / keyboard_controller.cc
blob17923d3f800a3d5bd0cddc5a47627d7f7d1b91db
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(aura::Window* window) OVERRIDE {}
127 virtual void OnWindowDestroyed(aura::Window* window) 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::ShowAndLockKeyboard() {
318 set_lock_keyboard(true);
319 OnShowImeIfNeeded();
322 void KeyboardController::OnWindowHierarchyChanged(
323 const HierarchyChangeParams& params) {
324 if (params.new_parent && params.target == container_.get())
325 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient());
328 void KeyboardController::SetOverrideContentUrl(const GURL& url) {
329 proxy_->SetOverrideContentUrl(url);
332 void KeyboardController::OnTextInputStateChanged(
333 const ui::TextInputClient* client) {
334 if (!container_.get())
335 return;
337 if (IsKeyboardUsabilityExperimentEnabled()) {
338 OnShowImeIfNeeded();
339 return;
342 ui::TextInputType type =
343 client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
344 if (type == ui::TEXT_INPUT_TYPE_NONE && !lock_keyboard_) {
345 if (keyboard_visible_) {
346 // Set the visibility state here so that any queries for visibility
347 // before the timer fires returns the correct future value.
348 keyboard_visible_ = false;
349 base::MessageLoop::current()->PostDelayedTask(
350 FROM_HERE,
351 base::Bind(&KeyboardController::HideKeyboard,
352 weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC),
353 base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs));
355 } else {
356 // Abort a pending keyboard hide.
357 if (WillHideKeyboard()) {
358 weak_factory_.InvalidateWeakPtrs();
359 keyboard_visible_ = true;
361 proxy_->SetUpdateInputType(type);
362 // Do not explicitly show the Virtual keyboard unless it is in the process
363 // of hiding. Instead, the virtual keyboard is shown in response to a user
364 // gesture (mouse or touch) that is received while an element has input
365 // focus. Showing the keyboard requires an explicit call to
366 // OnShowImeIfNeeded.
368 // TODO(bryeung): whenever the TextInputClient changes we need to notify the
369 // keyboard (with the TextInputType) so that it can reset it's state (e.g.
370 // abandon compositions in progress)
373 void KeyboardController::OnInputMethodDestroyed(
374 const ui::InputMethod* input_method) {
375 DCHECK_EQ(input_method_, input_method);
376 input_method_ = NULL;
379 void KeyboardController::OnShowImeIfNeeded() {
380 if (!container_.get())
381 return;
383 if (container_->children().empty()) {
384 keyboard::MarkKeyboardLoadStarted();
385 aura::Window* keyboard = proxy_->GetKeyboardWindow();
386 keyboard->Show();
387 container_->AddChild(keyboard);
388 keyboard->set_owned_by_parent(false);
390 if (keyboard_visible_)
391 return;
393 keyboard_visible_ = true;
395 // If the controller is in the process of hiding the keyboard, do not log
396 // the stat here since the keyboard will not actually be shown.
397 if (!WillHideKeyboard())
398 keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW);
400 weak_factory_.InvalidateWeakPtrs();
402 // If |container_| has hide animation, its visibility is set to false when
403 // hide animation finished. So even if the container is visible at this
404 // point, it may in the process of hiding. We still need to show keyboard
405 // container in this case.
406 if (container_->IsVisible() &&
407 !container_->layer()->GetAnimator()->is_animating())
408 return;
410 ShowKeyboard();
413 void KeyboardController::ShowKeyboard() {
414 ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
416 // If the container is not animating, makes sure the position and opacity
417 // are at begin states for animation.
418 if (!container_animator->is_animating()) {
419 gfx::Transform transform;
420 transform.Translate(0, proxy_->GetKeyboardWindow()->bounds().height());
421 container_->SetTransform(transform);
422 container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
425 container_animator->set_preemption_strategy(
426 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
427 animation_observer_.reset(new CallbackAnimationObserver(
428 container_animator,
429 base::Bind(&KeyboardController::ShowAnimationFinished,
430 base::Unretained(this))));
431 container_animator->AddObserver(animation_observer_.get());
434 // Scope the following animation settings as we don't want to animate
435 // visibility change that triggered by a call to the base class function
436 // ShowKeyboardContainer with these settings. The container should become
437 // visible immediately.
438 ui::ScopedLayerAnimationSettings settings(container_animator);
439 settings.SetTweenType(gfx::Tween::EASE_IN);
440 settings.SetTransitionDuration(
441 base::TimeDelta::FromMilliseconds(kAnimationDurationMs));
442 container_->SetTransform(gfx::Transform());
443 container_->layer()->SetOpacity(1.0);
446 proxy_->ShowKeyboardContainer(container_.get());
449 bool KeyboardController::WillHideKeyboard() const {
450 return weak_factory_.HasWeakPtrs();
453 void KeyboardController::ShowAnimationFinished() {
454 // Notify observers after animation finished to prevent reveal desktop
455 // background during animation.
456 NotifyKeyboardBoundsChanging(proxy_->GetKeyboardWindow()->bounds());
457 proxy_->EnsureCaretInWorkArea();
460 void KeyboardController::HideAnimationFinished() {
461 proxy_->HideKeyboardContainer(container_.get());
464 } // namespace keyboard