1 // Copyright 2014 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_layout_manager.h"
7 #include "ui/compositor/layer_animator.h"
8 #include "ui/keyboard/keyboard_controller.h"
9 #include "ui/keyboard/keyboard_controller_proxy.h"
10 #include "ui/keyboard/keyboard_util.h"
14 // Overridden from aura::LayoutManager
15 void KeyboardLayoutManager::OnWindowResized() {
17 // Container window is the top level window of the virtual keyboard window.
18 // To support window.moveTo for the virtual keyboard window, as it actually
19 // moves the top level window, the container window should be set to the
20 // desired bounds before changing the bounds of the virtual keyboard window.
21 gfx::Rect container_bounds
= controller_
->GetContainerWindow()->bounds();
22 // Always align container window and keyboard window.
23 SetChildBoundsDirect(keyboard_
, gfx::Rect(container_bounds
.size()));
27 void KeyboardLayoutManager::OnWindowAddedToLayout(aura::Window
* child
) {
30 if (controller_
->keyboard_mode() == FULL_WIDTH
) {
31 controller_
->GetContainerWindow()->SetBounds(gfx::Rect());
32 } else if (controller_
->keyboard_mode() == FLOATING
) {
33 controller_
->GetContainerWindow()->SetBounds(child
->bounds());
34 SetChildBoundsDirect(keyboard_
, gfx::Rect(child
->bounds().size()));
38 void KeyboardLayoutManager::SetChildBounds(aura::Window
* child
,
39 const gfx::Rect
& requested_bounds
) {
40 DCHECK(child
== keyboard_
);
42 // Request to change the bounds of child window (AKA the virtual keyboard
43 // window) should change the container window first. Then the child window is
44 // resized and covers the container window. Note the child's bound is only set
45 // in OnWindowResized.
46 gfx::Rect old_bounds
= controller_
->GetContainerWindow()->bounds();
47 gfx::Rect new_bounds
= requested_bounds
;
48 if (controller_
->keyboard_mode() == FULL_WIDTH
) {
49 const gfx::Rect
& window_bounds
=
50 controller_
->GetContainerWindow()->GetRootWindow()->bounds();
51 new_bounds
.set_y(window_bounds
.height() - new_bounds
.height());
52 // If shelf is positioned on the left side of screen, x is not 0. In
53 // FULL_WIDTH mode, the virtual keyboard should always align with the left
54 // edge of the screen. So manually set x to 0 here.
56 new_bounds
.set_width(window_bounds
.width());
58 // Keyboard bounds should only be reset when it actually changes. Otherwise
59 // it interrupts the initial animation of showing the keyboard. Described in
61 if (new_bounds
== old_bounds
) {
65 ui::LayerAnimator
* animator
=
66 controller_
->GetContainerWindow()->layer()->GetAnimator();
67 // Stops previous animation if a window resize is requested during animation.
68 if (animator
->is_animating())
69 animator
->StopAnimating();
71 controller_
->GetContainerWindow()->SetBounds(new_bounds
);
72 SetChildBoundsDirect(keyboard_
, gfx::Rect(new_bounds
.size()));
74 if (controller_
->keyboard_mode() == FULL_WIDTH
) {
75 if (old_bounds
.height() == 0 && child
->bounds().height() != 0 &&
76 controller_
->show_on_resize()) {
77 // The window height is set to 0 initially or before switch to an IME in a
78 // different extension. Virtual keyboard window may wait for this bounds
79 // change to correctly animate in.
80 controller_
->ShowKeyboard(false);
82 // We need to send out this notification only if keyboard is visible since
83 // keyboard window is resized even if keyboard is hidden.
84 if (controller_
->keyboard_visible())
85 controller_
->NotifyKeyboardBoundsChanging(requested_bounds
);
87 } else if (controller_
->keyboard_mode() == FLOATING
) {
88 controller_
->NotifyKeyboardBoundsChanging(gfx::Rect());
92 } // namespace keyboard