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/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"
35 WorkspaceLayoutManager::WorkspaceLayoutManager(aura::Window
* window
)
38 root_window_(window
->GetRootWindow()),
39 work_area_in_parent_(ScreenUtil::ConvertRectFromScreen(
41 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area())),
42 is_fullscreen_(GetRootWindowController(
43 window
->GetRootWindow())->GetWindowForFullscreenMode() != NULL
) {
44 Shell::GetInstance()->activation_client()->AddObserver(this);
45 Shell::GetInstance()->AddShellObserver(this);
46 root_window_
->AddObserver(this);
49 WorkspaceLayoutManager::~WorkspaceLayoutManager() {
51 root_window_
->RemoveObserver(this);
52 for (WindowSet::const_iterator i
= windows_
.begin(); i
!= windows_
.end(); ++i
)
53 (*i
)->RemoveObserver(this);
54 Shell::GetInstance()->RemoveShellObserver(this);
55 Shell::GetInstance()->activation_client()->RemoveObserver(this);
58 void WorkspaceLayoutManager::SetShelf(ShelfLayoutManager
* shelf
) {
62 void WorkspaceLayoutManager::SetMaximizeBackdropDelegate(
63 scoped_ptr
<WorkspaceLayoutManagerDelegate
> delegate
) {
64 backdrop_delegate_
.reset(delegate
.release());
67 //////////////////////////////////////////////////////////////////////////////
68 // WorkspaceLayoutManager, aura::LayoutManager implementation:
70 void WorkspaceLayoutManager::OnWindowAddedToLayout(Window
* child
) {
71 wm::WindowState
* window_state
= wm::GetWindowState(child
);
72 wm::WMEvent
event(wm::WM_EVENT_ADDED_TO_WORKSPACE
);
73 window_state
->OnWMEvent(&event
);
74 windows_
.insert(child
);
75 child
->AddObserver(this);
76 window_state
->AddObserver(this);
77 UpdateShelfVisibility();
78 UpdateFullscreenState();
79 if (backdrop_delegate_
)
80 backdrop_delegate_
->OnWindowAddedToLayout(child
);
81 WindowPositioner::RearrangeVisibleWindowOnShow(child
);
84 void WorkspaceLayoutManager::OnWillRemoveWindowFromLayout(Window
* child
) {
85 windows_
.erase(child
);
86 child
->RemoveObserver(this);
87 wm::GetWindowState(child
)->RemoveObserver(this);
89 if (child
->TargetVisibility())
90 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child
);
93 void WorkspaceLayoutManager::OnWindowRemovedFromLayout(Window
* child
) {
94 UpdateShelfVisibility();
95 UpdateFullscreenState();
96 if (backdrop_delegate_
)
97 backdrop_delegate_
->OnWindowRemovedFromLayout(child
);
100 void WorkspaceLayoutManager::OnChildWindowVisibilityChanged(Window
* child
,
102 wm::WindowState
* window_state
= wm::GetWindowState(child
);
103 // Attempting to show a minimized window. Unminimize it.
104 if (visible
&& window_state
->IsMinimized())
105 window_state
->Unminimize();
107 if (child
->TargetVisibility())
108 WindowPositioner::RearrangeVisibleWindowOnShow(child
);
110 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child
);
111 UpdateFullscreenState();
112 UpdateShelfVisibility();
113 if (backdrop_delegate_
)
114 backdrop_delegate_
->OnChildWindowVisibilityChanged(child
, visible
);
117 void WorkspaceLayoutManager::SetChildBounds(
119 const gfx::Rect
& requested_bounds
) {
120 wm::WindowState
* window_state
= wm::GetWindowState(child
);
121 wm::SetBoundsEvent
event(wm::WM_EVENT_SET_BOUNDS
, requested_bounds
);
122 window_state
->OnWMEvent(&event
);
123 UpdateShelfVisibility();
126 //////////////////////////////////////////////////////////////////////////////
127 // WorkspaceLayoutManager, ash::ShellObserver implementation:
129 void WorkspaceLayoutManager::OnDisplayWorkAreaInsetsChanged() {
130 const gfx::Rect
work_area(ScreenUtil::ConvertRectFromScreen(
132 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area()));
133 if (work_area
!= work_area_in_parent_
) {
134 const wm::WMEvent
event(wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
);
135 AdjustAllWindowsBoundsForWorkAreaChange(&event
);
139 //////////////////////////////////////////////////////////////////////////////
140 // WorkspaceLayoutManager, aura::WindowObserver implementation:
142 void WorkspaceLayoutManager::OnWindowHierarchyChanged(
143 const WindowObserver::HierarchyChangeParams
& params
) {
144 if (!wm::GetWindowState(params
.target
)->IsActive())
146 // If the window is already tracked by the workspace this update would be
147 // redundant as the fullscreen and shelf state would have been handled in
148 // OnWindowAddedToLayout.
149 if (windows_
.find(params
.target
) != windows_
.end())
152 // If the active window has moved to this root window then update the
154 // TODO(flackr): Track the active window leaving this root window and update
155 // the fullscreen state accordingly.
156 if (params
.new_parent
&& params
.new_parent
->GetRootWindow() == root_window_
) {
157 UpdateFullscreenState();
158 UpdateShelfVisibility();
162 void WorkspaceLayoutManager::OnWindowPropertyChanged(Window
* window
,
165 if (key
== aura::client::kAlwaysOnTopKey
&&
166 window
->GetProperty(aura::client::kAlwaysOnTopKey
)) {
167 GetRootWindowController(window
->GetRootWindow())->
168 always_on_top_controller()->GetContainer(window
)->AddChild(window
);
172 void WorkspaceLayoutManager::OnWindowStackingChanged(aura::Window
* window
) {
173 UpdateShelfVisibility();
174 UpdateFullscreenState();
175 if (backdrop_delegate_
)
176 backdrop_delegate_
->OnWindowStackingChanged(window
);
179 void WorkspaceLayoutManager::OnWindowDestroying(aura::Window
* window
) {
180 if (root_window_
== window
) {
181 root_window_
->RemoveObserver(this);
186 void WorkspaceLayoutManager::OnWindowBoundsChanged(aura::Window
* window
,
187 const gfx::Rect
& old_bounds
,
188 const gfx::Rect
& new_bounds
) {
189 if (root_window_
== window
) {
190 const wm::WMEvent
wm_event(wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED
);
191 AdjustAllWindowsBoundsForWorkAreaChange(&wm_event
);
195 //////////////////////////////////////////////////////////////////////////////
196 // WorkspaceLayoutManager,
197 // aura::client::ActivationChangeObserver implementation:
199 void WorkspaceLayoutManager::OnWindowActivated(aura::Window
* gained_active
,
200 aura::Window
* lost_active
) {
201 wm::WindowState
* window_state
= wm::GetWindowState(gained_active
);
202 if (window_state
&& window_state
->IsMinimized() &&
203 !gained_active
->IsVisible()) {
204 window_state
->Unminimize();
205 DCHECK(!window_state
->IsMinimized());
207 UpdateFullscreenState();
208 UpdateShelfVisibility();
211 //////////////////////////////////////////////////////////////////////////////
212 // WorkspaceLayoutManager, wm::WindowStateObserver implementation:
214 void WorkspaceLayoutManager::OnPostWindowStateTypeChange(
215 wm::WindowState
* window_state
,
216 wm::WindowStateType old_type
) {
218 // Notify observers that fullscreen state may be changing.
219 if (window_state
->IsFullscreen() ||
220 old_type
== wm::WINDOW_STATE_TYPE_FULLSCREEN
) {
221 UpdateFullscreenState();
224 UpdateShelfVisibility();
225 if (backdrop_delegate_
)
226 backdrop_delegate_
->OnPostWindowStateTypeChange(window_state
, old_type
);
229 //////////////////////////////////////////////////////////////////////////////
230 // WorkspaceLayoutManager, private:
232 void WorkspaceLayoutManager::AdjustAllWindowsBoundsForWorkAreaChange(
233 const wm::WMEvent
* event
) {
234 DCHECK(event
->type() == wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED
||
235 event
->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
);
237 work_area_in_parent_
= ScreenUtil::ConvertRectFromScreen(
239 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area());
241 // Don't do any adjustments of the insets while we are in screen locked mode.
242 // This would happen if the launcher was auto hidden before the login screen
243 // was shown and then gets shown when the login screen gets presented.
244 if (event
->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
&&
245 Shell::GetInstance()->session_state_delegate()->IsScreenLocked())
248 // If a user plugs an external display into a laptop running Aura the
249 // display size will change. Maximized windows need to resize to match.
250 // We also do this when developers running Aura on a desktop manually resize
252 // We also need to do this when the work area insets changes.
253 for (WindowSet::const_iterator it
= windows_
.begin();
254 it
!= windows_
.end();
256 wm::GetWindowState(*it
)->OnWMEvent(event
);
260 void WorkspaceLayoutManager::UpdateShelfVisibility() {
262 shelf_
->UpdateVisibilityState();
265 void WorkspaceLayoutManager::UpdateFullscreenState() {
266 // TODO(flackr): The fullscreen state is currently tracked per workspace
267 // but the shell notification implies a per root window state. Currently
268 // only windows in the default workspace container will go fullscreen but
269 // this should really be tracked by the RootWindowController since
270 // technically any container could get a fullscreen window.
273 bool is_fullscreen
= GetRootWindowController(
274 window_
->GetRootWindow())->GetWindowForFullscreenMode() != NULL
;
275 if (is_fullscreen
!= is_fullscreen_
) {
276 ash::Shell::GetInstance()->NotifyFullscreenStateChange(
277 is_fullscreen
, window_
->GetRootWindow());
278 is_fullscreen_
= is_fullscreen
;