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/ime/input_method.h"
27 #include "ui/base/ime/text_input_client.h"
28 #include "ui/base/ui_base_types.h"
29 #include "ui/compositor/layer.h"
30 #include "ui/events/event.h"
31 #include "ui/gfx/screen.h"
32 #include "ui/keyboard/keyboard_controller_observer.h"
33 #include "ui/wm/core/window_util.h"
34 #include "ui/wm/public/activation_client.h"
40 WorkspaceLayoutManager::WorkspaceLayoutManager(aura::Window
* window
)
43 root_window_(window
->GetRootWindow()),
44 work_area_in_parent_(ScreenUtil::ConvertRectFromScreen(
46 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area())),
47 is_fullscreen_(GetRootWindowController(
48 window
->GetRootWindow())->GetWindowForFullscreenMode() != NULL
) {
49 Shell::GetInstance()->activation_client()->AddObserver(this);
50 Shell::GetInstance()->AddShellObserver(this);
51 root_window_
->AddObserver(this);
52 DCHECK(window
->GetProperty(kSnapChildrenToPixelBoundary
));
55 WorkspaceLayoutManager::~WorkspaceLayoutManager() {
57 root_window_
->RemoveObserver(this);
58 for (WindowSet::const_iterator i
= windows_
.begin(); i
!= windows_
.end(); ++i
)
59 (*i
)->RemoveObserver(this);
60 Shell::GetInstance()->RemoveShellObserver(this);
61 Shell::GetInstance()->activation_client()->RemoveObserver(this);
64 void WorkspaceLayoutManager::SetShelf(ShelfLayoutManager
* shelf
) {
68 void WorkspaceLayoutManager::SetMaximizeBackdropDelegate(
69 scoped_ptr
<WorkspaceLayoutManagerDelegate
> delegate
) {
70 backdrop_delegate_
.reset(delegate
.release());
73 //////////////////////////////////////////////////////////////////////////////
74 // WorkspaceLayoutManager, aura::LayoutManager implementation:
76 void WorkspaceLayoutManager::OnWindowAddedToLayout(Window
* child
) {
77 wm::WindowState
* window_state
= wm::GetWindowState(child
);
78 wm::WMEvent
event(wm::WM_EVENT_ADDED_TO_WORKSPACE
);
79 window_state
->OnWMEvent(&event
);
80 windows_
.insert(child
);
81 child
->AddObserver(this);
82 window_state
->AddObserver(this);
83 UpdateShelfVisibility();
84 UpdateFullscreenState();
85 if (backdrop_delegate_
)
86 backdrop_delegate_
->OnWindowAddedToLayout(child
);
87 WindowPositioner::RearrangeVisibleWindowOnShow(child
);
90 void WorkspaceLayoutManager::OnWillRemoveWindowFromLayout(Window
* child
) {
91 windows_
.erase(child
);
92 child
->RemoveObserver(this);
93 wm::GetWindowState(child
)->RemoveObserver(this);
95 if (child
->TargetVisibility())
96 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child
);
99 void WorkspaceLayoutManager::OnWindowRemovedFromLayout(Window
* child
) {
100 UpdateShelfVisibility();
101 UpdateFullscreenState();
102 if (backdrop_delegate_
)
103 backdrop_delegate_
->OnWindowRemovedFromLayout(child
);
106 void WorkspaceLayoutManager::OnChildWindowVisibilityChanged(Window
* child
,
108 wm::WindowState
* window_state
= wm::GetWindowState(child
);
109 // Attempting to show a minimized window. Unminimize it.
110 if (visible
&& window_state
->IsMinimized())
111 window_state
->Unminimize();
113 if (child
->TargetVisibility())
114 WindowPositioner::RearrangeVisibleWindowOnShow(child
);
116 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child
);
117 UpdateFullscreenState();
118 UpdateShelfVisibility();
119 if (backdrop_delegate_
)
120 backdrop_delegate_
->OnChildWindowVisibilityChanged(child
, visible
);
123 void WorkspaceLayoutManager::SetChildBounds(
125 const gfx::Rect
& requested_bounds
) {
126 wm::WindowState
* window_state
= wm::GetWindowState(child
);
127 wm::SetBoundsEvent
event(wm::WM_EVENT_SET_BOUNDS
, requested_bounds
);
128 window_state
->OnWMEvent(&event
);
129 UpdateShelfVisibility();
132 //////////////////////////////////////////////////////////////////////////////
133 // WorkspaceLayoutManager, keyboard::KeyboardControllerObserver implementation:
135 void WorkspaceLayoutManager::OnKeyboardBoundsChanging(
136 const gfx::Rect
& new_bounds
) {
137 ui::InputMethod
* input_method
=
138 root_window_
->GetProperty(aura::client::kRootWindowInputMethodKey
);
139 ui::TextInputClient
* text_input_client
= input_method
->GetTextInputClient();
140 if (!text_input_client
)
142 aura::Window
*window
=
143 text_input_client
->GetAttachedWindow()->GetToplevelWindow();
144 if (!window
|| !window_
->Contains(window
))
146 wm::WindowState
* window_state
= wm::GetWindowState(window
);
147 if (!new_bounds
.IsEmpty()) {
148 // Store existing bounds to be restored before resizing for keyboard if it
149 // is not already stored.
150 if (!window_state
->HasRestoreBounds())
151 window_state
->SaveCurrentBoundsForRestore();
153 gfx::Rect window_bounds
= ScreenUtil::ConvertRectToScreen(
155 window
->GetTargetBounds());
156 int vertical_displacement
=
157 std::max(0, window_bounds
.bottom() - new_bounds
.y());
158 int shift
= std::min(vertical_displacement
,
159 window_bounds
.y() - work_area_in_parent_
.y());
161 gfx::Point
origin(window_bounds
.x(), window_bounds
.y() - shift
);
162 SetChildBounds(window
, gfx::Rect(origin
, window_bounds
.size()));
164 } else if (window_state
->HasRestoreBounds()) {
165 // Keyboard hidden, restore original bounds if they exist. If the user has
166 // resized or dragged the window in the meantime, WorkspaceWindowResizer
167 // will have cleared the restore bounds and this code will not accidentally
168 // override user intent.
169 window_state
->SetAndClearRestoreBounds();
173 //////////////////////////////////////////////////////////////////////////////
174 // WorkspaceLayoutManager, ash::ShellObserver implementation:
176 void WorkspaceLayoutManager::OnDisplayWorkAreaInsetsChanged() {
177 const gfx::Rect
work_area(ScreenUtil::ConvertRectFromScreen(
179 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area()));
180 if (work_area
!= work_area_in_parent_
) {
181 const wm::WMEvent
event(wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
);
182 AdjustAllWindowsBoundsForWorkAreaChange(&event
);
184 if (backdrop_delegate_
)
185 backdrop_delegate_
->OnDisplayWorkAreaInsetsChanged();
188 //////////////////////////////////////////////////////////////////////////////
189 // WorkspaceLayoutManager, aura::WindowObserver implementation:
191 void WorkspaceLayoutManager::OnWindowHierarchyChanged(
192 const WindowObserver::HierarchyChangeParams
& params
) {
193 if (!wm::GetWindowState(params
.target
)->IsActive())
195 // If the window is already tracked by the workspace this update would be
196 // redundant as the fullscreen and shelf state would have been handled in
197 // OnWindowAddedToLayout.
198 if (windows_
.find(params
.target
) != windows_
.end())
201 // If the active window has moved to this root window then update the
203 // TODO(flackr): Track the active window leaving this root window and update
204 // the fullscreen state accordingly.
205 if (params
.new_parent
&& params
.new_parent
->GetRootWindow() == root_window_
) {
206 UpdateFullscreenState();
207 UpdateShelfVisibility();
211 void WorkspaceLayoutManager::OnWindowPropertyChanged(Window
* window
,
214 if (key
== aura::client::kAlwaysOnTopKey
&&
215 window
->GetProperty(aura::client::kAlwaysOnTopKey
)) {
216 GetRootWindowController(window
->GetRootWindow())->
217 always_on_top_controller()->GetContainer(window
)->AddChild(window
);
221 void WorkspaceLayoutManager::OnWindowStackingChanged(aura::Window
* window
) {
222 UpdateShelfVisibility();
223 UpdateFullscreenState();
224 if (backdrop_delegate_
)
225 backdrop_delegate_
->OnWindowStackingChanged(window
);
228 void WorkspaceLayoutManager::OnWindowDestroying(aura::Window
* window
) {
229 if (root_window_
== window
) {
230 root_window_
->RemoveObserver(this);
235 void WorkspaceLayoutManager::OnWindowBoundsChanged(aura::Window
* window
,
236 const gfx::Rect
& old_bounds
,
237 const gfx::Rect
& new_bounds
) {
238 if (root_window_
== window
) {
239 const wm::WMEvent
wm_event(wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED
);
240 AdjustAllWindowsBoundsForWorkAreaChange(&wm_event
);
244 //////////////////////////////////////////////////////////////////////////////
245 // WorkspaceLayoutManager,
246 // aura::client::ActivationChangeObserver implementation:
248 void WorkspaceLayoutManager::OnWindowActivated(aura::Window
* gained_active
,
249 aura::Window
* lost_active
) {
250 wm::WindowState
* window_state
= wm::GetWindowState(gained_active
);
251 if (window_state
&& window_state
->IsMinimized() &&
252 !gained_active
->IsVisible()) {
253 window_state
->Unminimize();
254 DCHECK(!window_state
->IsMinimized());
256 UpdateFullscreenState();
257 UpdateShelfVisibility();
260 //////////////////////////////////////////////////////////////////////////////
261 // WorkspaceLayoutManager, wm::WindowStateObserver implementation:
263 void WorkspaceLayoutManager::OnPostWindowStateTypeChange(
264 wm::WindowState
* window_state
,
265 wm::WindowStateType old_type
) {
267 // Notify observers that fullscreen state may be changing.
268 if (window_state
->IsFullscreen() ||
269 old_type
== wm::WINDOW_STATE_TYPE_FULLSCREEN
) {
270 UpdateFullscreenState();
273 UpdateShelfVisibility();
274 if (backdrop_delegate_
)
275 backdrop_delegate_
->OnPostWindowStateTypeChange(window_state
, old_type
);
278 //////////////////////////////////////////////////////////////////////////////
279 // WorkspaceLayoutManager, private:
281 void WorkspaceLayoutManager::AdjustAllWindowsBoundsForWorkAreaChange(
282 const wm::WMEvent
* event
) {
283 DCHECK(event
->type() == wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED
||
284 event
->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
);
286 work_area_in_parent_
= ScreenUtil::ConvertRectFromScreen(
288 Shell::GetScreen()->GetDisplayNearestWindow(window_
).work_area());
290 // Don't do any adjustments of the insets while we are in screen locked mode.
291 // This would happen if the launcher was auto hidden before the login screen
292 // was shown and then gets shown when the login screen gets presented.
293 if (event
->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED
&&
294 Shell::GetInstance()->session_state_delegate()->IsScreenLocked())
297 // If a user plugs an external display into a laptop running Aura the
298 // display size will change. Maximized windows need to resize to match.
299 // We also do this when developers running Aura on a desktop manually resize
301 // We also need to do this when the work area insets changes.
302 for (WindowSet::const_iterator it
= windows_
.begin();
303 it
!= windows_
.end();
305 wm::GetWindowState(*it
)->OnWMEvent(event
);
309 void WorkspaceLayoutManager::UpdateShelfVisibility() {
311 shelf_
->UpdateVisibilityState();
314 void WorkspaceLayoutManager::UpdateFullscreenState() {
315 // TODO(flackr): The fullscreen state is currently tracked per workspace
316 // but the shell notification implies a per root window state. Currently
317 // only windows in the default workspace container will go fullscreen but
318 // this should really be tracked by the RootWindowController since
319 // technically any container could get a fullscreen window.
322 bool is_fullscreen
= GetRootWindowController(
323 window_
->GetRootWindow())->GetWindowForFullscreenMode() != NULL
;
324 if (is_fullscreen
!= is_fullscreen_
) {
325 ash::Shell::GetInstance()->NotifyFullscreenStateChange(
326 is_fullscreen
, window_
->GetRootWindow());
327 is_fullscreen_
= is_fullscreen
;