1 // Copyright 2019 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/overview/overview_wallpaper_controller.h"
6 
7 #include "ash/root_window_controller.h"
8 #include "ash/session/session_controller_impl.h"
9 #include "ash/shell.h"
10 #include "ash/wallpaper/wallpaper_constants.h"
11 #include "ash/wallpaper/wallpaper_widget_controller.h"
12 #include "ash/wm/overview/overview_constants.h"
13 #include "ash/wm/overview/overview_controller.h"
14 #include "ash/wm/overview/overview_utils.h"
15 
16 namespace ash {
17 
18 namespace {
19 
20 // Do not change the wallpaper when entering or exiting overview mode when this
21 // is true.
22 bool g_disable_wallpaper_change_for_tests = false;
23 
24 constexpr base::TimeDelta kBlurSlideDuration =
25     base::TimeDelta::FromMilliseconds(250);
26 
IsWallpaperChangeAllowed()27 bool IsWallpaperChangeAllowed() {
28   return !g_disable_wallpaper_change_for_tests;
29 }
30 
GetWallpaperWidgetController(aura::Window * root)31 WallpaperWidgetController* GetWallpaperWidgetController(aura::Window* root) {
32   return RootWindowController::ForWindow(root)->wallpaper_widget_controller();
33 }
34 
35 // Returns the wallpaper blur based on the current configuration.
GetBlur(bool should_blur)36 float GetBlur(bool should_blur) {
37   return should_blur ? wallpaper_constants::kOverviewBlur
38                      : wallpaper_constants::kClear;
39 }
40 
41 }  // namespace
42 
OverviewWallpaperController()43 OverviewWallpaperController::OverviewWallpaperController() {
44   Shell::Get()->tablet_mode_controller()->AddObserver(this);
45 }
46 
~OverviewWallpaperController()47 OverviewWallpaperController::~OverviewWallpaperController() {
48   Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
49 }
50 
51 // static
SetDoNotChangeWallpaperForTests()52 void OverviewWallpaperController::SetDoNotChangeWallpaperForTests() {
53   g_disable_wallpaper_change_for_tests = true;
54 }
55 
Blur(bool animate)56 void OverviewWallpaperController::Blur(bool animate) {
57   UpdateWallpaper(/*should_blur=*/true, animate);
58 }
59 
Unblur()60 void OverviewWallpaperController::Unblur() {
61   UpdateWallpaper(/*should_blur=*/false, /*animate=*/true);
62 }
63 
OnTabletModeStarted()64 void OverviewWallpaperController::OnTabletModeStarted() {
65   UpdateWallpaper(wallpaper_blurred_, /*animate=*/base::nullopt);
66 }
67 
OnTabletModeEnded()68 void OverviewWallpaperController::OnTabletModeEnded() {
69   UpdateWallpaper(wallpaper_blurred_, /*animate=*/base::nullopt);
70 }
71 
UpdateWallpaper(bool should_blur,base::Optional<bool> animate)72 void OverviewWallpaperController::UpdateWallpaper(
73     bool should_blur,
74     base::Optional<bool> animate) {
75   if (!IsWallpaperChangeAllowed())
76     return;
77 
78   // Don't apply wallpaper change while the session is blocked.
79   if (Shell::Get()->session_controller()->IsUserSessionBlocked())
80     return;
81 
82   float blur = GetBlur(should_blur);
83 
84   for (aura::Window* root : Shell::Get()->GetAllRootWindows()) {
85     auto* wallpaper_widget_controller = GetWallpaperWidgetController(root);
86 
87     if (blur == wallpaper_widget_controller->GetWallpaperBlur())
88       continue;
89 
90     if (!animate.has_value()) {
91       wallpaper_widget_controller->SetWallpaperBlur(blur);
92       continue;
93     }
94 
95     const bool should_animate = ShouldAnimateWallpaper(root);
96     // On adding blur, we want to blur immediately if there are no animations
97     // and blur after the rest of the overview animations have completed if
98     // there is to be wallpaper animations. |UpdateWallpaper| will get called
99     // twice when blurring, but only change the wallpaper when |should_animate|
100     // matches |animate|.
101     if (should_blur && should_animate != animate.value())
102       continue;
103 
104     wallpaper_widget_controller->SetWallpaperBlur(
105         blur, should_animate ? kBlurSlideDuration : base::TimeDelta());
106   }
107 
108   wallpaper_blurred_ = should_blur;
109 }
110 
111 }  // namespace ash
112