| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_dimmer.h" | 5 #include "ash/wm/screen_dimmer.h" |
| 6 | 6 |
| 7 #include "ash/aura/wm_window_aura.h" | 7 #include "ash/common/shell_window_ids.h" |
| 8 #include "ash/common/wm/container_finder.h" | 8 #include "ash/common/wm/container_finder.h" |
| 9 #include "ash/common/wm/window_dimmer.h" | 9 #include "ash/common/wm/window_dimmer.h" |
| 10 #include "ash/common/wm_shell.h" | 10 #include "ash/common/wm_shell.h" |
| 11 #include "ash/common/wm_window.h" | 11 #include "ash/common/wm_window.h" |
| 12 #include "ash/common/wm_window_user_data.h" | 12 #include "ash/common/wm_window_user_data.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "ui/aura/window.h" | |
| 15 #include "ui/aura/window_property.h" | |
| 16 | |
| 17 DECLARE_WINDOW_PROPERTY_TYPE(ash::ScreenDimmer*); | |
| 18 | 14 |
| 19 namespace ash { | 15 namespace ash { |
| 20 namespace { | 16 namespace { |
| 21 DEFINE_OWNED_WINDOW_PROPERTY_KEY(ScreenDimmer, kScreenDimmerKey, nullptr); | |
| 22 | 17 |
| 23 // Opacity when it's dimming the entire screen. | 18 // Opacity when it's dimming the entire screen. |
| 24 const float kDimmingLayerOpacityForRoot = 0.4f; | 19 const float kDimmingLayerOpacityForRoot = 0.4f; |
| 25 | 20 |
| 26 // Id used to indicate the root window. | 21 // Opacity for lock screen. |
| 27 const int kRootWindowMagicId = -100; | 22 const float kDimmingLayerOpacityForLockScreen = 0.5f; |
| 28 | |
| 29 std::vector<WmWindow*> GetAllContainers(int container_id) { | |
| 30 return container_id == kRootWindowMagicId | |
| 31 ? WmShell::Get()->GetAllRootWindows() | |
| 32 : wm::GetContainersFromAllRootWindows(container_id); | |
| 33 } | |
| 34 | |
| 35 WmWindow* FindContainer(int container_id) { | |
| 36 WmWindow* primary = WmShell::Get()->GetPrimaryRootWindow(); | |
| 37 return container_id == kRootWindowMagicId | |
| 38 ? primary | |
| 39 : primary->GetChildByShellWindowId(container_id); | |
| 40 } | |
| 41 | 23 |
| 42 } // namespace | 24 } // namespace |
| 43 | 25 |
| 44 // static | 26 ScreenDimmer::ScreenDimmer(Container container) |
| 45 ScreenDimmer* ScreenDimmer::GetForContainer(int container_id) { | 27 : container_(container), |
| 46 aura::Window* primary_container = | |
| 47 WmWindowAura::GetAuraWindow(FindContainer(container_id)); | |
| 48 ScreenDimmer* dimmer = primary_container->GetProperty(kScreenDimmerKey); | |
| 49 if (!dimmer) { | |
| 50 dimmer = new ScreenDimmer(container_id); | |
| 51 primary_container->SetProperty(kScreenDimmerKey, dimmer); | |
| 52 } | |
| 53 return dimmer; | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 ScreenDimmer* ScreenDimmer::GetForRoot() { | |
| 58 ScreenDimmer* dimmer = GetForContainer(kRootWindowMagicId); | |
| 59 // Root window's dimmer | |
| 60 dimmer->target_opacity_ = kDimmingLayerOpacityForRoot; | |
| 61 return dimmer; | |
| 62 } | |
| 63 | |
| 64 ScreenDimmer::ScreenDimmer(int container_id) | |
| 65 : container_id_(container_id), | |
| 66 target_opacity_(0.5f), | |
| 67 is_dimming_(false), | 28 is_dimming_(false), |
| 68 at_bottom_(false), | 29 at_bottom_(false), |
| 69 window_dimmers_(base::MakeUnique<WmWindowUserData<WindowDimmer>>()) { | 30 window_dimmers_(base::MakeUnique<WmWindowUserData<WindowDimmer>>()) { |
| 70 WmShell::Get()->AddShellObserver(this); | 31 WmShell::Get()->AddShellObserver(this); |
| 71 } | 32 } |
| 72 | 33 |
| 73 ScreenDimmer::~ScreenDimmer() { | 34 ScreenDimmer::~ScreenDimmer() { |
| 74 WmShell::Get()->RemoveShellObserver(this); | 35 WmShell::Get()->RemoveShellObserver(this); |
| 75 } | 36 } |
| 76 | 37 |
| 77 void ScreenDimmer::SetDimming(bool should_dim) { | 38 void ScreenDimmer::SetDimming(bool should_dim) { |
| 78 if (should_dim == is_dimming_) | 39 if (should_dim == is_dimming_) |
| 79 return; | 40 return; |
| 80 is_dimming_ = should_dim; | 41 is_dimming_ = should_dim; |
| 81 | 42 |
| 82 Update(should_dim); | 43 Update(should_dim); |
| 83 } | 44 } |
| 84 | 45 |
| 85 // static | 46 std::vector<WmWindow*> ScreenDimmer::GetAllContainers() { |
| 86 ScreenDimmer* ScreenDimmer::FindForTest(int container_id) { | 47 return container_ == Container::ROOT |
| 87 return WmWindowAura::GetAuraWindow(FindContainer(container_id)) | 48 ? WmShell::Get()->GetAllRootWindows() |
| 88 ->GetProperty(kScreenDimmerKey); | 49 : wm::GetContainersFromAllRootWindows( |
| 50 ash::kShellWindowId_LockScreenContainersContainer); |
| 89 } | 51 } |
| 90 | 52 |
| 91 void ScreenDimmer::OnRootWindowAdded(WmWindow* root_window) { | 53 void ScreenDimmer::OnRootWindowAdded(WmWindow* root_window) { |
| 92 Update(is_dimming_); | 54 Update(is_dimming_); |
| 93 } | 55 } |
| 94 | 56 |
| 95 void ScreenDimmer::Update(bool should_dim) { | 57 void ScreenDimmer::Update(bool should_dim) { |
| 96 for (WmWindow* container : GetAllContainers(container_id_)) { | 58 for (WmWindow* container : GetAllContainers()) { |
| 97 WindowDimmer* window_dimmer = window_dimmers_->Get(container); | 59 WindowDimmer* window_dimmer = window_dimmers_->Get(container); |
| 98 if (should_dim) { | 60 if (should_dim) { |
| 99 if (!window_dimmer) { | 61 if (!window_dimmer) { |
| 100 window_dimmers_->Set(container, | 62 window_dimmers_->Set(container, |
| 101 base::MakeUnique<WindowDimmer>(container)); | 63 base::MakeUnique<WindowDimmer>(container)); |
| 102 window_dimmer = window_dimmers_->Get(container); | 64 window_dimmer = window_dimmers_->Get(container); |
| 103 window_dimmer->SetDimOpacity(target_opacity_); | 65 window_dimmer->SetDimOpacity(container_ == Container::ROOT |
| 66 ? kDimmingLayerOpacityForRoot |
| 67 : kDimmingLayerOpacityForLockScreen); |
| 104 } | 68 } |
| 105 if (at_bottom_) | 69 if (at_bottom_) |
| 106 container->StackChildAtBottom(window_dimmer->window()); | 70 container->StackChildAtBottom(window_dimmer->window()); |
| 107 else | 71 else |
| 108 container->StackChildAtTop(window_dimmer->window()); | 72 container->StackChildAtTop(window_dimmer->window()); |
| 109 window_dimmer->window()->Show(); | 73 window_dimmer->window()->Show(); |
| 110 } else if (window_dimmer) { | 74 } else if (window_dimmer) { |
| 111 window_dimmers_->Set(container, nullptr); | 75 window_dimmers_->Set(container, nullptr); |
| 112 } | 76 } |
| 113 } | 77 } |
| 114 } | 78 } |
| 115 | 79 |
| 116 } // namespace ash | 80 } // namespace ash |
| OLD | NEW |