1effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// found in the LICENSE file.
4effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "ui/keyboard/keyboard_layout_manager.h"
6effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "ui/compositor/layer_animator.h"
8effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "ui/keyboard/keyboard_controller.h"
9effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "ui/keyboard/keyboard_controller_proxy.h"
10effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "ui/keyboard/keyboard_util.h"
11effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
12effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochnamespace keyboard {
13effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
14effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Overridden from aura::LayoutManager
15effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid KeyboardLayoutManager::OnWindowResized() {
160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (keyboard_) {
170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    gfx::Rect window_bounds = controller_->GetContainerWindow()->bounds();
180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // Keep the same height when window resize. It usually get called when
190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // screen rotate.
200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int height = keyboard_->bounds().height();
210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    keyboard_->SetBounds(gfx::Rect(
220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        window_bounds.x(),
230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        window_bounds.bottom() - height,
240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        window_bounds.width(),
250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch        height));
260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
27effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid KeyboardLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
30effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK(!keyboard_);
31effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  keyboard_ = child;
320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  keyboard_->SetBounds(DefaultKeyboardBoundsFromWindowBounds(
330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      controller_->GetContainerWindow()->bounds()));
34effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
35effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
36effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid KeyboardLayoutManager::SetChildBounds(aura::Window* child,
37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                           const gfx::Rect& requested_bounds) {
38effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // SetChildBounds can be invoked by resizing from the container or by
39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // resizing from the contents (through window.resizeTo call in JS).
40effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The flag resizing_from_contents() is used to determine the source of the
41effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // resize.
420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  DCHECK(child == keyboard_);
430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  ui::LayerAnimator* animator =
450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      controller_->GetContainerWindow()->layer()->GetAnimator();
460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Stops previous animation if a window resize is requested during animation.
470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (animator->is_animating())
480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    animator->StopAnimating();
490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  gfx::Rect old_bounds = child->bounds();
510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  SetChildBoundsDirect(child, requested_bounds);
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (old_bounds.height() == 0 && child->bounds().height() != 0 &&
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      controller_->show_on_resize()) {
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // The window height is set to 0 initially or before switch to an IME in a
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // different extension. Virtual keyboard window may wait for this bounds
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // change to correctly animate in.
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    controller_->ShowKeyboard(false);
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  } else {
5903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    // We need to send out this notification only if keyboard is visible since
6003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    // keyboard window is resized even if keyboard is hidden.
6103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    if (controller_->keyboard_visible())
6203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      controller_->NotifyKeyboardBoundsChanging(requested_bounds);
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}  // namespace keyboard
67