OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h" | |
6 | |
7 #include "ash/ash_switches.h" | |
8 #include "ash/public/cpp/shell_window_ids.h" | |
9 #include "ash/root_window_controller.h" | |
10 #include "ash/session/session_state_delegate.h" | |
11 #include "ash/shell.h" | |
12 #include "ash/shell_port.h" | |
13 #include "ash/wm/maximize_mode/maximize_mode_backdrop_delegate_impl.h" | |
14 #include "ash/wm/maximize_mode/maximize_mode_event_handler.h" | |
15 #include "ash/wm/maximize_mode/maximize_mode_window_state.h" | |
16 #include "ash/wm/mru_window_tracker.h" | |
17 #include "ash/wm/overview/window_selector_controller.h" | |
18 #include "ash/wm/window_state.h" | |
19 #include "ash/wm/wm_event.h" | |
20 #include "ash/wm/workspace_controller.h" | |
21 #include "base/command_line.h" | |
22 #include "base/memory/ptr_util.h" | |
23 #include "base/stl_util.h" | |
24 #include "ui/aura/client/aura_constants.h" | |
25 #include "ui/display/screen.h" | |
26 | |
27 namespace ash { | |
28 | |
29 namespace { | |
30 | |
31 // Exits overview mode if it is currently active. | |
32 void CancelOverview() { | |
33 WindowSelectorController* controller = | |
34 Shell::Get()->window_selector_controller(); | |
35 if (controller->IsSelecting()) | |
36 controller->OnSelectionEnded(); | |
37 } | |
38 | |
39 } // namespace | |
40 | |
41 MaximizeModeWindowManager::~MaximizeModeWindowManager() { | |
42 // Overview mode needs to be ended before exiting maximize mode to prevent | |
43 // transforming windows which are currently in | |
44 // overview: http://crbug.com/366605 | |
45 CancelOverview(); | |
46 for (aura::Window* window : added_windows_) | |
47 window->RemoveObserver(this); | |
48 added_windows_.clear(); | |
49 Shell::Get()->RemoveShellObserver(this); | |
50 display::Screen::GetScreen()->RemoveObserver(this); | |
51 EnableBackdropBehindTopWindowOnEachDisplay(false); | |
52 RemoveWindowCreationObservers(); | |
53 RestoreAllWindows(); | |
54 } | |
55 | |
56 int MaximizeModeWindowManager::GetNumberOfManagedWindows() { | |
57 return window_state_map_.size(); | |
58 } | |
59 | |
60 void MaximizeModeWindowManager::AddWindow(aura::Window* window) { | |
61 // Only add the window if it is a direct dependent of a container window | |
62 // and not yet tracked. | |
63 if (!ShouldHandleWindow(window) || | |
64 base::ContainsKey(window_state_map_, window) || | |
65 !IsContainerWindow(window->parent())) { | |
66 return; | |
67 } | |
68 | |
69 MaximizeAndTrackWindow(window); | |
70 } | |
71 | |
72 void MaximizeModeWindowManager::WindowStateDestroyed(aura::Window* window) { | |
73 // At this time ForgetWindow() should already have been called. If not, | |
74 // someone else must have replaced the "window manager's state object". | |
75 DCHECK(!window->HasObserver(this)); | |
76 | |
77 auto it = window_state_map_.find(window); | |
78 DCHECK(it != window_state_map_.end()); | |
79 window_state_map_.erase(it); | |
80 } | |
81 | |
82 void MaximizeModeWindowManager::OnOverviewModeStarting() { | |
83 SetDeferBoundsUpdates(true); | |
84 } | |
85 | |
86 void MaximizeModeWindowManager::OnOverviewModeEnded() { | |
87 SetDeferBoundsUpdates(false); | |
88 } | |
89 | |
90 void MaximizeModeWindowManager::OnWindowDestroying(aura::Window* window) { | |
91 if (IsContainerWindow(window)) { | |
92 // container window can be removed on display destruction. | |
93 window->RemoveObserver(this); | |
94 observed_container_windows_.erase(window); | |
95 } else if (base::ContainsValue(added_windows_, window)) { | |
96 // Added window was destroyed before being shown. | |
97 added_windows_.erase(window); | |
98 window->RemoveObserver(this); | |
99 } else { | |
100 // If a known window gets destroyed we need to remove all knowledge about | |
101 // it. | |
102 ForgetWindow(window); | |
103 } | |
104 } | |
105 | |
106 void MaximizeModeWindowManager::OnWindowHierarchyChanged( | |
107 const HierarchyChangeParams& params) { | |
108 // A window can get removed and then re-added by a drag and drop operation. | |
109 if (params.new_parent && IsContainerWindow(params.new_parent) && | |
110 !base::ContainsKey(window_state_map_, params.target)) { | |
111 // Don't register the window if the window is invisible. Instead, | |
112 // wait until it becomes visible because the client may update the | |
113 // flag to control if the window should be added. | |
114 if (!params.target->IsVisible()) { | |
115 if (!base::ContainsValue(added_windows_, params.target)) { | |
116 added_windows_.insert(params.target); | |
117 params.target->AddObserver(this); | |
118 } | |
119 return; | |
120 } | |
121 MaximizeAndTrackWindow(params.target); | |
122 // When the state got added, the "WM_EVENT_ADDED_TO_WORKSPACE" event got | |
123 // already sent and we have to notify our state again. | |
124 if (base::ContainsKey(window_state_map_, params.target)) { | |
125 wm::WMEvent event(wm::WM_EVENT_ADDED_TO_WORKSPACE); | |
126 wm::GetWindowState(params.target)->OnWMEvent(&event); | |
127 } | |
128 } | |
129 } | |
130 | |
131 void MaximizeModeWindowManager::OnWindowPropertyChanged(aura::Window* window, | |
132 const void* key, | |
133 intptr_t old) { | |
134 // Stop managing |window| if the always-on-top property is added. | |
135 if (key == aura::client::kAlwaysOnTopKey && | |
136 window->GetProperty(aura::client::kAlwaysOnTopKey)) { | |
137 ForgetWindow(window); | |
138 } | |
139 } | |
140 | |
141 void MaximizeModeWindowManager::OnWindowBoundsChanged( | |
142 aura::Window* window, | |
143 const gfx::Rect& old_bounds, | |
144 const gfx::Rect& new_bounds) { | |
145 if (!IsContainerWindow(window)) | |
146 return; | |
147 // Reposition all non maximizeable windows. | |
148 for (auto& pair : window_state_map_) | |
149 pair.second->UpdateWindowPosition(wm::GetWindowState(pair.first)); | |
150 } | |
151 | |
152 void MaximizeModeWindowManager::OnWindowVisibilityChanged(aura::Window* window, | |
153 bool visible) { | |
154 // Skip if it's already managed. | |
155 if (base::ContainsKey(window_state_map_, window)) | |
156 return; | |
157 | |
158 if (IsContainerWindow(window->parent()) && | |
159 base::ContainsValue(added_windows_, window) && visible) { | |
160 added_windows_.erase(window); | |
161 window->RemoveObserver(this); | |
162 MaximizeAndTrackWindow(window); | |
163 // When the state got added, the "WM_EVENT_ADDED_TO_WORKSPACE" event got | |
164 // already sent and we have to notify our state again. | |
165 if (base::ContainsKey(window_state_map_, window)) { | |
166 wm::WMEvent event(wm::WM_EVENT_ADDED_TO_WORKSPACE); | |
167 wm::GetWindowState(window)->OnWMEvent(&event); | |
168 } | |
169 } | |
170 } | |
171 | |
172 void MaximizeModeWindowManager::OnDisplayAdded( | |
173 const display::Display& display) { | |
174 DisplayConfigurationChanged(); | |
175 } | |
176 | |
177 void MaximizeModeWindowManager::OnDisplayRemoved( | |
178 const display::Display& display) { | |
179 DisplayConfigurationChanged(); | |
180 } | |
181 | |
182 void MaximizeModeWindowManager::OnDisplayMetricsChanged(const display::Display&, | |
183 uint32_t) { | |
184 // Nothing to do here. | |
185 } | |
186 | |
187 void MaximizeModeWindowManager::SetIgnoreWmEventsForExit() { | |
188 for (auto& pair : window_state_map_) { | |
189 pair.second->set_ignore_wm_events(true); | |
190 } | |
191 } | |
192 | |
193 MaximizeModeWindowManager::MaximizeModeWindowManager() { | |
194 // The overview mode needs to be ended before the maximize mode is started. To | |
195 // guarantee the proper order, it will be turned off from here. | |
196 CancelOverview(); | |
197 | |
198 MaximizeAllWindows(); | |
199 AddWindowCreationObservers(); | |
200 EnableBackdropBehindTopWindowOnEachDisplay(true); | |
201 display::Screen::GetScreen()->AddObserver(this); | |
202 Shell::Get()->AddShellObserver(this); | |
203 event_handler_ = ShellPort::Get()->CreateMaximizeModeEventHandler(); | |
204 } | |
205 | |
206 void MaximizeModeWindowManager::MaximizeAllWindows() { | |
207 MruWindowTracker::WindowList windows = | |
208 Shell::Get()->mru_window_tracker()->BuildWindowListIgnoreModal(); | |
209 // Add all existing MRU windows. | |
210 for (auto* window : windows) | |
211 MaximizeAndTrackWindow(window); | |
212 } | |
213 | |
214 void MaximizeModeWindowManager::RestoreAllWindows() { | |
215 while (window_state_map_.size()) | |
216 ForgetWindow(window_state_map_.begin()->first); | |
217 } | |
218 | |
219 void MaximizeModeWindowManager::SetDeferBoundsUpdates( | |
220 bool defer_bounds_updates) { | |
221 for (auto& pair : window_state_map_) | |
222 pair.second->SetDeferBoundsUpdates(defer_bounds_updates); | |
223 } | |
224 | |
225 void MaximizeModeWindowManager::MaximizeAndTrackWindow(aura::Window* window) { | |
226 if (!ShouldHandleWindow(window)) | |
227 return; | |
228 | |
229 DCHECK(!base::ContainsKey(window_state_map_, window)); | |
230 window->AddObserver(this); | |
231 | |
232 // We create and remember a maximize mode state which will attach itself to | |
233 // the provided state object. | |
234 window_state_map_[window] = new MaximizeModeWindowState(window, this); | |
235 } | |
236 | |
237 void MaximizeModeWindowManager::ForgetWindow(aura::Window* window) { | |
238 WindowToState::iterator it = window_state_map_.find(window); | |
239 | |
240 // The following DCHECK could fail if our window state object was destroyed | |
241 // earlier by someone else. However - at this point there is no other client | |
242 // which replaces the state object and therefore this should not happen. | |
243 DCHECK(it != window_state_map_.end()); | |
244 window->RemoveObserver(this); | |
245 | |
246 // By telling the state object to revert, it will switch back the old | |
247 // State object and destroy itself, calling WindowStateDestroyed(). | |
248 it->second->LeaveMaximizeMode(wm::GetWindowState(it->first)); | |
249 DCHECK(!base::ContainsKey(window_state_map_, window)); | |
250 } | |
251 | |
252 bool MaximizeModeWindowManager::ShouldHandleWindow(aura::Window* window) { | |
253 DCHECK(window); | |
254 | |
255 // Windows with the always-on-top property should be free-floating and thus | |
256 // not managed by us. | |
257 if (window->GetProperty(aura::client::kAlwaysOnTopKey)) | |
258 return false; | |
259 | |
260 // If the changing bounds in the maximized/fullscreen is allowed, then | |
261 // let the client manage it even in maximized mode. | |
262 if (wm::GetWindowState(window)->allow_set_bounds_direct()) | |
263 return false; | |
264 | |
265 return window->type() == aura::client::WINDOW_TYPE_NORMAL; | |
266 } | |
267 | |
268 void MaximizeModeWindowManager::AddWindowCreationObservers() { | |
269 DCHECK(observed_container_windows_.empty()); | |
270 // Observe window activations/creations in the default containers on all root | |
271 // windows. | |
272 for (aura::Window* root : Shell::GetAllRootWindows()) { | |
273 aura::Window* default_container = | |
274 root->GetChildById(kShellWindowId_DefaultContainer); | |
275 DCHECK(!base::ContainsKey(observed_container_windows_, default_container)); | |
276 default_container->AddObserver(this); | |
277 observed_container_windows_.insert(default_container); | |
278 } | |
279 } | |
280 | |
281 void MaximizeModeWindowManager::RemoveWindowCreationObservers() { | |
282 for (aura::Window* window : observed_container_windows_) | |
283 window->RemoveObserver(this); | |
284 observed_container_windows_.clear(); | |
285 } | |
286 | |
287 void MaximizeModeWindowManager::DisplayConfigurationChanged() { | |
288 EnableBackdropBehindTopWindowOnEachDisplay(false); | |
289 RemoveWindowCreationObservers(); | |
290 AddWindowCreationObservers(); | |
291 EnableBackdropBehindTopWindowOnEachDisplay(true); | |
292 } | |
293 | |
294 bool MaximizeModeWindowManager::IsContainerWindow(aura::Window* window) { | |
295 return base::ContainsKey(observed_container_windows_, window); | |
296 } | |
297 | |
298 void MaximizeModeWindowManager::EnableBackdropBehindTopWindowOnEachDisplay( | |
299 bool enable) { | |
300 // Inform the WorkspaceLayoutManager that we want to show a backdrop behind | |
301 // the topmost window of its container. | |
302 for (auto* controller : Shell::GetAllRootWindowControllers()) { | |
303 controller->workspace_controller()->SetBackdropDelegate( | |
304 enable ? base::MakeUnique<MaximizeModeBackdropDelegateImpl>() | |
305 : nullptr); | |
306 } | |
307 } | |
308 | |
309 } // namespace ash | |
OLD | NEW |