screen_dimmer.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include "ash/wm/screen_dimmer.h"
  5. #include <memory>
  6. #include "ash/public/cpp/shell_window_ids.h"
  7. #include "ash/shell.h"
  8. #include "ash/window_user_data.h"
  9. #include "ash/wm/container_finder.h"
  10. #include "ash/wm/window_dimmer.h"
  11. #include "ui/aura/window.h"
  12. namespace ash {
  13. namespace {
  14. // Opacity when it's dimming the entire screen.
  15. const float kDimmingLayerOpacityForRoot = 0.4f;
  16. // Opacity for lock screen.
  17. const float kDimmingLayerOpacityForLockScreen = 0.5f;
  18. } // namespace
  19. ScreenDimmer::ScreenDimmer(Container container)
  20. : container_(container),
  21. is_dimming_(false),
  22. at_bottom_(false),
  23. window_dimmers_(std::make_unique<WindowUserData<WindowDimmer>>()) {
  24. Shell::Get()->AddShellObserver(this);
  25. }
  26. ScreenDimmer::~ScreenDimmer() {
  27. // Usage in chrome results in ScreenDimmer outliving the shell.
  28. if (Shell::HasInstance())
  29. Shell::Get()->RemoveShellObserver(this);
  30. }
  31. void ScreenDimmer::SetDimming(bool should_dim) {
  32. if (should_dim == is_dimming_)
  33. return;
  34. is_dimming_ = should_dim;
  35. Update(should_dim);
  36. }
  37. aura::Window::Windows ScreenDimmer::GetAllContainers() {
  38. return container_ == Container::ROOT
  39. ? Shell::GetAllRootWindows()
  40. : GetContainersForAllRootWindows(
  41. kShellWindowId_LockScreenContainersContainer);
  42. }
  43. void ScreenDimmer::OnRootWindowAdded(aura::Window* root_window) {
  44. Update(is_dimming_);
  45. }
  46. void ScreenDimmer::Update(bool should_dim) {
  47. for (aura::Window* container : GetAllContainers()) {
  48. WindowDimmer* window_dimmer = window_dimmers_->Get(container);
  49. if (should_dim) {
  50. if (!window_dimmer) {
  51. window_dimmers_->Set(container,
  52. std::make_unique<WindowDimmer>(container));
  53. window_dimmer = window_dimmers_->Get(container);
  54. window_dimmer->SetDimOpacity(container_ == Container::ROOT
  55. ? kDimmingLayerOpacityForRoot
  56. : kDimmingLayerOpacityForLockScreen);
  57. }
  58. if (at_bottom_)
  59. container->StackChildAtBottom(window_dimmer->window());
  60. else
  61. container->StackChildAtTop(window_dimmer->window());
  62. window_dimmer->window()->Show();
  63. } else if (window_dimmer) {
  64. window_dimmers_->Set(container, nullptr);
  65. }
  66. }
  67. }
  68. } // namespace ash