OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ | |
6 #define ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ | |
7 | |
8 #include <memory> | |
9 #include <vector> | |
10 | |
11 #include "ash/ash_export.h" | |
12 #include "ash/common/shelf/wm_shelf_observer.h" | |
13 #include "ash/common/wallpaper/wallpaper_controller_observer.h" | |
14 #include "ash/public/cpp/shelf_types.h" | |
15 #include "base/macros.h" | |
16 #include "base/observer_list.h" | |
17 #include "third_party/skia/include/core/SkColor.h" | |
18 #include "ui/gfx/animation/animation_delegate.h" | |
19 | |
20 namespace gfx { | |
21 class SlideAnimation; | |
22 } // namespace gfx | |
23 | |
24 namespace ash { | |
25 | |
26 enum class AnimationChangeType; | |
27 class ShelfBackgroundAnimatorObserver; | |
28 class ShelfBackgroundAnimatorTestApi; | |
29 class WallpaperController; | |
30 class WmShelf; | |
31 | |
32 // Central controller for the Shelf and Dock opacity animations. | |
33 // | |
34 // The ShelfBackgroundAnimator is capable of observing a WmShelf instance for | |
35 // background type changes or clients can call PaintBackground() directly. | |
36 // | |
37 // The Shelf uses 2 surfaces for the animations: | |
38 // | |
39 // Material Design: | |
40 // 1. Shelf button backgrounds | |
41 // 2. Overlay for the SHELF_BACKGROUND_OVERLAP and SHELF_BACKGROUND_MAXIMIZED | |
42 // states. | |
43 class ASH_EXPORT ShelfBackgroundAnimator : public WmShelfObserver, | |
44 public gfx::AnimationDelegate, | |
45 public WallpaperControllerObserver { | |
46 public: | |
47 // The maximum alpha value that can be used. | |
48 static const int kMaxAlpha = SK_AlphaOPAQUE; | |
49 | |
50 // Initializes this with the given |background_type|. This will observe the | |
51 // |wm_shelf| for background type changes and the |wallpaper_controller| for | |
52 // wallpaper changes if not null. | |
53 ShelfBackgroundAnimator(ShelfBackgroundType background_type, | |
54 WmShelf* wm_shelf, | |
55 WallpaperController* wallpaper_controller); | |
56 ~ShelfBackgroundAnimator() override; | |
57 | |
58 ShelfBackgroundType target_background_type() const { | |
59 return target_background_type_; | |
60 } | |
61 | |
62 // Initializes |observer| with current values using the Initialize() function. | |
63 void AddObserver(ShelfBackgroundAnimatorObserver* observer); | |
64 void RemoveObserver(ShelfBackgroundAnimatorObserver* observer); | |
65 | |
66 // Updates |observer| with current values. | |
67 void NotifyObserver(ShelfBackgroundAnimatorObserver* observer); | |
68 | |
69 // Conditionally animates the background to the specified |background_type| | |
70 // and notifies observers of the new background parameters (e.g. color). | |
71 // If |change_type| is BACKGROUND_CHANGE_IMMEDIATE then the | |
72 // observers will only receive one notification with the final background | |
73 // state, otherwise the observers will be notified multiple times in order to | |
74 // animate the changes to the backgrounds. | |
75 // | |
76 // NOTE: If a second request to paint the same |background_type| using the | |
77 // BACKGROUND_CHANGE_ANIMATE change type is received it will be ignored and | |
78 // observers will NOT be notified. | |
79 void PaintBackground(ShelfBackgroundType background_type, | |
80 AnimationChangeType change_type); | |
81 | |
82 // gfx::AnimationDelegate: | |
83 void AnimationProgressed(const gfx::Animation* animation) override; | |
84 void AnimationEnded(const gfx::Animation* animation) override; | |
85 void AnimationCanceled(const gfx::Animation* animation) override; | |
86 | |
87 protected: | |
88 // WmShelfObserver: | |
89 void OnBackgroundTypeChanged(ShelfBackgroundType background_type, | |
90 AnimationChangeType change_type) override; | |
91 | |
92 // WallpaperControllerObserver: | |
93 void OnWallpaperDataChanged() override; | |
94 void OnWallpaperColorsChanged() override; | |
95 | |
96 private: | |
97 friend class ShelfBackgroundAnimatorTestApi; | |
98 | |
99 // Track the values related to a single animation (e.g. Shelf background, | |
100 // shelf item background) | |
101 class AnimationValues { | |
102 public: | |
103 AnimationValues(); | |
104 ~AnimationValues(); | |
105 | |
106 SkColor current_color() const { return current_color_; } | |
107 SkColor target_color() const { return target_color_; } | |
108 | |
109 // Updates the |current_color_| based on |t| value between 0 and 1. | |
110 void UpdateCurrentValues(double t); | |
111 | |
112 // Set the target color and assign the current color to the initial color. | |
113 void SetTargetValues(SkColor target_color); | |
114 | |
115 // Returns true if the initial values of |this| equal the target values of | |
116 // |other|. | |
117 bool InitialValuesEqualTargetValuesOf(const AnimationValues& other) const; | |
118 | |
119 private: | |
120 SkColor initial_color_ = SK_ColorTRANSPARENT; | |
121 SkColor current_color_ = SK_ColorTRANSPARENT; | |
122 SkColor target_color_ = SK_ColorTRANSPARENT; | |
123 | |
124 DISALLOW_COPY_AND_ASSIGN(AnimationValues); | |
125 }; | |
126 | |
127 // Helper function used by PaintBackground() to animate the background. | |
128 void AnimateBackground(ShelfBackgroundType background_type, | |
129 AnimationChangeType change_type); | |
130 | |
131 // Returns true if the current |animator_| should be re-used to animate to the | |
132 // given |background_type|. This allows for pre-empted animations to take the | |
133 // same amount of time to reverse to the |previous_background_type_|. | |
134 bool CanReuseAnimator(ShelfBackgroundType background_type) const; | |
135 | |
136 // Creates a new |animator_| configured with the correct duration. If the | |
137 // |animator_| is currently animating it will be stopped. | |
138 void CreateAnimator(ShelfBackgroundType background_type); | |
139 | |
140 // Stops the animator owned by this. | |
141 void StopAnimator(); | |
142 | |
143 // Updates the |shelf_background_values_| and |shelf_item_background_values_|. | |
144 void SetTargetValues(ShelfBackgroundType background_type); | |
145 | |
146 // Sets the target values for |shelf_background_values| and | |
147 // |item_background_values| according to |background_type|. | |
148 void GetTargetValues(ShelfBackgroundType background_type, | |
149 AnimationValues* shelf_background_values, | |
150 AnimationValues* item_background_values) const; | |
151 | |
152 // Updates the animation values corresponding to the |t| value between 0 and | |
153 // 1. | |
154 void SetAnimationValues(double t); | |
155 | |
156 // Called when observers need to be notified. | |
157 void NotifyObservers(); | |
158 | |
159 // The shelf to observe for changes to the shelf background type, can be null. | |
160 WmShelf* wm_shelf_; | |
161 | |
162 // The wallpaper controller to observe for changes and to extract colors from. | |
163 WallpaperController* wallpaper_controller_; | |
164 | |
165 // The background type that this is animating towards or has reached. | |
166 ShelfBackgroundType target_background_type_ = SHELF_BACKGROUND_DEFAULT; | |
167 | |
168 // The last background type this is animating away from. | |
169 ShelfBackgroundType previous_background_type_ = SHELF_BACKGROUND_MAXIMIZED; | |
170 | |
171 // Drives the animtion. | |
172 std::unique_ptr<gfx::SlideAnimation> animator_; | |
173 | |
174 // Tracks the shelf background animation values. | |
175 AnimationValues shelf_background_values_; | |
176 | |
177 // Tracks the item background animation values. | |
178 AnimationValues item_background_values_; | |
179 | |
180 base::ObserverList<ShelfBackgroundAnimatorObserver> observers_; | |
181 | |
182 DISALLOW_COPY_AND_ASSIGN(ShelfBackgroundAnimator); | |
183 }; | |
184 | |
185 } // namespace ash | |
186 | |
187 #endif // ASH_COMMON_SHELF_SHELF_BACKGROUND_ANIMATOR_H_ | |
OLD | NEW |