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 
5 #ifndef ASH_WALLPAPER_WALLPAPER_WIDGET_CONTROLLER_H_
6 #define ASH_WALLPAPER_WALLPAPER_WIDGET_CONTROLLER_H_
7 
8 #include <list>
9 #include <memory>
10 
11 #include "ash/ash_export.h"
12 #include "ash/wallpaper/wallpaper_constants.h"
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "ui/compositor/layer_animation_observer.h"
16 
17 namespace ui {
18 class LayerTreeOwner;
19 }
20 
21 namespace aura {
22 class Window;
23 }
24 
25 namespace views {
26 class Widget;
27 }
28 
29 namespace ash {
30 class WallpaperView;
31 
32 // This class manages widget-based wallpapers.
33 // WallpaperWidgetController is owned by RootWindowController.
34 // Exported for tests.
35 class ASH_EXPORT WallpaperWidgetController
36     : public ui::ImplicitAnimationObserver {
37  public:
38   WallpaperWidgetController(aura::Window* root_window,
39                             base::OnceClosure wallpaper_set_callback);
40   ~WallpaperWidgetController() override;
41 
42   // Initialize the widget. |lock| specifies if the wallpaper should be created
43   // for the locked state.
44   void Init(bool locked);
45 
46   views::Widget* GetWidget();
47 
48   // Whether a wallpaper change is in progress, i.e. |animating_widget_| exists.
49   bool IsAnimating() const;
50 
51   // If an animating wallpaper change is in progress, it ends the animation and
52   // changes the wallpaper immediately. No-op if IsAnimation() returns false.
53   void StopAnimating();
54 
55   // Adds a callback that will be run when the wallpaper animation ends. Used
56   // when you're expecting a wallpaper change (e.g. when IsAnimation() returns
57   // true or you just set a new wallpaper) and want to be notified of the exact
58   // timing that the wallpaper is applied.
59   void AddAnimationEndCallback(base::OnceClosure callback);
60 
61   // Move the wallpaper widget to the specified |container|.
62   // The lock screen moves the wallpaper container to hides the user's windows.
63   // Returns true if there was something to reparent.
64   bool Reparent(int container);
65 
66   // Sets/Gets the blur used to draw wallpaper. |animation_duration| specifies
67   // the animation to apply the change. If its zero duration, then no animation
68   // will be applied.
69   bool SetWallpaperBlur(
70       float blur,
71       const base::TimeDelta& animation_duration = base::TimeDelta());
72   float GetWallpaperBlur() const;
73 
74   // ui::ImplicitAnimationObserver:
75   void OnImplicitAnimationsCompleted() override;
76 
wallpaper_view()77   WallpaperView* wallpaper_view() { return wallpaper_view_; }
78 
old_layer_tree_owner_for_testing()79   ui::LayerTreeOwner* old_layer_tree_owner_for_testing() {
80     return old_layer_tree_owner_.get();
81   }
82 
83  private:
84   // Runs callbacks in |animation_end_callbacks_|.
85   void RunAnimationEndCallbacks();
86 
87   // Copies and fades out the existing wallpaper.
88   void ApplyCrossFadeAnimation(base::TimeDelta duration);
89 
90   aura::Window* root_window_;
91 
92   // Callback that will be run when |active_widget_| is first set.
93   base::OnceClosure wallpaper_set_callback_;
94 
95   // The current wallpaper widget.
96   std::unique_ptr<views::Widget> widget_;
97 
98   // The animating layer which contains old content. This is the layer that is
99   // animated when changing wallpapers.
100   std::unique_ptr<ui::LayerTreeOwner> old_layer_tree_owner_;
101 
102   // Pointer to the wallpaper view owned by |widget_|.
103   WallpaperView* wallpaper_view_ = nullptr;
104 
105   // Callbacks to be run when the |animating_widget_| stops animating and gets
106   // set as the active widget.
107   std::list<base::OnceClosure> animation_end_callbacks_;
108 
109   DISALLOW_COPY_AND_ASSIGN(WallpaperWidgetController);
110 };
111 
112 }  // namespace ash
113 
114 #endif  // ASH_WALLPAPER_WALLPAPER_WIDGET_CONTROLLER_H_
115