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