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.
4
5 #include "ash/wm/base_layout_manager.h"
6
7 #include "ash/screen_ash.h"
8 #include "ash/session_state_delegate.h"
9 #include "ash/shelf/shelf_layout_manager.h"
10 #include "ash/shell.h"
11 #include "ash/wm/window_animations.h"
12 #include "ash/wm/window_properties.h"
13 #include "ash/wm/window_state.h"
14 #include "ash/wm/window_util.h"
15 #include "ash/wm/workspace/workspace_window_resizer.h"
16 #include "ui/aura/client/activation_client.h"
17 #include "ui/aura/client/aura_constants.h"
18 #include "ui/aura/window.h"
19 #include "ui/base/ui_base_types.h"
20 #include "ui/compositor/layer.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/views/corewm/corewm_switches.h"
23 #include "ui/views/corewm/window_util.h"
24
25 namespace ash {
26 namespace internal {
27
28 /////////////////////////////////////////////////////////////////////////////
29 // BaseLayoutManager, public:
30
BaseLayoutManager(aura::Window * root_window)31 BaseLayoutManager::BaseLayoutManager(aura::Window* root_window)
32 : root_window_(root_window) {
33 Shell::GetInstance()->activation_client()->AddObserver(this);
34 Shell::GetInstance()->AddShellObserver(this);
35 root_window_->AddObserver(this);
36 }
37
~BaseLayoutManager()38 BaseLayoutManager::~BaseLayoutManager() {
39 if (root_window_)
40 root_window_->RemoveObserver(this);
41 for (WindowSet::const_iterator i = windows_.begin(); i != windows_.end(); ++i)
42 (*i)->RemoveObserver(this);
43 Shell::GetInstance()->RemoveShellObserver(this);
44 Shell::GetInstance()->activation_client()->RemoveObserver(this);
45 }
46
47 // static
BoundsWithScreenEdgeVisible(aura::Window * window,const gfx::Rect & restore_bounds)48 gfx::Rect BaseLayoutManager::BoundsWithScreenEdgeVisible(
49 aura::Window* window,
50 const gfx::Rect& restore_bounds) {
51 gfx::Rect max_bounds =
52 ash::ScreenAsh::GetMaximizedWindowBoundsInParent(window);
53 // If the restore_bounds are more than 1 grid step away from the size the
54 // window would be when maximized, inset it.
55 max_bounds.Inset(ash::internal::WorkspaceWindowResizer::kScreenEdgeInset,
56 ash::internal::WorkspaceWindowResizer::kScreenEdgeInset);
57 if (restore_bounds.Contains(max_bounds))
58 return max_bounds;
59 return restore_bounds;
60 }
61
62 /////////////////////////////////////////////////////////////////////////////
63 // BaseLayoutManager, aura::LayoutManager overrides:
64
OnWindowResized()65 void BaseLayoutManager::OnWindowResized() {
66 }
67
OnWindowAddedToLayout(aura::Window * child)68 void BaseLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
69 windows_.insert(child);
70 child->AddObserver(this);
71 wm::WindowState* window_state = wm::GetWindowState(child);
72 window_state->AddObserver(this);
73
74 // Only update the bounds if the window has a show state that depends on the
75 // workspace area.
76 if (window_state->IsMaximizedOrFullscreen())
77 UpdateBoundsFromShowState(window_state);
78 }
79
OnWillRemoveWindowFromLayout(aura::Window * child)80 void BaseLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) {
81 windows_.erase(child);
82 child->RemoveObserver(this);
83 wm::GetWindowState(child)->RemoveObserver(this);
84 }
85
OnWindowRemovedFromLayout(aura::Window * child)86 void BaseLayoutManager::OnWindowRemovedFromLayout(aura::Window* child) {
87 }
88
OnChildWindowVisibilityChanged(aura::Window * child,bool visible)89 void BaseLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child,
90 bool visible) {
91 wm::WindowState* window_state = wm::GetWindowState(child);
92 // Attempting to show a minimized window. Unminimize it.
93 if (visible && window_state->IsMinimized())
94 window_state->Unminimize();
95 }
96
SetChildBounds(aura::Window * child,const gfx::Rect & requested_bounds)97 void BaseLayoutManager::SetChildBounds(aura::Window* child,
98 const gfx::Rect& requested_bounds) {
99 gfx::Rect child_bounds(requested_bounds);
100 wm::WindowState* window_state = wm::GetWindowState(child);
101 // Some windows rely on this to set their initial bounds.
102 if (window_state->IsMaximized())
103 child_bounds = ScreenAsh::GetMaximizedWindowBoundsInParent(child);
104 else if (window_state->IsFullscreen())
105 child_bounds = ScreenAsh::GetDisplayBoundsInParent(child);
106 SetChildBoundsDirect(child, child_bounds);
107 }
108
109 /////////////////////////////////////////////////////////////////////////////
110 // BaseLayoutManager, aura::WindowObserver overrides:
111
OnWindowDestroying(aura::Window * window)112 void BaseLayoutManager::OnWindowDestroying(aura::Window* window) {
113 if (root_window_ == window) {
114 root_window_->RemoveObserver(this);
115 root_window_ = NULL;
116 }
117 }
118
OnWindowBoundsChanged(aura::Window * window,const gfx::Rect & old_bounds,const gfx::Rect & new_bounds)119 void BaseLayoutManager::OnWindowBoundsChanged(aura::Window* window,
120 const gfx::Rect& old_bounds,
121 const gfx::Rect& new_bounds) {
122 if (root_window_ == window)
123 AdjustAllWindowsBoundsForWorkAreaChange(ADJUST_WINDOW_DISPLAY_SIZE_CHANGED);
124 }
125
126 //////////////////////////////////////////////////////////////////////////////
127 // BaseLayoutManager, aura::client::ActivationChangeObserver implementation:
128
OnWindowActivated(aura::Window * gained_active,aura::Window * lost_active)129 void BaseLayoutManager::OnWindowActivated(aura::Window* gained_active,
130 aura::Window* lost_active) {
131 wm::WindowState* window_state = wm::GetWindowState(gained_active);
132 if (window_state && window_state->IsMinimized() &&
133 !gained_active->IsVisible()) {
134 window_state->Unminimize();
135 DCHECK(!window_state->IsMinimized());
136 }
137 }
138
139 /////////////////////////////////////////////////////////////////////////////
140 // BaseLayoutManager, ash::ShellObserver overrides:
141
OnDisplayWorkAreaInsetsChanged()142 void BaseLayoutManager::OnDisplayWorkAreaInsetsChanged() {
143 AdjustAllWindowsBoundsForWorkAreaChange(
144 ADJUST_WINDOW_WORK_AREA_INSETS_CHANGED);
145 }
146
147 /////////////////////////////////////////////////////////////////////////////
148 // BaseLayoutManager, ash::wm::WindowStateObserver overrides:
149
OnWindowShowTypeChanged(wm::WindowState * window_state,wm::WindowShowType old_type)150 void BaseLayoutManager::OnWindowShowTypeChanged(wm::WindowState* window_state,
151 wm::WindowShowType old_type) {
152 ui::WindowShowState old_state = ToWindowShowState(old_type);
153 ui::WindowShowState new_state = window_state->GetShowState();
154
155 if (old_state != new_state && old_state != ui::SHOW_STATE_MINIMIZED &&
156 !window_state->HasRestoreBounds() &&
157 ((new_state == ui::SHOW_STATE_MAXIMIZED &&
158 old_state != ui::SHOW_STATE_FULLSCREEN) ||
159 (new_state == ui::SHOW_STATE_FULLSCREEN &&
160 old_state != ui::SHOW_STATE_MAXIMIZED))) {
161 window_state->SetRestoreBoundsInParent(window_state->window()->bounds());
162 }
163
164 UpdateBoundsFromShowState(window_state);
165 ShowStateChanged(window_state, old_state);
166 }
167
168 //////////////////////////////////////////////////////////////////////////////
169 // BaseLayoutManager, protected:
170
ShowStateChanged(wm::WindowState * window_state,ui::WindowShowState last_show_state)171 void BaseLayoutManager::ShowStateChanged(
172 wm::WindowState* window_state,
173 ui::WindowShowState last_show_state) {
174 if (window_state->IsMinimized()) {
175 if (last_show_state == ui::SHOW_STATE_MINIMIZED)
176 return;
177
178 // Save the previous show state so that we can correctly restore it.
179 window_state->window()->SetProperty(aura::client::kRestoreShowStateKey,
180 last_show_state);
181 views::corewm::SetWindowVisibilityAnimationType(
182 window_state->window(), WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE);
183
184 // Hide the window.
185 window_state->window()->Hide();
186 // Activate another window.
187 if (window_state->IsActive())
188 window_state->Deactivate();
189 } else if ((window_state->window()->TargetVisibility() ||
190 last_show_state == ui::SHOW_STATE_MINIMIZED) &&
191 !window_state->window()->layer()->visible()) {
192 // The layer may be hidden if the window was previously minimized. Make
193 // sure it's visible.
194 window_state->window()->Show();
195 if (last_show_state == ui::SHOW_STATE_MINIMIZED &&
196 !window_state->IsMaximizedOrFullscreen()) {
197 window_state->set_always_restores_to_restore_bounds(false);
198 }
199 }
200 }
201
AdjustAllWindowsBoundsForWorkAreaChange(AdjustWindowReason reason)202 void BaseLayoutManager::AdjustAllWindowsBoundsForWorkAreaChange(
203 AdjustWindowReason reason) {
204 // Don't do any adjustments of the insets while we are in screen locked mode.
205 // This would happen if the launcher was auto hidden before the login screen
206 // was shown and then gets shown when the login screen gets presented.
207 if (reason == ADJUST_WINDOW_WORK_AREA_INSETS_CHANGED &&
208 Shell::GetInstance()->session_state_delegate()->IsScreenLocked())
209 return;
210
211 // If a user plugs an external display into a laptop running Aura the
212 // display size will change. Maximized windows need to resize to match.
213 // We also do this when developers running Aura on a desktop manually resize
214 // the host window.
215 // We also need to do this when the work area insets changes.
216 for (WindowSet::const_iterator it = windows_.begin();
217 it != windows_.end();
218 ++it) {
219 AdjustWindowBoundsForWorkAreaChange(wm::GetWindowState(*it), reason);
220 }
221 }
222
AdjustWindowBoundsForWorkAreaChange(wm::WindowState * window_state,AdjustWindowReason reason)223 void BaseLayoutManager::AdjustWindowBoundsForWorkAreaChange(
224 wm::WindowState* window_state,
225 AdjustWindowReason reason) {
226 aura::Window* window = window_state->window();
227 if (window_state->IsMaximized()) {
228 SetChildBoundsDirect(
229 window, ScreenAsh::GetMaximizedWindowBoundsInParent(window));
230 } else if (window_state->IsFullscreen()) {
231 SetChildBoundsDirect(
232 window, ScreenAsh::GetDisplayBoundsInParent(window));
233 } else {
234 // The work area may be smaller than the full screen.
235 gfx::Rect display_rect =
236 ScreenAsh::GetDisplayWorkAreaBoundsInParent(window);
237 // Put as much of the window as possible within the display area.
238 gfx::Rect bounds = window->bounds();
239 bounds.AdjustToFit(display_rect);
240 window->SetBounds(bounds);
241 }
242 }
243
244 //////////////////////////////////////////////////////////////////////////////
245 // BaseLayoutManager, private:
246
UpdateBoundsFromShowState(wm::WindowState * window_state)247 void BaseLayoutManager::UpdateBoundsFromShowState(
248 wm::WindowState* window_state) {
249 aura::Window* window = window_state->window();
250 switch (window_state->GetShowState()) {
251 case ui::SHOW_STATE_DEFAULT:
252 case ui::SHOW_STATE_NORMAL: {
253 if (window_state->HasRestoreBounds()) {
254 gfx::Rect bounds_in_parent = window_state->GetRestoreBoundsInParent();
255 SetChildBoundsDirect(window,
256 BoundsWithScreenEdgeVisible(window,
257 bounds_in_parent));
258 }
259 window_state->ClearRestoreBounds();
260 break;
261 }
262
263 case ui::SHOW_STATE_MAXIMIZED:
264 SetChildBoundsDirect(
265 window, ScreenAsh::GetMaximizedWindowBoundsInParent(window));
266 break;
267
268 case ui::SHOW_STATE_FULLSCREEN:
269 // Don't animate the full-screen window transition.
270 // TODO(jamescook): Use animation here. Be sure the lock screen works.
271 SetChildBoundsDirect(window,
272 ScreenAsh::GetDisplayBoundsInParent(window));
273 break;
274
275 default:
276 break;
277 }
278 }
279
280 } // namespace internal
281 } // namespace ash
282