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/root_window_controller.h"
10 #include "ash/screen_util.h"
11 #include "ash/session/session_state_delegate.h"
12 #include "ash/shelf/shelf_layout_manager.h"
13 #include "ash/shell.h"
14 #include "ash/wm/always_on_top_controller.h"
15 #include "ash/wm/window_animations.h"
16 #include "ash/wm/window_positioner.h"
17 #include "ash/wm/window_properties.h"
18 #include "ash/wm/window_state.h"
19 #include "ash/wm/window_util.h"
20 #include "ash/wm/wm_event.h"
21 #include "ash/wm/workspace/workspace_layout_manager_delegate.h"
22 #include "ui/aura/client/aura_constants.h"
23 #include "ui/aura/window.h"
24 #include "ui/aura/window_observer.h"
25 #include "ui/base/ui_base_types.h"
26 #include "ui/compositor/layer.h"
27 #include "ui/events/event.h"
28 #include "ui/gfx/screen.h"
29 #include "ui/keyboard/keyboard_controller_observer.h"
30 #include "ui/wm/core/window_util.h"
31 #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);
49 DCHECK(window
->GetProperty(kSnapChildrenToPixelBoundary
));
52 WorkspaceLayoutManager::~WorkspaceLayoutManager() {
54 root_window_
->RemoveObserver(this);
55 for (WindowSet::const_iterator i
= windows_
.begin(); i
!= windows_
.end(); ++i
)
56 (*i
)->RemoveObserver(this);
57 Shell::GetInstance()->RemoveShellObserver(this);
58 Shell::GetInstance()->activation_client()->RemoveObserver(this);
61 void WorkspaceLayoutManager::SetShelf(ShelfLayoutManager
* shelf
) {
65 void WorkspaceLayoutManager::SetMaximizeBackdropDelegate(
66 scoped_ptr
<WorkspaceLayoutManagerDelegate
> delegate
) {
67 backdrop_delegate_
.reset(delegate
.release());
70 //////////////////////////////////////////////////////////////////////////////
71 // WorkspaceLayoutManager, aura::LayoutManager implementation:
73 void WorkspaceLayoutManager::OnWindowAddedToLayout(Window
* child
) {
74 wm::WindowState
* window_state
= wm::GetWindowState(child
);
75 wm::WMEvent
event(wm::WM_EVENT_ADDED_TO_WORKSPACE
);
76 window_state
->OnWMEvent(&event
);
77 windows_
.insert(child
);
78 child
->AddObserver(this);
79 window_state
->AddObserver(this);
80 UpdateShelfVisibility();
81 UpdateFullscreenState();
82 if (backdrop_delegate_
)
83 backdrop_delegate_
->OnWindowAddedToLayout(child
);
84 WindowPositioner::RearrangeVisibleWindowOnShow(child
);
87 void WorkspaceLayoutManager::OnWillRemoveWindowFromLayout(Window
* child
) {
88 windows_
.erase(child
);
89 child
->RemoveObserver(this);
90 wm::GetWindowState(child
)->RemoveObserver(this);
92 if (child
->TargetVisibility())
93 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child
);
96 void WorkspaceLayoutManager::OnWindowRemovedFromLayout(Window
* child
) {
97 UpdateShelfVisibility();
98 UpdateFullscreenState();
99 if (backdrop_delegate_
)
100 backdrop_delegate_
->OnWindowRemovedFromLayout(child
);
103 void WorkspaceLayoutManager::OnChildWindowVisibilityChanged(Window
* child
,
105 wm::WindowState
* window_state
= wm::GetWindowState(child
);
106 // Attempting to show a minimized window. Unminimize it.
107 if (visible
&& window_state
->IsMinimized())
108 window_state
->Unminimize();
110 if (child
->TargetVisibility())
111 WindowPositioner::RearrangeVisibleWindowOnShow(child
);
113 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child
);
114 UpdateFullscreenState();
115 UpdateShelfVisibility();
116 if (backdrop_delegate_
)
117 backdrop_delegate_
->OnChildWindowVisibilityChanged(child
, visible
);
120 void WorkspaceLayoutManager::SetChildBounds(
122 const gfx::Rect
& requested_bounds
) {
123 wm::WindowState
* window_state
= wm::GetWindowState(child
);
124 wm::SetBoundsEvent
event(wm::WM_EVENT_SET_BOUNDS
, requested_bounds
);
125 window_state
->OnWMEvent(&event
);
126 UpdateShelfVisibility();
129 //////////////////////////////////////////////////////////////////////////////
130 // WorkspaceLayoutManager, keyboard::KeyboardControllerObserver implementation:
132 void WorkspaceLayoutManager::OnKeyboardBoundsChanging(
133 const gfx::Rect
& new_bounds
) {
134 aura::Window
* window
= wm::GetActiveWindow()->GetToplevelWindow();
135 if (!window
|| !window_
->Contains(window
))
137 wm::WindowState
* window_state
= wm::GetWindowState(window
);
138 if (!new_bounds
.IsEmpty()) {
139 // Store existing bounds to be restored before resizing for keyboard if it
140 // is not already stored.
141 if (!window_state
->HasRestoreBounds())
142 window_state
->SaveCurrentBoundsForRestore();
144 gfx::Rect window_bounds
= ScreenUtil::ConvertRectToScreen(
146 window
->GetTargetBounds());
147 int vertical_displacement
=
148 std::max(0, window_bounds
.bottom() - new_bounds
.y());
149 int shift
= std::min(vertical_displacement
,
150 window_bounds
.y() - work_area_in_parent_
.y());
152 gfx::Point
origin(window_bounds
.x(), window_bounds
.y() - shift
);
153 SetChildBounds(window
, gfx::Rect(origin
, window_bounds
.size()));
155 } else if (window_state
->HasRestoreBounds()) {
156 // Keyboard hidden, restore original bounds if they exist. If the user has
157 // resized or dragged the window in the meantime, WorkspaceWindowResizer
158 // will have cleared the restore bounds and this code will not accidentally
159 // override user intent.
160 window_state
->SetAndClearRestoreBounds();
164 //////////////////////////////////////////////////////////////////////////////
165 // WorkspaceLayoutManager, ash::ShellObserver implementation:
167 void WorkspaceLayoutManager::OnDisplayWorkAreaInsetsChanged() {
168 const gfx::Rect
work_area(ScreenUtil::ConvertRectFromScreen(
170 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area()));
171 if (work_area
!= work_area_in_parent_
) {
172 const wm::WMEvent
event(wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
);
173 AdjustAllWindowsBoundsForWorkAreaChange(&event
);
175 if (backdrop_delegate_
)
176 backdrop_delegate_
->OnDisplayWorkAreaInsetsChanged();
179 void WorkspaceLayoutManager::OnFullscreenStateChanged(
181 aura::Window
* root_window
) {
182 if (window_
->GetRootWindow() != root_window
||
183 is_fullscreen_
== is_fullscreen
) {
186 is_fullscreen_
= is_fullscreen
;
187 Window
* fullscreen_window
=
189 ? GetRootWindowController(window_
->GetRootWindow())
190 ->GetWindowForFullscreenMode()
192 // Changing always on top state may change window's parent. Iterate on a copy
193 // of |windows_| to avoid invalidating an iterator. Since both workspace and
194 // always_on_top containers' layouts are managed by this class all the
195 // appropriate windows will be included in the iteration.
196 WindowSet
windows(windows_
);
197 for (auto window
: windows
) {
198 wm::WindowState
* window_state
= wm::GetWindowState(window
);
200 window_state
->DisableAlwaysOnTop(fullscreen_window
);
202 window_state
->RestoreAlwaysOnTop();
206 //////////////////////////////////////////////////////////////////////////////
207 // WorkspaceLayoutManager, aura::WindowObserver implementation:
209 void WorkspaceLayoutManager::OnWindowHierarchyChanged(
210 const WindowObserver::HierarchyChangeParams
& params
) {
211 if (!wm::GetWindowState(params
.target
)->IsActive())
213 // If the window is already tracked by the workspace this update would be
214 // redundant as the fullscreen and shelf state would have been handled in
215 // OnWindowAddedToLayout.
216 if (windows_
.find(params
.target
) != windows_
.end())
219 // If the active window has moved to this root window then update the
221 // TODO(flackr): Track the active window leaving this root window and update
222 // the fullscreen state accordingly.
223 if (params
.new_parent
&& params
.new_parent
->GetRootWindow() == root_window_
) {
224 UpdateFullscreenState();
225 UpdateShelfVisibility();
229 void WorkspaceLayoutManager::OnWindowPropertyChanged(Window
* window
,
232 if (key
== aura::client::kAlwaysOnTopKey
&&
233 window
->GetProperty(aura::client::kAlwaysOnTopKey
)) {
234 GetRootWindowController(window
->GetRootWindow())->
235 always_on_top_controller()->GetContainer(window
)->AddChild(window
);
239 void WorkspaceLayoutManager::OnWindowStackingChanged(aura::Window
* window
) {
240 UpdateShelfVisibility();
241 UpdateFullscreenState();
242 if (backdrop_delegate_
)
243 backdrop_delegate_
->OnWindowStackingChanged(window
);
246 void WorkspaceLayoutManager::OnWindowDestroying(aura::Window
* window
) {
247 if (root_window_
== window
) {
248 root_window_
->RemoveObserver(this);
253 void WorkspaceLayoutManager::OnWindowBoundsChanged(aura::Window
* window
,
254 const gfx::Rect
& old_bounds
,
255 const gfx::Rect
& new_bounds
) {
256 if (root_window_
== window
) {
257 const wm::WMEvent
wm_event(wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED
);
258 AdjustAllWindowsBoundsForWorkAreaChange(&wm_event
);
262 //////////////////////////////////////////////////////////////////////////////
263 // WorkspaceLayoutManager,
264 // aura::client::ActivationChangeObserver implementation:
266 void WorkspaceLayoutManager::OnWindowActivated(
267 aura::client::ActivationChangeObserver::ActivationReason reason
,
268 aura::Window
* gained_active
,
269 aura::Window
* lost_active
) {
270 wm::WindowState
* window_state
= wm::GetWindowState(gained_active
);
271 if (window_state
&& window_state
->IsMinimized() &&
272 !gained_active
->IsVisible()) {
273 window_state
->Unminimize();
274 DCHECK(!window_state
->IsMinimized());
276 UpdateFullscreenState();
277 UpdateShelfVisibility();
280 //////////////////////////////////////////////////////////////////////////////
281 // WorkspaceLayoutManager, wm::WindowStateObserver implementation:
283 void WorkspaceLayoutManager::OnPostWindowStateTypeChange(
284 wm::WindowState
* window_state
,
285 wm::WindowStateType old_type
) {
287 // Notify observers that fullscreen state may be changing.
288 if (window_state
->IsFullscreen() ||
289 old_type
== wm::WINDOW_STATE_TYPE_FULLSCREEN
) {
290 UpdateFullscreenState();
293 UpdateShelfVisibility();
294 if (backdrop_delegate_
)
295 backdrop_delegate_
->OnPostWindowStateTypeChange(window_state
, old_type
);
298 //////////////////////////////////////////////////////////////////////////////
299 // WorkspaceLayoutManager, private:
301 void WorkspaceLayoutManager::AdjustAllWindowsBoundsForWorkAreaChange(
302 const wm::WMEvent
* event
) {
303 DCHECK(event
->type() == wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED
||
304 event
->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
);
306 work_area_in_parent_
= ScreenUtil::ConvertRectFromScreen(
308 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area());
310 // Don't do any adjustments of the insets while we are in screen locked mode.
311 // This would happen if the launcher was auto hidden before the login screen
312 // was shown and then gets shown when the login screen gets presented.
313 if (event
->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
&&
314 Shell::GetInstance()->session_state_delegate()->IsScreenLocked())
317 // If a user plugs an external display into a laptop running Aura the
318 // display size will change. Maximized windows need to resize to match.
319 // We also do this when developers running Aura on a desktop manually resize
321 // We also need to do this when the work area insets changes.
322 for (WindowSet::const_iterator it
= windows_
.begin();
323 it
!= windows_
.end();
325 wm::GetWindowState(*it
)->OnWMEvent(event
);
329 void WorkspaceLayoutManager::UpdateShelfVisibility() {
331 shelf_
->UpdateVisibilityState();
334 void WorkspaceLayoutManager::UpdateFullscreenState() {
335 // TODO(flackr): The fullscreen state is currently tracked per workspace
336 // but the shell notification implies a per root window state. Currently
337 // only windows in the default workspace container will go fullscreen but
338 // this should really be tracked by the RootWindowController since
339 // technically any container could get a fullscreen window.
342 bool is_fullscreen
= GetRootWindowController(
343 window_
->GetRootWindow())->GetWindowForFullscreenMode() != NULL
;
344 if (is_fullscreen
!= is_fullscreen_
) {
345 ash::Shell::GetInstance()->NotifyFullscreenStateChange(
346 is_fullscreen
, window_
->GetRootWindow());
347 is_fullscreen_
= is_fullscreen
;