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