layer_animation_observer.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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.
4
5#ifndef UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
6#define UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
7
8#include <map>
9#include <set>
10
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"
15
16namespace ui {
17
18class LayerAnimationSequence;
19class ScopedLayerAnimationSettings;
20class ImplicitAnimationObserver;
21
22// LayerAnimationObservers are notified when animations complete.
23class COMPOSITOR_EXPORT LayerAnimationObserver  {
24 public:
25  // Called when the |sequence| ends. Not called if |sequence| is aborted.
26  virtual void OnLayerAnimationEnded(
27      LayerAnimationSequence* sequence) = 0;
28
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;
33
34  // Called when the animation is scheduled.
35  virtual void OnLayerAnimationScheduled(
36      LayerAnimationSequence* sequence) = 0;
37
38 protected:
39  typedef std::set<LayerAnimationSequence*> AttachedSequences;
40
41  LayerAnimationObserver();
42  virtual ~LayerAnimationObserver();
43
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;
50
51  // Called when |this| is added to |sequence|'s observer list.
52  virtual void OnAttachedToSequence(LayerAnimationSequence* sequence);
53
54  // Called when |this| is removed to |sequence|'s observer list.
55  virtual void OnDetachedFromSequence(LayerAnimationSequence* sequence);
56
57  // Detaches this observer from all sequences it is currently observing.
58  void StopObserving();
59
60  const AttachedSequences& attached_sequences() const {
61    return attached_sequences_;
62  }
63
64 private:
65  friend class LayerAnimationSequence;
66
67  // Called when |this| is added to |sequence|'s observer list.
68  void AttachedToSequence(LayerAnimationSequence* sequence);
69
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);
74
75  AttachedSequences attached_sequences_;
76};
77
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.
81class COMPOSITOR_EXPORT ImplicitAnimationObserver
82    : public LayerAnimationObserver {
83 public:
84  ImplicitAnimationObserver();
85  virtual ~ImplicitAnimationObserver();
86
87  // Called when the first animation sequence has started.
88  virtual void OnImplicitAnimationsScheduled() {}
89
90  virtual void OnImplicitAnimationsCompleted() = 0;
91
92 protected:
93  // Deactivates the observer and clears the collection of animations it is
94  // waiting for.
95  void StopObservingImplicitAnimations();
96
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;
102
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;
108
109 private:
110  enum AnimationStatus {
111    ANIMATION_STATUS_UNKNOWN,
112    ANIMATION_STATUS_COMPLETED,
113    ANIMATION_STATUS_ABORTED,
114  };
115
116  friend class ScopedLayerAnimationSettings;
117
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;
129
130  // OnImplicitAnimationsCompleted is not fired unless the observer is active.
131  bool active() const { return active_; }
132  void SetActive(bool active);
133
134  void CheckCompleted();
135
136  void UpdatePropertyAnimationStatus(LayerAnimationSequence* sequence,
137                                     AnimationStatus status);
138  AnimationStatus AnimationStatusForProperty(
139      LayerAnimationElement::AnimatableProperty property) const;
140
141  bool active_;
142
143  // Set to true in the destructor (if non-NULL). Used to detect deletion while
144  // calling out.
145  bool* destroyed_;
146
147  typedef std::map<LayerAnimationElement::AnimatableProperty,
148                   AnimationStatus> PropertyAnimationStatusMap;
149  PropertyAnimationStatusMap property_animation_status_;
150
151  // True if OnLayerAnimationScheduled() has been called at least once.
152  bool first_sequence_scheduled_;
153};
154
155}  // namespace ui
156
157#endif  // UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
158