1 // Copyright 2014 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/maximize_mode/maximize_mode_window_manager.h"
7 #include "ash/root_window_controller.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/wm/maximize_mode/maximize_mode_window_state.h"
11 #include "ash/wm/maximize_mode/workspace_backdrop_delegate.h"
12 #include "ash/wm/mru_window_tracker.h"
13 #include "ash/wm/overview/window_selector_controller.h"
14 #include "ash/wm/wm_event.h"
15 #include "ash/wm/workspace_controller.h"
16 #include "ui/aura/window.h"
17 #include "ui/gfx/screen.h"
23 // Exits overview mode if it is currently active.
24 void CancelOverview() {
25 WindowSelectorController
* controller
=
26 Shell::GetInstance()->window_selector_controller();
27 if (controller
&& controller
->IsSelecting())
28 controller
->OnSelectionCanceled();
33 MaximizeModeWindowManager::~MaximizeModeWindowManager() {
34 // Overview mode needs to be ended before exiting maximize mode to prevent
35 // transforming windows which are currently in
36 // overview: http://crbug.com/366605
39 Shell::GetInstance()->RemoveShellObserver(this);
40 Shell::GetScreen()->RemoveObserver(this);
41 EnableBackdropBehindTopWindowOnEachDisplay(false);
42 RemoveWindowCreationObservers();
44 Shell::GetInstance()->OnMaximizeModeEnded();
47 int MaximizeModeWindowManager::GetNumberOfManagedWindows() {
48 return window_state_map_
.size();
51 void MaximizeModeWindowManager::WindowStateDestroyed(aura::Window
* window
) {
52 // At this time ForgetWindow() should already have been called. If not,
53 // someone else must have replaced the "window manager's state object".
54 DCHECK(!window
->HasObserver(this));
56 WindowToState::iterator it
= window_state_map_
.find(window
);
57 DCHECK(it
!= window_state_map_
.end());
58 window_state_map_
.erase(it
);
61 void MaximizeModeWindowManager::OnOverviewModeStarting() {
62 if (backdrops_hidden_
)
65 EnableBackdropBehindTopWindowOnEachDisplay(false);
66 backdrops_hidden_
= true;
69 void MaximizeModeWindowManager::OnOverviewModeEnding() {
70 if (!backdrops_hidden_
)
73 backdrops_hidden_
= false;
74 EnableBackdropBehindTopWindowOnEachDisplay(true);
77 void MaximizeModeWindowManager::OnWindowDestroying(aura::Window
* window
) {
78 // If a known window gets destroyed we need to remove all knowledge about it.
79 if (!IsContainerWindow(window
))
83 void MaximizeModeWindowManager::OnWindowAdded(aura::Window
* window
) {
84 // A window can get removed and then re-added by a drag and drop operation.
85 if (IsContainerWindow(window
->parent()) &&
86 window_state_map_
.find(window
) == window_state_map_
.end()) {
87 MaximizeAndTrackWindow(window
);
88 // When the state got added, the "WM_EVENT_ADDED_TO_WORKSPACE" event got
89 // already sent and we have to notify our state again.
90 if (window_state_map_
.find(window
) != window_state_map_
.end()) {
91 wm::WMEvent
event(wm::WM_EVENT_ADDED_TO_WORKSPACE
);
92 wm::GetWindowState(window
)->OnWMEvent(&event
);
97 void MaximizeModeWindowManager::OnWindowBoundsChanged(
99 const gfx::Rect
& old_bounds
,
100 const gfx::Rect
& new_bounds
) {
101 if (!IsContainerWindow(window
))
103 // Reposition all non maximizeable windows.
104 for (WindowToState::iterator it
= window_state_map_
.begin();
105 it
!= window_state_map_
.end();
107 it
->second
->UpdateWindowPosition(wm::GetWindowState(it
->first
), false);
111 void MaximizeModeWindowManager::OnDisplayBoundsChanged(
112 const gfx::Display
& display
) {
113 // Nothing to do here.
116 void MaximizeModeWindowManager::OnDisplayAdded(const gfx::Display
& display
) {
117 DisplayConfigurationChanged();
120 void MaximizeModeWindowManager::OnDisplayRemoved(const gfx::Display
& display
) {
121 DisplayConfigurationChanged();
124 MaximizeModeWindowManager::MaximizeModeWindowManager()
125 : backdrops_hidden_(false) {
126 // The overview mode needs to be ended before the maximize mode is started. To
127 // guarantee the proper order, it will be turned off from here.
130 MaximizeAllWindows();
131 AddWindowCreationObservers();
132 EnableBackdropBehindTopWindowOnEachDisplay(true);
133 Shell::GetInstance()->OnMaximizeModeStarted();
134 Shell::GetScreen()->AddObserver(this);
135 Shell::GetInstance()->AddShellObserver(this);
138 void MaximizeModeWindowManager::MaximizeAllWindows() {
139 MruWindowTracker::WindowList windows
=
140 MruWindowTracker::BuildWindowList(false);
141 // Add all existing Mru windows.
142 for (MruWindowTracker::WindowList::iterator window
= windows
.begin();
143 window
!= windows
.end(); ++window
) {
144 MaximizeAndTrackWindow(*window
);
148 void MaximizeModeWindowManager::RestoreAllWindows() {
149 while (window_state_map_
.size())
150 ForgetWindow(window_state_map_
.begin()->first
);
153 void MaximizeModeWindowManager::MaximizeAndTrackWindow(
154 aura::Window
* window
) {
155 if (!ShouldHandleWindow(window
))
158 DCHECK(window_state_map_
.find(window
) == window_state_map_
.end());
159 window
->AddObserver(this);
161 // We create and remember a maximize mode state which will attach itself to
162 // the provided state object.
163 window_state_map_
[window
] = new MaximizeModeWindowState(window
, this);
166 void MaximizeModeWindowManager::ForgetWindow(aura::Window
* window
) {
167 WindowToState::iterator it
= window_state_map_
.find(window
);
169 // The following DCHECK could fail if our window state object was destroyed
170 // earlier by someone else. However - at this point there is no other client
171 // which replaces the state object and therefore this should not happen.
172 DCHECK(it
!= window_state_map_
.end());
173 window
->RemoveObserver(this);
175 // By telling the state object to revert, it will switch back the old
176 // State object and destroy itself, calling WindowStateDerstroyed().
177 it
->second
->LeaveMaximizeMode(wm::GetWindowState(it
->first
));
178 DCHECK(window_state_map_
.find(window
) == window_state_map_
.end());
181 bool MaximizeModeWindowManager::ShouldHandleWindow(aura::Window
* window
) {
183 return window
->type() == ui::wm::WINDOW_TYPE_NORMAL
;
186 void MaximizeModeWindowManager::AddWindowCreationObservers() {
187 DCHECK(observed_container_windows_
.empty());
188 // Observe window activations/creations in the default containers on all root
190 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
191 for (aura::Window::Windows::const_iterator iter
= root_windows
.begin();
192 iter
!= root_windows
.end(); ++iter
) {
193 aura::Window
* container
=
194 Shell::GetContainer(*iter
, kShellWindowId_DefaultContainer
);
195 DCHECK(observed_container_windows_
.find(container
) ==
196 observed_container_windows_
.end());
197 container
->AddObserver(this);
198 observed_container_windows_
.insert(container
);
202 void MaximizeModeWindowManager::RemoveWindowCreationObservers() {
203 for (std::set
<aura::Window
*>::iterator iter
=
204 observed_container_windows_
.begin();
205 iter
!= observed_container_windows_
.end(); ++iter
) {
206 (*iter
)->RemoveObserver(this);
208 observed_container_windows_
.clear();
211 void MaximizeModeWindowManager::DisplayConfigurationChanged() {
212 EnableBackdropBehindTopWindowOnEachDisplay(false);
213 RemoveWindowCreationObservers();
214 AddWindowCreationObservers();
215 EnableBackdropBehindTopWindowOnEachDisplay(true);
218 bool MaximizeModeWindowManager::IsContainerWindow(aura::Window
* window
) {
219 return observed_container_windows_
.find(window
) !=
220 observed_container_windows_
.end();
223 void MaximizeModeWindowManager::EnableBackdropBehindTopWindowOnEachDisplay(
225 if (backdrops_hidden_
)
227 // Inform the WorkspaceLayoutManager that we want to show a backdrop behind
228 // the topmost window of its container.
229 Shell::RootWindowControllerList controllers
=
230 Shell::GetAllRootWindowControllers();
231 for (Shell::RootWindowControllerList::iterator iter
= controllers
.begin();
232 iter
!= controllers
.end(); ++iter
) {
233 RootWindowController
* controller
= *iter
;
234 aura::Window
* container
= Shell::GetContainer(
235 controller
->GetRootWindow(), kShellWindowId_DefaultContainer
);
236 controller
->workspace_controller()->SetMaximizeBackdropDelegate(
237 scoped_ptr
<WorkspaceLayoutManagerDelegate
>(
238 enable
? new WorkspaceBackdropDelegate(container
) : NULL
));