1 // Copyright (c) 2012 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 "ash/wm/workspace/workspace_layout_manager.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/screen_util.h"
10 #include "ash/session_state_delegate.h"
11 #include "ash/shelf/shelf_layout_manager.h"
12 #include "ash/shell.h"
13 #include "ash/wm/always_on_top_controller.h"
14 #include "ash/wm/window_animations.h"
15 #include "ash/wm/window_positioner.h"
16 #include "ash/wm/window_properties.h"
17 #include "ash/wm/window_state.h"
18 #include "ash/wm/window_util.h"
19 #include "ash/wm/wm_event.h"
20 #include "ash/wm/workspace/workspace_layout_manager_delegate.h"
21 #include "ui/aura/client/aura_constants.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_observer.h"
24 #include "ui/base/ui_base_types.h"
25 #include "ui/compositor/layer.h"
26 #include "ui/events/event.h"
27 #include "ui/gfx/screen.h"
28 #include "ui/wm/core/window_util.h"
29 #include "ui/wm/public/activation_client.h"
37 WorkspaceLayoutManager::WorkspaceLayoutManager(aura::Window* window)
40 root_window_(window->GetRootWindow()),
41 work_area_in_parent_(ScreenUtil::ConvertRectFromScreen(
43 Shell::GetScreen()->GetDisplayNearestWindow(window_).work_area())),
44 is_fullscreen_(GetRootWindowController(
45 window->GetRootWindow())->GetWindowForFullscreenMode() != NULL) {
46 Shell::GetInstance()->activation_client()->AddObserver(this);
47 Shell::GetInstance()->AddShellObserver(this);
48 root_window_->AddObserver(this);
51 WorkspaceLayoutManager::~WorkspaceLayoutManager() {
53 root_window_->RemoveObserver(this);
54 for (WindowSet::const_iterator i = windows_.begin(); i != windows_.end(); ++i)
55 (*i)->RemoveObserver(this);
56 Shell::GetInstance()->RemoveShellObserver(this);
57 Shell::GetInstance()->activation_client()->RemoveObserver(this);
60 void WorkspaceLayoutManager::SetShelf(internal::ShelfLayoutManager* shelf) {
64 void WorkspaceLayoutManager::SetMaximizeBackdropDelegate(
65 scoped_ptr<WorkspaceLayoutManagerDelegate> delegate) {
66 backdrop_delegate_.reset(delegate.release());
69 //////////////////////////////////////////////////////////////////////////////
70 // WorkspaceLayoutManager, aura::LayoutManager implementation:
72 void WorkspaceLayoutManager::OnWindowAddedToLayout(Window* child) {
73 wm::WindowState* window_state = wm::GetWindowState(child);
74 wm::WMEvent event(wm::WM_EVENT_ADDED_TO_WORKSPACE);
75 window_state->OnWMEvent(&event);
76 windows_.insert(child);
77 child->AddObserver(this);
78 window_state->AddObserver(this);
79 UpdateShelfVisibility();
80 UpdateFullscreenState();
81 if (backdrop_delegate_)
82 backdrop_delegate_->OnWindowAddedToLayout(child);
83 WindowPositioner::RearrangeVisibleWindowOnShow(child);
86 void WorkspaceLayoutManager::OnWillRemoveWindowFromLayout(Window* child) {
87 windows_.erase(child);
88 child->RemoveObserver(this);
89 wm::GetWindowState(child)->RemoveObserver(this);
91 if (child->TargetVisibility())
92 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child);
95 void WorkspaceLayoutManager::OnWindowRemovedFromLayout(Window* child) {
96 UpdateShelfVisibility();
97 UpdateFullscreenState();
98 if (backdrop_delegate_)
99 backdrop_delegate_->OnWindowRemovedFromLayout(child);
102 void WorkspaceLayoutManager::OnChildWindowVisibilityChanged(Window* child,
104 wm::WindowState* window_state = wm::GetWindowState(child);
105 // Attempting to show a minimized window. Unminimize it.
106 if (visible && window_state->IsMinimized())
107 window_state->Unminimize();
109 if (child->TargetVisibility())
110 WindowPositioner::RearrangeVisibleWindowOnShow(child);
112 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child);
113 UpdateFullscreenState();
114 UpdateShelfVisibility();
115 if (backdrop_delegate_)
116 backdrop_delegate_->OnChildWindowVisibilityChanged(child, visible);
119 void WorkspaceLayoutManager::SetChildBounds(
121 const gfx::Rect& requested_bounds) {
122 wm::WindowState* window_state = wm::GetWindowState(child);
123 wm::SetBoundsEvent event(wm::WM_EVENT_SET_BOUNDS, requested_bounds);
124 window_state->OnWMEvent(&event);
125 UpdateShelfVisibility();
128 //////////////////////////////////////////////////////////////////////////////
129 // WorkspaceLayoutManager, ash::ShellObserver implementation:
131 void WorkspaceLayoutManager::OnDisplayWorkAreaInsetsChanged() {
132 const gfx::Rect work_area(ScreenUtil::ConvertRectFromScreen(
134 Shell::GetScreen()->GetDisplayNearestWindow(window_).work_area()));
135 if (work_area != work_area_in_parent_) {
136 const wm::WMEvent event(wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED);
137 AdjustAllWindowsBoundsForWorkAreaChange(&event);
141 //////////////////////////////////////////////////////////////////////////////
142 // WorkspaceLayoutManager, aura::WindowObserver implementation:
144 void WorkspaceLayoutManager::OnWindowHierarchyChanged(
145 const WindowObserver::HierarchyChangeParams& params) {
146 if (!wm::GetWindowState(params.target)->IsActive())
148 // If the window is already tracked by the workspace this update would be
149 // redundant as the fullscreen and shelf state would have been handled in
150 // OnWindowAddedToLayout.
151 if (windows_.find(params.target) != windows_.end())
154 // If the active window has moved to this root window then update the
156 // TODO(flackr): Track the active window leaving this root window and update
157 // the fullscreen state accordingly.
158 if (params.new_parent && params.new_parent->GetRootWindow() == root_window_) {
159 UpdateFullscreenState();
160 UpdateShelfVisibility();
164 void WorkspaceLayoutManager::OnWindowPropertyChanged(Window* window,
167 if (key == aura::client::kAlwaysOnTopKey &&
168 window->GetProperty(aura::client::kAlwaysOnTopKey)) {
169 GetRootWindowController(window->GetRootWindow())->
170 always_on_top_controller()->GetContainer(window)->AddChild(window);
174 void WorkspaceLayoutManager::OnWindowStackingChanged(aura::Window* window) {
175 UpdateShelfVisibility();
176 UpdateFullscreenState();
177 if (backdrop_delegate_)
178 backdrop_delegate_->OnWindowStackingChanged(window);
181 void WorkspaceLayoutManager::OnWindowDestroying(aura::Window* window) {
182 if (root_window_ == window) {
183 root_window_->RemoveObserver(this);
188 void WorkspaceLayoutManager::OnWindowBoundsChanged(aura::Window* window,
189 const gfx::Rect& old_bounds,
190 const gfx::Rect& new_bounds) {
191 if (root_window_ == window) {
192 const wm::WMEvent wm_event(wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED);
193 AdjustAllWindowsBoundsForWorkAreaChange(&wm_event);
197 //////////////////////////////////////////////////////////////////////////////
198 // WorkspaceLayoutManager,
199 // aura::client::ActivationChangeObserver implementation:
201 void WorkspaceLayoutManager::OnWindowActivated(aura::Window* gained_active,
202 aura::Window* lost_active) {
203 wm::WindowState* window_state = wm::GetWindowState(gained_active);
204 if (window_state && window_state->IsMinimized() &&
205 !gained_active->IsVisible()) {
206 window_state->Unminimize();
207 DCHECK(!window_state->IsMinimized());
209 UpdateFullscreenState();
210 UpdateShelfVisibility();
213 //////////////////////////////////////////////////////////////////////////////
214 // WorkspaceLayoutManager, wm::WindowStateObserver implementation:
216 void WorkspaceLayoutManager::OnPostWindowStateTypeChange(
217 wm::WindowState* window_state,
218 wm::WindowStateType old_type) {
220 // Notify observers that fullscreen state may be changing.
221 if (window_state->IsFullscreen() ||
222 old_type == wm::WINDOW_STATE_TYPE_FULLSCREEN) {
223 UpdateFullscreenState();
226 UpdateShelfVisibility();
227 if (backdrop_delegate_)
228 backdrop_delegate_->OnPostWindowStateTypeChange(window_state, old_type);
231 //////////////////////////////////////////////////////////////////////////////
232 // WorkspaceLayoutManager, private:
234 void WorkspaceLayoutManager::AdjustAllWindowsBoundsForWorkAreaChange(
235 const wm::WMEvent* event) {
236 DCHECK(event->type() == wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED ||
237 event->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED);
239 work_area_in_parent_ = ScreenUtil::ConvertRectFromScreen(
241 Shell::GetScreen()->GetDisplayNearestWindow(window_).work_area());
243 // Don't do any adjustments of the insets while we are in screen locked mode.
244 // This would happen if the launcher was auto hidden before the login screen
245 // was shown and then gets shown when the login screen gets presented.
246 if (event->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED &&
247 Shell::GetInstance()->session_state_delegate()->IsScreenLocked())
250 // If a user plugs an external display into a laptop running Aura the
251 // display size will change. Maximized windows need to resize to match.
252 // We also do this when developers running Aura on a desktop manually resize
254 // We also need to do this when the work area insets changes.
255 for (WindowSet::const_iterator it = windows_.begin();
256 it != windows_.end();
258 wm::GetWindowState(*it)->OnWMEvent(event);
262 void WorkspaceLayoutManager::UpdateShelfVisibility() {
264 shelf_->UpdateVisibilityState();
267 void WorkspaceLayoutManager::UpdateFullscreenState() {
268 // TODO(flackr): The fullscreen state is currently tracked per workspace
269 // but the shell notification implies a per root window state. Currently
270 // only windows in the default workspace container will go fullscreen but
271 // this should really be tracked by the RootWindowController since
272 // technically any container could get a fullscreen window.
275 bool is_fullscreen = GetRootWindowController(
276 window_->GetRootWindow())->GetWindowForFullscreenMode() != NULL;
277 if (is_fullscreen != is_fullscreen_) {
278 ash::Shell::GetInstance()->NotifyFullscreenStateChange(
279 is_fullscreen, window_->GetRootWindow());
280 is_fullscreen_ = is_fullscreen;
284 } // namespace internal