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.
5 #ifndef UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
6 #define UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "ui/compositor/compositor_export.h"
14 #include "ui/compositor/layer_animation_element.h"
18 class LayerAnimationSequence
;
19 class ScopedLayerAnimationSettings
;
20 class ImplicitAnimationObserver
;
22 // LayerAnimationObservers are notified when animations complete.
23 class COMPOSITOR_EXPORT LayerAnimationObserver
{
25 // Called when the |sequence| ends. Not called if |sequence| is aborted.
26 virtual void OnLayerAnimationEnded(
27 LayerAnimationSequence
* sequence
) = 0;
29 // Called if |sequence| is aborted for any reason. Should never do anything
30 // that may cause another animation to be started.
31 virtual void OnLayerAnimationAborted(
32 LayerAnimationSequence
* sequence
) = 0;
34 // Called when the animation is scheduled.
35 virtual void OnLayerAnimationScheduled(
36 LayerAnimationSequence
* sequence
) = 0;
39 typedef std::set
<LayerAnimationSequence
*> AttachedSequences
;
41 LayerAnimationObserver();
42 virtual ~LayerAnimationObserver();
44 // If the animator is destroyed during an animation, the animations are
45 // aborted. The resulting NotifyAborted notifications will NOT be sent to
46 // this observer if this function returns false. NOTE: IF YOU OVERRIDE THIS
47 // FUNCTION TO RETURN TRUE, YOU MUST REMEMBER TO REMOVE YOURSELF AS AN
48 // OBSERVER WHEN YOU ARE DESTROYED.
49 virtual bool RequiresNotificationWhenAnimatorDestroyed() const;
51 // Called when |this| is added to |sequence|'s observer list.
52 virtual void OnAttachedToSequence(LayerAnimationSequence
* sequence
);
54 // Called when |this| is removed to |sequence|'s observer list.
55 virtual void OnDetachedFromSequence(LayerAnimationSequence
* sequence
);
57 // Detaches this observer from all sequences it is currently observing.
60 const AttachedSequences
& attached_sequences() const {
61 return attached_sequences_
;
65 friend class LayerAnimationSequence
;
67 // Called when |this| is added to |sequence|'s observer list.
68 void AttachedToSequence(LayerAnimationSequence
* sequence
);
70 // Called when |this| is removed to |sequence|'s observer list.
71 // This will only result in notifications if |send_notification| is true.
72 void DetachedFromSequence(LayerAnimationSequence
* sequence
,
73 bool send_notification
);
75 AttachedSequences attached_sequences_
;
78 // An implicit animation observer is intended to be used in conjunction with a
79 // ScopedLayerAnimationSettings object in order to receive a notification when
80 // all implicit animations complete.
81 class COMPOSITOR_EXPORT ImplicitAnimationObserver
82 : public LayerAnimationObserver
{
84 ImplicitAnimationObserver();
85 virtual ~ImplicitAnimationObserver();
87 // Called when the first animation sequence has started.
88 virtual void OnImplicitAnimationsScheduled() {}
90 virtual void OnImplicitAnimationsCompleted() = 0;
93 // Deactivates the observer and clears the collection of animations it is
95 void StopObservingImplicitAnimations();
97 // Returns whether animation for |property| was aborted.
98 // Note that if the property wasn't animated, then it couldn't have been
99 // aborted, so this will return false for that property.
100 bool WasAnimationAbortedForProperty(
101 LayerAnimationElement::AnimatableProperty property
) const;
103 // Returns whether animation for |property| was completed successfully.
104 // Note that if the property wasn't animated, then it couldn't have been
105 // completed, so this will return false for that property.
106 bool WasAnimationCompletedForProperty(
107 LayerAnimationElement::AnimatableProperty property
) const;
110 enum AnimationStatus
{
111 ANIMATION_STATUS_UNKNOWN
,
112 ANIMATION_STATUS_COMPLETED
,
113 ANIMATION_STATUS_ABORTED
,
116 friend class ScopedLayerAnimationSettings
;
118 // LayerAnimationObserver implementation
119 virtual void OnLayerAnimationEnded(
120 LayerAnimationSequence
* sequence
) OVERRIDE
;
121 virtual void OnLayerAnimationAborted(
122 LayerAnimationSequence
* sequence
) OVERRIDE
;
123 virtual void OnLayerAnimationScheduled(
124 LayerAnimationSequence
* sequence
) OVERRIDE
;
125 virtual void OnAttachedToSequence(
126 LayerAnimationSequence
* sequence
) OVERRIDE
;
127 virtual void OnDetachedFromSequence(
128 LayerAnimationSequence
* sequence
) OVERRIDE
;
130 // OnImplicitAnimationsCompleted is not fired unless the observer is active.
131 bool active() const { return active_
; }
132 void SetActive(bool active
);
134 void CheckCompleted();
136 void UpdatePropertyAnimationStatus(LayerAnimationSequence
* sequence
,
137 AnimationStatus status
);
138 AnimationStatus
AnimationStatusForProperty(
139 LayerAnimationElement::AnimatableProperty property
) const;
143 // Set to true in the destructor (if non-NULL). Used to detect deletion while
147 typedef std::map
<LayerAnimationElement::AnimatableProperty
,
148 AnimationStatus
> PropertyAnimationStatusMap
;
149 PropertyAnimationStatusMap property_animation_status_
;
151 // True if OnLayerAnimationScheduled() has been called at least once.
152 bool first_sequence_scheduled_
;
157 #endif // UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_