OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/wm/screen_pinning_controller.h" | 5 #include "ash/wm/screen_pinning_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "ash/common/wm/container_finder.h" | 10 #include "ash/common/wm/container_finder.h" |
11 #include "ash/common/wm/window_dimmer.h" | 11 #include "ash/common/wm/window_dimmer.h" |
12 #include "ash/common/wm/window_state.h" | 12 #include "ash/common/wm/window_state.h" |
13 #include "ash/common/wm_window.h" | 13 #include "ash/common/wm_window.h" |
14 #include "ash/common/wm_window_user_data.h" | |
15 #include "ash/display/window_tree_host_manager.h" | 14 #include "ash/display/window_tree_host_manager.h" |
16 #include "ash/public/cpp/shell_window_ids.h" | 15 #include "ash/public/cpp/shell_window_ids.h" |
17 #include "ash/shell.h" | 16 #include "ash/shell.h" |
18 #include "base/auto_reset.h" | 17 #include "base/auto_reset.h" |
19 #include "base/logging.h" | 18 #include "base/logging.h" |
20 #include "base/memory/ptr_util.h" | 19 #include "base/memory/ptr_util.h" |
| 20 #include "ui/aura/window.h" |
21 #include "ui/aura/window_observer.h" | 21 #include "ui/aura/window_observer.h" |
| 22 #include "ui/wm/core/window_util.h" |
22 | 23 |
23 namespace ash { | 24 namespace ash { |
24 namespace { | 25 namespace { |
25 | 26 |
| 27 DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(WindowDimmer, kWindowDimmerKey, nullptr); |
| 28 |
26 // Returns a list of WmWindows corresponding to SystemModalContainers, | 29 // Returns a list of WmWindows corresponding to SystemModalContainers, |
27 // except ones whose root is shared with |pinned_window|. | 30 // except ones whose root is shared with |pinned_window|. |
28 std::vector<WmWindow*> GetSystemModalWindowsExceptPinned( | 31 std::vector<WmWindow*> GetSystemModalWindowsExceptPinned( |
29 WmWindow* pinned_window) { | 32 WmWindow* pinned_window) { |
30 WmWindow* pinned_root = pinned_window->GetRootWindow(); | 33 WmWindow* pinned_root = pinned_window->GetRootWindow(); |
31 | 34 |
32 std::vector<WmWindow*> result; | 35 std::vector<WmWindow*> result; |
33 for (WmWindow* system_modal : wm::GetContainersFromAllRootWindows( | 36 for (aura::Window* system_modal : wm::GetContainersFromAllRootWindows( |
34 kShellWindowId_SystemModalContainer)) { | 37 kShellWindowId_SystemModalContainer)) { |
35 if (system_modal->GetRootWindow() == pinned_root) | 38 if (WmWindow::Get(system_modal)->GetRootWindow() == pinned_root) |
36 continue; | 39 continue; |
37 result.push_back(system_modal); | 40 result.push_back(WmWindow::Get(system_modal)); |
38 } | 41 } |
39 return result; | 42 return result; |
40 } | 43 } |
41 | 44 |
42 void AddObserverToChildren(WmWindow* container, | 45 void AddObserverToChildren(WmWindow* container, |
43 aura::WindowObserver* observer) { | 46 aura::WindowObserver* observer) { |
44 for (WmWindow* child : container->GetChildren()) { | 47 for (WmWindow* child : container->GetChildren()) { |
45 WmWindow::GetAuraWindow(child)->AddObserver(observer); | 48 WmWindow::GetAuraWindow(child)->AddObserver(observer); |
46 } | 49 } |
47 } | 50 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 window->RemoveObserver(this); | 138 window->RemoveObserver(this); |
136 } | 139 } |
137 | 140 |
138 private: | 141 private: |
139 ScreenPinningController* controller_; | 142 ScreenPinningController* controller_; |
140 DISALLOW_COPY_AND_ASSIGN(SystemModalContainerWindowObserver); | 143 DISALLOW_COPY_AND_ASSIGN(SystemModalContainerWindowObserver); |
141 }; | 144 }; |
142 | 145 |
143 ScreenPinningController::ScreenPinningController( | 146 ScreenPinningController::ScreenPinningController( |
144 WindowTreeHostManager* window_tree_host_manager) | 147 WindowTreeHostManager* window_tree_host_manager) |
145 : window_dimmers_(base::MakeUnique<WmWindowUserData<WindowDimmer>>()), | 148 : window_tree_host_manager_(window_tree_host_manager), |
146 window_tree_host_manager_(window_tree_host_manager), | |
147 pinned_container_window_observer_( | 149 pinned_container_window_observer_( |
148 base::MakeUnique<PinnedContainerWindowObserver>(this)), | 150 base::MakeUnique<PinnedContainerWindowObserver>(this)), |
149 pinned_container_child_window_observer_( | 151 pinned_container_child_window_observer_( |
150 base::MakeUnique<PinnedContainerChildWindowObserver>(this)), | 152 base::MakeUnique<PinnedContainerChildWindowObserver>(this)), |
151 system_modal_container_window_observer_( | 153 system_modal_container_window_observer_( |
152 base::MakeUnique<SystemModalContainerWindowObserver>(this)), | 154 base::MakeUnique<SystemModalContainerWindowObserver>(this)), |
153 system_modal_container_child_window_observer_( | 155 system_modal_container_child_window_observer_( |
154 base::MakeUnique<SystemModalContainerChildWindowObserver>(this)) { | 156 base::MakeUnique<SystemModalContainerChildWindowObserver>(this)) { |
155 window_tree_host_manager_->AddObserver(this); | 157 window_tree_host_manager_->AddObserver(this); |
156 } | 158 } |
157 | 159 |
158 ScreenPinningController::~ScreenPinningController() { | 160 ScreenPinningController::~ScreenPinningController() { |
159 window_tree_host_manager_->RemoveObserver(this); | 161 window_tree_host_manager_->RemoveObserver(this); |
160 } | 162 } |
161 | 163 |
162 bool ScreenPinningController::IsPinned() const { | 164 bool ScreenPinningController::IsPinned() const { |
163 return pinned_window_ != nullptr; | 165 return pinned_window_ != nullptr; |
164 } | 166 } |
165 | 167 |
166 void ScreenPinningController::SetPinnedWindow(WmWindow* pinned_window) { | 168 void ScreenPinningController::SetPinnedWindow(WmWindow* pinned_window) { |
167 window_dimmers_->clear(); | 169 for (aura::Window* window : windows_with_dimmers_.windows()) |
| 170 window->ClearProperty(kWindowDimmerKey); |
| 171 windows_with_dimmers_.RemoveAll(); |
168 | 172 |
169 if (pinned_window->GetWindowState()->IsPinned()) { | 173 if (pinned_window->GetWindowState()->IsPinned()) { |
170 if (pinned_window_) { | 174 if (pinned_window_) { |
171 LOG(DFATAL) << "Pinned mode is enabled, while it is already in " | 175 LOG(DFATAL) << "Pinned mode is enabled, while it is already in " |
172 << "the pinned mode"; | 176 << "the pinned mode"; |
173 return; | 177 return; |
174 } | 178 } |
175 | 179 |
176 WmWindow* container = pinned_window->GetParent(); | 180 WmWindow* container = pinned_window->GetParent(); |
177 std::vector<WmWindow*> system_modal_containers = | 181 std::vector<WmWindow*> system_modal_containers = |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 system_modal_container_child_window_observer_.get()); | 267 system_modal_container_child_window_observer_.get()); |
264 } | 268 } |
265 | 269 |
266 void ScreenPinningController::OnSystemModalContainerWindowStackingChanged( | 270 void ScreenPinningController::OnSystemModalContainerWindowStackingChanged( |
267 WmWindow* window) { | 271 WmWindow* window) { |
268 KeepDimWindowAtBottom(window->GetParent()); | 272 KeepDimWindowAtBottom(window->GetParent()); |
269 } | 273 } |
270 | 274 |
271 WmWindow* ScreenPinningController::CreateWindowDimmer(WmWindow* container) { | 275 WmWindow* ScreenPinningController::CreateWindowDimmer(WmWindow* container) { |
272 std::unique_ptr<WindowDimmer> window_dimmer = | 276 std::unique_ptr<WindowDimmer> window_dimmer = |
273 base::MakeUnique<WindowDimmer>(container); | 277 base::MakeUnique<WindowDimmer>(container->aura_window()); |
274 window_dimmer->SetDimOpacity(1); // Fully opaque. | 278 window_dimmer->SetDimOpacity(1); // Fully opaque. |
275 window_dimmer->window()->SetFullscreen(true); | 279 ::wm::SetWindowFullscreen(window_dimmer->window(), true); |
276 window_dimmer->window()->Show(); | 280 window_dimmer->window()->Show(); |
277 WmWindow* window = window_dimmer->window(); | 281 WmWindow* window = WmWindow::Get(window_dimmer->window()); |
278 window_dimmers_->Set(container, std::move(window_dimmer)); | 282 // |container| takes ownership of |window_dimmer|. |
| 283 container->aura_window()->SetProperty(kWindowDimmerKey, |
| 284 window_dimmer.release()); |
| 285 windows_with_dimmers_.Add(container->aura_window()); |
279 return window; | 286 return window; |
280 } | 287 } |
281 | 288 |
282 void ScreenPinningController::OnDisplayConfigurationChanged() { | 289 void ScreenPinningController::OnDisplayConfigurationChanged() { |
283 // Note: this is called on display attached or detached. | 290 // Note: this is called on display attached or detached. |
284 if (!IsPinned()) | 291 if (!IsPinned()) |
285 return; | 292 return; |
286 | 293 |
287 // On display detaching, all necessary windows are transfered to the | 294 // On display detaching, all necessary windows are transfered to the |
288 // primary display's tree, and called this. | 295 // primary display's tree, and called this. |
289 // So, delete WindowDimmers which are not a part of target system modal | 296 // So, delete WindowDimmers which are not a part of target system modal |
290 // container. | 297 // container. |
291 // On display attaching, the new system modal container does not have the | 298 // On display attaching, the new system modal container does not have the |
292 // WindowDimmer. So create it. | 299 // WindowDimmer. So create it. |
293 | 300 |
294 // First, delete unnecessary WindowDimmers. | 301 // First, delete unnecessary WindowDimmers. |
295 for (WmWindow* container : window_dimmers_->GetWindows()) { | 302 { |
296 if (container != pinned_window_->GetParent() && | 303 aura::Window::Windows windows = windows_with_dimmers_.windows(); |
297 !window_dimmers_->Get(container)->window()->GetTargetVisibility()) { | 304 for (aura::Window* container : windows) { |
298 window_dimmers_->Set(container, nullptr); | 305 if (WmWindow::Get(container) != pinned_window_->GetParent() && |
| 306 container->GetProperty(kWindowDimmerKey) |
| 307 ->window() |
| 308 ->layer() |
| 309 ->GetTargetVisibility()) { |
| 310 windows_with_dimmers_.Remove(container); |
| 311 container->ClearProperty(kWindowDimmerKey); |
| 312 } |
299 } | 313 } |
300 } | 314 } |
301 | 315 |
302 // Then, create missing WindowDimmers. | 316 // Then, create missing WindowDimmers. |
303 std::vector<WmWindow*> system_modal_containers = | 317 std::vector<WmWindow*> system_modal_containers = |
304 GetSystemModalWindowsExceptPinned(pinned_window_); | 318 GetSystemModalWindowsExceptPinned(pinned_window_); |
305 for (WmWindow* system_modal : system_modal_containers) { | 319 for (WmWindow* system_modal : system_modal_containers) { |
306 if (window_dimmers_->Get(system_modal)) { | 320 if (system_modal->aura_window()->GetProperty(kWindowDimmerKey)) { |
307 // |system_modal| already has a WindowDimmer. | 321 // |system_modal| already has a WindowDimmer. |
308 continue; | 322 continue; |
309 } | 323 } |
310 | 324 |
311 // This is the new system modal dialog. | 325 // This is the new system modal dialog. |
312 system_modal->StackChildAtBottom(CreateWindowDimmer(system_modal)); | 326 system_modal->StackChildAtBottom(CreateWindowDimmer(system_modal)); |
313 | 327 |
314 // Set observers to the tree. | 328 // Set observers to the tree. |
315 WmWindow::GetAuraWindow(system_modal) | 329 WmWindow::GetAuraWindow(system_modal) |
316 ->AddObserver(system_modal_container_window_observer_.get()); | 330 ->AddObserver(system_modal_container_window_observer_.get()); |
317 AddObserverToChildren(system_modal, | 331 AddObserverToChildren(system_modal, |
318 system_modal_container_child_window_observer_.get()); | 332 system_modal_container_child_window_observer_.get()); |
319 } | 333 } |
320 } | 334 } |
321 | 335 |
322 void ScreenPinningController::KeepPinnedWindowOnTop() { | 336 void ScreenPinningController::KeepPinnedWindowOnTop() { |
323 if (in_restacking_) | 337 if (in_restacking_) |
324 return; | 338 return; |
325 | 339 |
326 base::AutoReset<bool> auto_reset(&in_restacking_, true); | 340 base::AutoReset<bool> auto_reset(&in_restacking_, true); |
327 WmWindow* container = pinned_window_->GetParent(); | 341 WmWindow* container = pinned_window_->GetParent(); |
328 container->StackChildAtTop(pinned_window_); | 342 container->StackChildAtTop(pinned_window_); |
329 WindowDimmer* pinned_window_dimmer = window_dimmers_->Get(container); | 343 WindowDimmer* pinned_window_dimmer = |
330 if (pinned_window_dimmer && pinned_window_dimmer->window()) | 344 container->aura_window()->GetProperty(kWindowDimmerKey); |
331 container->StackChildBelow(pinned_window_dimmer->window(), pinned_window_); | 345 if (pinned_window_dimmer && pinned_window_dimmer->window()) { |
| 346 container->StackChildBelow(WmWindow::Get(pinned_window_dimmer->window()), |
| 347 pinned_window_); |
| 348 } |
332 } | 349 } |
333 | 350 |
334 void ScreenPinningController::KeepDimWindowAtBottom(WmWindow* container) { | 351 void ScreenPinningController::KeepDimWindowAtBottom(WmWindow* container) { |
335 if (in_restacking_) | 352 if (in_restacking_) |
336 return; | 353 return; |
337 | 354 |
338 WindowDimmer* window_dimmer = window_dimmers_->Get(container); | 355 WindowDimmer* window_dimmer = |
| 356 container->aura_window()->GetProperty(kWindowDimmerKey); |
339 if (window_dimmer) { | 357 if (window_dimmer) { |
340 base::AutoReset<bool> auto_reset(&in_restacking_, true); | 358 base::AutoReset<bool> auto_reset(&in_restacking_, true); |
341 container->StackChildAtBottom(window_dimmer->window()); | 359 container->StackChildAtBottom(WmWindow::Get(window_dimmer->window())); |
342 } | 360 } |
343 } | 361 } |
344 | 362 |
345 } // namespace ash | 363 } // namespace ash |
OLD | NEW |