OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 5 #ifndef UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
6 #define UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 6 #define UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <deque> | 9 #include <deque> |
| 10 #include <set> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/observer_list.h" |
14 #include "base/time.h" | 16 #include "base/time.h" |
15 #include "ui/base/animation/animation_container_element.h" | 17 #include "ui/base/animation/animation_container_element.h" |
16 #include "ui/gfx/compositor/compositor_export.h" | 18 #include "ui/gfx/compositor/compositor_export.h" |
17 #include "ui/gfx/compositor/layer_animation_element.h" | 19 #include "ui/gfx/compositor/layer_animation_element.h" |
18 | 20 |
19 namespace gfx { | 21 namespace gfx { |
20 class Rect; | 22 class Rect; |
21 } | 23 } |
22 | 24 |
23 namespace ui { | 25 namespace ui { |
24 class Animation; | 26 class Animation; |
25 class Layer; | 27 class Layer; |
26 class LayerAnimationSequence; | 28 class LayerAnimationSequence; |
27 class LayerAnimatorDelegate; | 29 class LayerAnimationDelegate; |
| 30 class LayerAnimationObserver; |
28 class Transform; | 31 class Transform; |
29 | 32 |
30 // When a property of layer needs to be changed it is set by way of | 33 // When a property of layer needs to be changed it is set by way of |
31 // LayerAnimator. This enables LayerAnimator to animate property changes. | 34 // LayerAnimator. This enables LayerAnimator to animate property changes. |
32 class COMPOSITOR_EXPORT LayerAnimator : public AnimationContainerElement { | 35 class COMPOSITOR_EXPORT LayerAnimator : public AnimationContainerElement { |
33 public: | 36 public: |
34 enum PreemptionStrategy { | 37 enum PreemptionStrategy { |
35 IMMEDIATELY_SET_NEW_TARGET, | 38 IMMEDIATELY_SET_NEW_TARGET, |
36 IMMEDIATELY_ANIMATE_TO_NEW_TARGET, | 39 IMMEDIATELY_ANIMATE_TO_NEW_TARGET, |
37 ENQUEUE_NEW_ANIMATION, | 40 ENQUEUE_NEW_ANIMATION, |
(...skipping 17 matching lines...) Expand all Loading... |
55 // Sets the bounds on the delegate. May cause an implicit animation. | 58 // Sets the bounds on the delegate. May cause an implicit animation. |
56 virtual void SetBounds(const gfx::Rect& bounds); | 59 virtual void SetBounds(const gfx::Rect& bounds); |
57 gfx::Rect GetTargetBounds() const; | 60 gfx::Rect GetTargetBounds() const; |
58 | 61 |
59 // Sets the opacity on the delegate. May cause an implicit animation. | 62 // Sets the opacity on the delegate. May cause an implicit animation. |
60 virtual void SetOpacity(float opacity); | 63 virtual void SetOpacity(float opacity); |
61 float GetTargetOpacity() const; | 64 float GetTargetOpacity() const; |
62 | 65 |
63 // Sets the layer animation delegate the animator is associated with. The | 66 // Sets the layer animation delegate the animator is associated with. The |
64 // animator does not own the delegate. | 67 // animator does not own the delegate. |
65 void SetDelegate(LayerAnimatorDelegate* delegate); | 68 void SetDelegate(LayerAnimationDelegate* delegate); |
66 | 69 |
67 // Sets the animation preemption strategy. This determines the behaviour if | 70 // Sets the animation preemption strategy. This determines the behaviour if |
68 // a property is set during an animation. The default is | 71 // a property is set during an animation. The default is |
69 // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below). | 72 // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below). |
70 void set_preemption_strategy(PreemptionStrategy strategy) { | 73 void set_preemption_strategy(PreemptionStrategy strategy) { |
71 preemption_strategy_ = strategy; | 74 preemption_strategy_ = strategy; |
72 } | 75 } |
73 | 76 |
74 // Start an animation sequence. If an animation for the same property is in | 77 // Start an animation sequence. If an animation for the same property is in |
75 // progress, it needs to be interrupted with the new animation. The animator | 78 // progress, it needs to be interrupted with the new animation. The animator |
76 // takes ownership of this animation sequence. | 79 // takes ownership of this animation sequence. |
77 void StartAnimation(LayerAnimationSequence* animation); | 80 void StartAnimation(LayerAnimationSequence* animation); |
78 | 81 |
79 // Schedule an animation to be run when possible. The animator takes ownership | 82 // Schedule an animation to be run when possible. The animator takes ownership |
80 // of this animation sequence. | 83 // of this animation sequence. |
81 void ScheduleAnimation(LayerAnimationSequence* animation); | 84 void ScheduleAnimation(LayerAnimationSequence* animation); |
82 | 85 |
83 // Schedules the animations to be run together. Obviously will no work if | 86 // Schedules the animations to be run together. Obviously will no work if |
84 // they animate any common properties. The animator takes ownership of the | 87 // they animate any common properties. The animator takes ownership of the |
85 // animation sequences. | 88 // animation sequences. |
86 void ScheduleTogether(const std::vector<LayerAnimationSequence*>& animations); | 89 void ScheduleTogether(const std::vector<LayerAnimationSequence*>& animations); |
87 | 90 |
88 // These are cover functions that create sequences for you to wrap the given | 91 // Returns true if there is an animation in the queue (animations remain in |
89 // elements. These sequences are then passed to the corresponding function | 92 // the queue until they complete, so this includes running animations). |
90 // above. | 93 bool is_animating() const { return !animation_queue_.empty(); } |
91 void StartAnimationElement(LayerAnimationElement* element); | |
92 void ScheduleAnimationElement(LayerAnimationElement* element); | |
93 void ScheduleElementsTogether( | |
94 const std::vector<LayerAnimationElement*>& element); | |
95 | 94 |
96 // Returns true if there is an animation in the queue (animations remain in | 95 // Returns true if there is an animation in the queue that animates the given |
97 // the queue until they complete). | 96 // property (animations remain in the queue until they complete, so this |
98 bool is_animating() const { return !animation_queue_.empty(); } | 97 // includes running animations). |
| 98 bool IsAnimatingProperty( |
| 99 LayerAnimationElement::AnimatableProperty property) const; |
99 | 100 |
100 // Stops animating the given property. No effect if there is no running | 101 // Stops animating the given property. No effect if there is no running |
101 // animation for the given property. Skips to the final state of the | 102 // animation for the given property. Skips to the final state of the |
102 // animation. | 103 // animation. |
103 void StopAnimatingProperty( | 104 void StopAnimatingProperty( |
104 LayerAnimationElement::AnimatableProperty property); | 105 LayerAnimationElement::AnimatableProperty property); |
105 | 106 |
106 // Stops all animation and clears any queued animations. | 107 // Stops all animation and clears any queued animations. |
107 void StopAnimating(); | 108 void StopAnimating(); |
108 | 109 |
109 // For testing purposes only. | 110 // For testing purposes only. |
110 void set_disable_timer_for_test(bool enabled) { | 111 void set_disable_timer_for_test(bool enabled) { |
111 disable_timer_for_test_ = enabled; | 112 disable_timer_for_test_ = enabled; |
112 } | 113 } |
113 base::TimeTicks get_last_step_time_for_test() { return last_step_time_; } | 114 base::TimeTicks get_last_step_time_for_test() { return last_step_time_; } |
114 | 115 |
| 116 // These functions are used for adding or removing observers from the observer |
| 117 // list. The observers are notified when animations end. |
| 118 void AddObserver(LayerAnimationObserver* observer); |
| 119 void RemoveObserver(LayerAnimationObserver* observer); |
| 120 |
115 // Scoped settings allow you to temporarily change the animator's settings and | 121 // Scoped settings allow you to temporarily change the animator's settings and |
116 // these changes are reverted when the object is destroyed. NOTE: when the | 122 // these changes are reverted when the object is destroyed. NOTE: when the |
117 // settings object is created, it applies the default transition duration | 123 // settings object is created, it applies the default transition duration |
118 // (200ms). | 124 // (200ms). |
119 class ScopedSettings { | 125 class ScopedSettings { |
120 public: | 126 public: |
121 explicit ScopedSettings(LayerAnimator* animator); | 127 explicit ScopedSettings(LayerAnimator* animator); |
122 virtual ~ScopedSettings(); | 128 virtual ~ScopedSettings(); |
123 | 129 |
| 130 void AddObserver(LayerAnimationObserver* observer); |
124 void SetTransitionDuration(base::TimeDelta duration); | 131 void SetTransitionDuration(base::TimeDelta duration); |
125 | 132 |
126 private: | 133 private: |
127 LayerAnimator* animator_; | 134 LayerAnimator* animator_; |
128 base::TimeDelta old_transition_duration_; | 135 base::TimeDelta old_transition_duration_; |
| 136 std::set<LayerAnimationObserver*> observers_; |
129 | 137 |
130 DISALLOW_COPY_AND_ASSIGN(ScopedSettings); | 138 DISALLOW_COPY_AND_ASSIGN(ScopedSettings); |
131 }; | 139 }; |
132 | 140 |
133 protected: | 141 protected: |
134 LayerAnimatorDelegate* delegate() { return delegate_; } | 142 LayerAnimationDelegate* delegate() { return delegate_; } |
135 | 143 |
136 private: | 144 private: |
137 friend class TransientSettings; | 145 friend class ScopedSettings; |
138 | 146 |
139 // We need to keep track of the start time of every running animation. | 147 // We need to keep track of the start time of every running animation. |
140 struct RunningAnimation { | 148 struct RunningAnimation { |
141 RunningAnimation(LayerAnimationSequence* sequence, | 149 RunningAnimation(LayerAnimationSequence* sequence, |
142 base::TimeTicks start_time) | 150 base::TimeTicks start_time) |
143 : sequence(sequence), | 151 : sequence(sequence), |
144 start_time(start_time) { | 152 start_time(start_time) { |
145 } | 153 } |
146 LayerAnimationSequence* sequence; | 154 LayerAnimationSequence* sequence; |
147 base::TimeTicks start_time; | 155 base::TimeTicks start_time; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 | 212 |
205 // Attempts to add the sequence to the list of running animations. Returns | 213 // Attempts to add the sequence to the list of running animations. Returns |
206 // false if there is an animation running that already animates one of the | 214 // false if there is an animation running that already animates one of the |
207 // properties affected by |sequence|. | 215 // properties affected by |sequence|. |
208 bool StartSequenceImmediately(LayerAnimationSequence* sequence); | 216 bool StartSequenceImmediately(LayerAnimationSequence* sequence); |
209 | 217 |
210 // Sets the value of target as if all the running and queued animations were | 218 // Sets the value of target as if all the running and queued animations were |
211 // allowed to finish. | 219 // allowed to finish. |
212 void GetTargetValue(LayerAnimationElement::TargetValue* target) const; | 220 void GetTargetValue(LayerAnimationElement::TargetValue* target) const; |
213 | 221 |
| 222 // Called whenever an animation is added to the animation queue. Either by |
| 223 // starting the animation or adding to the queue. |
| 224 void OnScheduled(LayerAnimationSequence* sequence); |
| 225 |
214 // This is the queue of animations to run. | 226 // This is the queue of animations to run. |
215 AnimationQueue animation_queue_; | 227 AnimationQueue animation_queue_; |
216 | 228 |
217 // The target of all layer animations. | 229 // The target of all layer animations. |
218 LayerAnimatorDelegate* delegate_; | 230 LayerAnimationDelegate* delegate_; |
219 | 231 |
220 // The currently running animations. | 232 // The currently running animations. |
221 RunningAnimations running_animations_; | 233 RunningAnimations running_animations_; |
222 | 234 |
223 // Determines how animations are replaced. | 235 // Determines how animations are replaced. |
224 PreemptionStrategy preemption_strategy_; | 236 PreemptionStrategy preemption_strategy_; |
225 | 237 |
226 // The default length of animations. | 238 // The default length of animations. |
227 base::TimeDelta transition_duration_; | 239 base::TimeDelta transition_duration_; |
228 | 240 |
229 // Used for coordinating the starting of animations. | 241 // Used for coordinating the starting of animations. |
230 base::TimeTicks last_step_time_; | 242 base::TimeTicks last_step_time_; |
231 | 243 |
232 // True if we are being stepped by our container. | 244 // True if we are being stepped by our container. |
233 bool is_started_; | 245 bool is_started_; |
234 | 246 |
235 // This prevents the animator from automatically stepping through animations | 247 // This prevents the animator from automatically stepping through animations |
236 // and allows for manual stepping. | 248 // and allows for manual stepping. |
237 bool disable_timer_for_test_; | 249 bool disable_timer_for_test_; |
238 | 250 |
| 251 // Observers are notified when layer animations end, are scheduled or are |
| 252 // aborted. |
| 253 ObserverList<LayerAnimationObserver> observers_; |
| 254 |
239 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); | 255 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); |
240 }; | 256 }; |
241 | 257 |
242 } // namespace ui | 258 } // namespace ui |
243 | 259 |
244 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 260 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
OLD | NEW |