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