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"
9 #include "ash/display/display_controller.h"
10 #include "ash/root_window_controller.h"
11 #include "ash/screen_util.h"
12 #include "ash/session/session_state_delegate.h"
13 #include "ash/shelf/shelf_layout_manager.h"
14 #include "ash/shell.h"
15 #include "ash/wm/always_on_top_controller.h"
16 #include "ash/wm/window_animations.h"
17 #include "ash/wm/window_positioner.h"
18 #include "ash/wm/window_properties.h"
19 #include "ash/wm/window_state.h"
20 #include "ash/wm/window_util.h"
21 #include "ash/wm/wm_event.h"
22 #include "ash/wm/workspace/workspace_layout_manager_delegate.h"
23 #include "ui/aura/client/aura_constants.h"
24 #include "ui/aura/window.h"
25 #include "ui/aura/window_observer.h"
26 #include "ui/base/ui_base_types.h"
27 #include "ui/compositor/layer.h"
28 #include "ui/events/event.h"
29 #include "ui/gfx/screen.h"
30 #include "ui/keyboard/keyboard_controller_observer.h"
31 #include "ui/wm/core/window_util.h"
32 #include "ui/wm/public/activation_client.h"
38 WorkspaceLayoutManager::WorkspaceLayoutManager(aura::Window
* window
)
41 root_window_(window
->GetRootWindow()),
42 work_area_in_parent_(ScreenUtil::ConvertRectFromScreen(
44 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area())),
45 is_fullscreen_(GetRootWindowController(
46 window
->GetRootWindow())->GetWindowForFullscreenMode() != NULL
) {
47 Shell::GetInstance()->activation_client()->AddObserver(this);
48 Shell::GetInstance()->AddShellObserver(this);
49 root_window_
->AddObserver(this);
50 DCHECK(window
->GetProperty(kSnapChildrenToPixelBoundary
));
53 WorkspaceLayoutManager::~WorkspaceLayoutManager() {
55 root_window_
->RemoveObserver(this);
56 for (WindowSet::const_iterator i
= windows_
.begin(); i
!= windows_
.end(); ++i
)
57 (*i
)->RemoveObserver(this);
58 Shell::GetInstance()->RemoveShellObserver(this);
59 Shell::GetInstance()->activation_client()->RemoveObserver(this);
62 void WorkspaceLayoutManager::SetShelf(ShelfLayoutManager
* shelf
) {
66 void WorkspaceLayoutManager::SetMaximizeBackdropDelegate(
67 scoped_ptr
<WorkspaceLayoutManagerDelegate
> delegate
) {
68 backdrop_delegate_
.reset(delegate
.release());
71 //////////////////////////////////////////////////////////////////////////////
72 // WorkspaceLayoutManager, aura::LayoutManager implementation:
74 void WorkspaceLayoutManager::OnWindowAddedToLayout(Window
* child
) {
75 wm::WindowState
* window_state
= wm::GetWindowState(child
);
76 wm::WMEvent
event(wm::WM_EVENT_ADDED_TO_WORKSPACE
);
77 window_state
->OnWMEvent(&event
);
78 windows_
.insert(child
);
79 child
->AddObserver(this);
80 window_state
->AddObserver(this);
81 UpdateShelfVisibility();
82 UpdateFullscreenState();
83 if (backdrop_delegate_
)
84 backdrop_delegate_
->OnWindowAddedToLayout(child
);
85 WindowPositioner::RearrangeVisibleWindowOnShow(child
);
88 void WorkspaceLayoutManager::OnWillRemoveWindowFromLayout(Window
* child
) {
89 windows_
.erase(child
);
90 child
->RemoveObserver(this);
91 wm::GetWindowState(child
)->RemoveObserver(this);
93 if (child
->TargetVisibility())
94 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child
);
97 void WorkspaceLayoutManager::OnWindowRemovedFromLayout(Window
* child
) {
98 UpdateShelfVisibility();
99 UpdateFullscreenState();
100 if (backdrop_delegate_
)
101 backdrop_delegate_
->OnWindowRemovedFromLayout(child
);
104 void WorkspaceLayoutManager::OnChildWindowVisibilityChanged(Window
* child
,
106 wm::WindowState
* window_state
= wm::GetWindowState(child
);
107 // Attempting to show a minimized window. Unminimize it.
108 if (visible
&& window_state
->IsMinimized())
109 window_state
->Unminimize();
111 if (child
->TargetVisibility())
112 WindowPositioner::RearrangeVisibleWindowOnShow(child
);
114 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child
);
115 UpdateFullscreenState();
116 UpdateShelfVisibility();
117 if (backdrop_delegate_
)
118 backdrop_delegate_
->OnChildWindowVisibilityChanged(child
, visible
);
121 void WorkspaceLayoutManager::SetChildBounds(
123 const gfx::Rect
& requested_bounds
) {
124 wm::WindowState
* window_state
= wm::GetWindowState(child
);
125 wm::SetBoundsEvent
event(wm::WM_EVENT_SET_BOUNDS
, requested_bounds
);
126 window_state
->OnWMEvent(&event
);
127 UpdateShelfVisibility();
130 //////////////////////////////////////////////////////////////////////////////
131 // WorkspaceLayoutManager, keyboard::KeyboardControllerObserver implementation:
133 void WorkspaceLayoutManager::OnKeyboardBoundsChanging(
134 const gfx::Rect
& new_bounds
) {
135 aura::Window
* window
= wm::GetActiveWindow()->GetToplevelWindow();
136 if (!window
|| !window_
->Contains(window
))
138 wm::WindowState
* window_state
= wm::GetWindowState(window
);
139 if (!new_bounds
.IsEmpty()) {
140 // Store existing bounds to be restored before resizing for keyboard if it
141 // is not already stored.
142 if (!window_state
->HasRestoreBounds())
143 window_state
->SaveCurrentBoundsForRestore();
145 gfx::Rect window_bounds
= ScreenUtil::ConvertRectToScreen(
147 window
->GetTargetBounds());
148 int vertical_displacement
=
149 std::max(0, window_bounds
.bottom() - new_bounds
.y());
150 int shift
= std::min(vertical_displacement
,
151 window_bounds
.y() - work_area_in_parent_
.y());
153 gfx::Point
origin(window_bounds
.x(), window_bounds
.y() - shift
);
154 SetChildBounds(window
, gfx::Rect(origin
, window_bounds
.size()));
156 } else if (window_state
->HasRestoreBounds()) {
157 // Keyboard hidden, restore original bounds if they exist. If the user has
158 // resized or dragged the window in the meantime, WorkspaceWindowResizer
159 // will have cleared the restore bounds and this code will not accidentally
160 // override user intent.
161 window_state
->SetAndClearRestoreBounds();
165 //////////////////////////////////////////////////////////////////////////////
166 // WorkspaceLayoutManager, ash::ShellObserver implementation:
168 void WorkspaceLayoutManager::OnDisplayWorkAreaInsetsChanged() {
169 const gfx::Rect
work_area(ScreenUtil::ConvertRectFromScreen(
171 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area()));
172 if (work_area
!= work_area_in_parent_
) {
173 const wm::WMEvent
event(wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
);
174 AdjustAllWindowsBoundsForWorkAreaChange(&event
);
176 if (backdrop_delegate_
)
177 backdrop_delegate_
->OnDisplayWorkAreaInsetsChanged();
180 void WorkspaceLayoutManager::OnFullscreenStateChanged(
182 aura::Window
* root_window
) {
183 if (window_
->GetRootWindow() != root_window
||
184 is_fullscreen_
== is_fullscreen
) {
187 is_fullscreen_
= is_fullscreen
;
188 Window
* fullscreen_window
=
190 ? GetRootWindowController(window_
->GetRootWindow())
191 ->GetWindowForFullscreenMode()
193 // Changing always on top state may change window's parent. Iterate on a copy
194 // of |windows_| to avoid invalidating an iterator. Since both workspace and
195 // always_on_top containers' layouts are managed by this class all the
196 // appropriate windows will be included in the iteration.
197 WindowSet
windows(windows_
);
198 for (auto window
: windows
) {
199 wm::WindowState
* window_state
= wm::GetWindowState(window
);
201 window_state
->DisableAlwaysOnTop(fullscreen_window
);
203 window_state
->RestoreAlwaysOnTop();
207 //////////////////////////////////////////////////////////////////////////////
208 // WorkspaceLayoutManager, aura::WindowObserver implementation:
210 void WorkspaceLayoutManager::OnWindowHierarchyChanged(
211 const WindowObserver::HierarchyChangeParams
& params
) {
212 if (!wm::GetWindowState(params
.target
)->IsActive())
214 // If the window is already tracked by the workspace this update would be
215 // redundant as the fullscreen and shelf state would have been handled in
216 // OnWindowAddedToLayout.
217 if (windows_
.find(params
.target
) != windows_
.end())
220 // If the active window has moved to this root window then update the
222 // TODO(flackr): Track the active window leaving this root window and update
223 // the fullscreen state accordingly.
224 if (params
.new_parent
&& params
.new_parent
->GetRootWindow() == root_window_
) {
225 UpdateFullscreenState();
226 UpdateShelfVisibility();
230 void WorkspaceLayoutManager::OnWindowPropertyChanged(Window
* window
,
233 if (key
== aura::client::kAlwaysOnTopKey
&&
234 window
->GetProperty(aura::client::kAlwaysOnTopKey
)) {
235 GetRootWindowController(window
->GetRootWindow())->
236 always_on_top_controller()->GetContainer(window
)->AddChild(window
);
240 void WorkspaceLayoutManager::OnWindowStackingChanged(aura::Window
* window
) {
241 UpdateShelfVisibility();
242 UpdateFullscreenState();
243 if (backdrop_delegate_
)
244 backdrop_delegate_
->OnWindowStackingChanged(window
);
247 void WorkspaceLayoutManager::OnWindowDestroying(aura::Window
* window
) {
248 if (root_window_
== window
) {
249 root_window_
->RemoveObserver(this);
254 void WorkspaceLayoutManager::OnWindowBoundsChanged(aura::Window
* window
,
255 const gfx::Rect
& old_bounds
,
256 const gfx::Rect
& new_bounds
) {
257 if (root_window_
== window
) {
258 const wm::WMEvent
wm_event(wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED
);
259 AdjustAllWindowsBoundsForWorkAreaChange(&wm_event
);
263 //////////////////////////////////////////////////////////////////////////////
264 // WorkspaceLayoutManager,
265 // aura::client::ActivationChangeObserver implementation:
267 void WorkspaceLayoutManager::OnWindowActivated(
268 aura::client::ActivationChangeObserver::ActivationReason reason
,
269 aura::Window
* gained_active
,
270 aura::Window
* lost_active
) {
271 wm::WindowState
* window_state
= wm::GetWindowState(gained_active
);
272 if (window_state
&& window_state
->IsMinimized() &&
273 !gained_active
->IsVisible()) {
274 window_state
->Unminimize();
275 DCHECK(!window_state
->IsMinimized());
277 UpdateFullscreenState();
278 UpdateShelfVisibility();
281 //////////////////////////////////////////////////////////////////////////////
282 // WorkspaceLayoutManager, wm::WindowStateObserver implementation:
284 void WorkspaceLayoutManager::OnPostWindowStateTypeChange(
285 wm::WindowState
* window_state
,
286 wm::WindowStateType old_type
) {
288 // Notify observers that fullscreen state may be changing.
289 if (window_state
->IsFullscreen() ||
290 old_type
== wm::WINDOW_STATE_TYPE_FULLSCREEN
) {
291 UpdateFullscreenState();
294 UpdateShelfVisibility();
295 if (backdrop_delegate_
)
296 backdrop_delegate_
->OnPostWindowStateTypeChange(window_state
, old_type
);
299 //////////////////////////////////////////////////////////////////////////////
300 // WorkspaceLayoutManager, private:
302 void WorkspaceLayoutManager::AdjustAllWindowsBoundsForWorkAreaChange(
303 const wm::WMEvent
* event
) {
304 DCHECK(event
->type() == wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED
||
305 event
->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
);
307 work_area_in_parent_
= ScreenUtil::ConvertRectFromScreen(
309 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area());
311 // Don't do any adjustments of the insets while we are in screen locked mode.
312 // This would happen if the launcher was auto hidden before the login screen
313 // was shown and then gets shown when the login screen gets presented.
314 if (event
->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
&&
315 Shell::GetInstance()->session_state_delegate()->IsScreenLocked())
318 // If a user plugs an external display into a laptop running Aura the
319 // display size will change. Maximized windows need to resize to match.
320 // We also do this when developers running Aura on a desktop manually resize
322 // We also need to do this when the work area insets changes.
323 for (WindowSet::const_iterator it
= windows_
.begin();
324 it
!= windows_
.end();
326 wm::GetWindowState(*it
)->OnWMEvent(event
);
330 void WorkspaceLayoutManager::UpdateShelfVisibility() {
332 shelf_
->UpdateVisibilityState();
335 void WorkspaceLayoutManager::UpdateFullscreenState() {
336 // TODO(flackr): The fullscreen state is currently tracked per workspace
337 // but the shell notification implies a per root window state. Currently
338 // only windows in the default workspace container will go fullscreen but
339 // this should really be tracked by the RootWindowController since
340 // technically any container could get a fullscreen window.
343 bool is_fullscreen
= GetRootWindowController(
344 window_
->GetRootWindow())->GetWindowForFullscreenMode() != NULL
;
345 if (is_fullscreen
!= is_fullscreen_
) {
346 ash::Shell::GetInstance()->NotifyFullscreenStateChange(
347 is_fullscreen
, window_
->GetRootWindow());
348 is_fullscreen_
= is_fullscreen
;