Home | History | Annotate | Download | only in wm
      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.
      4 
      5 #include "ash/wm/lock_layout_manager.h"
      6 
      7 #include "ash/shell.h"
      8 #include "ash/shell_delegate.h"
      9 #include "ash/wm/lock_window_state.h"
     10 #include "ash/wm/window_state.h"
     11 #include "ash/wm/wm_event.h"
     12 #include "ui/aura/window.h"
     13 #include "ui/aura/window_observer.h"
     14 #include "ui/events/event.h"
     15 #include "ui/keyboard/keyboard_controller.h"
     16 #include "ui/keyboard/keyboard_util.h"
     17 
     18 namespace ash {
     19 
     20 LockLayoutManager::LockLayoutManager(aura::Window* window)
     21     : window_(window),
     22       root_window_(window->GetRootWindow()),
     23       is_observing_keyboard_(false) {
     24   Shell::GetInstance()->delegate()->AddVirtualKeyboardStateObserver(this);
     25   root_window_->AddObserver(this);
     26   if (keyboard::KeyboardController::GetInstance()) {
     27     keyboard::KeyboardController::GetInstance()->AddObserver(this);
     28     is_observing_keyboard_ = true;
     29   }
     30 }
     31 
     32 LockLayoutManager::~LockLayoutManager() {
     33   if (root_window_)
     34     root_window_->RemoveObserver(this);
     35 
     36   for (aura::Window::Windows::const_iterator it = window_->children().begin();
     37        it != window_->children().end(); ++it) {
     38     (*it)->RemoveObserver(this);
     39   }
     40 
     41   Shell::GetInstance()->delegate()->RemoveVirtualKeyboardStateObserver(this);
     42 
     43   if (keyboard::KeyboardController::GetInstance() && is_observing_keyboard_) {
     44     keyboard::KeyboardController::GetInstance()->RemoveObserver(this);
     45     is_observing_keyboard_ = false;
     46   }
     47 }
     48 
     49 void LockLayoutManager::OnWindowResized() {
     50   const wm::WMEvent event(wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED);
     51   AdjustWindowsForWorkAreaChange(&event);
     52 }
     53 
     54 void LockLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
     55   child->AddObserver(this);
     56 
     57   // LockWindowState replaces default WindowState of a child.
     58   wm::WindowState* window_state = LockWindowState::SetLockWindowState(child);
     59   wm::WMEvent event(wm::WM_EVENT_ADDED_TO_WORKSPACE);
     60   window_state->OnWMEvent(&event);
     61 }
     62 
     63 void LockLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) {
     64   child->RemoveObserver(this);
     65 }
     66 
     67 void LockLayoutManager::OnWindowRemovedFromLayout(aura::Window* child) {
     68 }
     69 
     70 void LockLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child,
     71                                                        bool visible) {
     72 }
     73 
     74 void LockLayoutManager::SetChildBounds(aura::Window* child,
     75                                        const gfx::Rect& requested_bounds) {
     76   wm::WindowState* window_state = wm::GetWindowState(child);
     77   wm::SetBoundsEvent event(wm::WM_EVENT_SET_BOUNDS, requested_bounds);
     78   window_state->OnWMEvent(&event);
     79 }
     80 
     81 void LockLayoutManager::OnWindowHierarchyChanged(
     82     const WindowObserver::HierarchyChangeParams& params) {
     83 }
     84 
     85 void LockLayoutManager::OnWindowPropertyChanged(aura::Window* window,
     86                                                 const void* key,
     87                                                 intptr_t old) {
     88 }
     89 
     90 void LockLayoutManager::OnWindowStackingChanged(aura::Window* window) {
     91 }
     92 
     93 void LockLayoutManager::OnWindowDestroying(aura::Window* window) {
     94   window->RemoveObserver(this);
     95   if (root_window_ == window)
     96     root_window_ = NULL;
     97 }
     98 
     99 void LockLayoutManager::OnWindowBoundsChanged(aura::Window* window,
    100                                               const gfx::Rect& old_bounds,
    101                                               const gfx::Rect& new_bounds) {
    102   if (root_window_ == window) {
    103     const wm::WMEvent wm_event(wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED);
    104     AdjustWindowsForWorkAreaChange(&wm_event);
    105   }
    106 }
    107 
    108 void LockLayoutManager::OnVirtualKeyboardStateChanged(bool activated) {
    109   if (keyboard::KeyboardController::GetInstance()) {
    110     if (activated) {
    111       if (!is_observing_keyboard_) {
    112         keyboard::KeyboardController::GetInstance()->AddObserver(this);
    113         is_observing_keyboard_ = true;
    114       }
    115     } else {
    116       keyboard::KeyboardController::GetInstance()->RemoveObserver(this);
    117       is_observing_keyboard_ = false;
    118     }
    119   }
    120 }
    121 
    122 void LockLayoutManager::OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) {
    123   keyboard_bounds_ = new_bounds;
    124   OnWindowResized();
    125 }
    126 
    127 void LockLayoutManager::AdjustWindowsForWorkAreaChange(
    128     const wm::WMEvent* event) {
    129   DCHECK(event->type() == wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED ||
    130          event->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED);
    131 
    132   for (aura::Window::Windows::const_iterator it = window_->children().begin();
    133        it != window_->children().end();
    134        ++it) {
    135     wm::GetWindowState(*it)->OnWMEvent(event);
    136   }
    137 }
    138 
    139 }  // namespace ash
    140