layer_animation_observer.h revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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  // Do not "delete this" in your implementation.  Consider using something
91  // like MessageLoop::current()->DeleteSoon() instead.
92  virtual void OnImplicitAnimationsCompleted() = 0;
93
94 protected:
95  // Deactivates the observer and clears the collection of animations it is
96  // waiting for.
97  void StopObservingImplicitAnimations();
98
99  // Returns whether animation for |property| was aborted.
100  // Note that if the property wasn't animated, then it couldn't have been
101  // aborted, so this will return false for that property.
102  bool WasAnimationAbortedForProperty(
103      LayerAnimationElement::AnimatableProperty property) const;
104
105  // Returns whether animation for |property| was completed successfully.
106  // Note that if the property wasn't animated, then it couldn't have been
107  // completed, so this will return false for that property.
108  bool WasAnimationCompletedForProperty(
109      LayerAnimationElement::AnimatableProperty property) const;
110
111 private:
112  enum AnimationStatus {
113    ANIMATION_STATUS_UNKNOWN,
114    ANIMATION_STATUS_COMPLETED,
115    ANIMATION_STATUS_ABORTED,
116  };
117
118  friend class ScopedLayerAnimationSettings;
119
120  // LayerAnimationObserver implementation
121  virtual void OnLayerAnimationEnded(
122      LayerAnimationSequence* sequence) OVERRIDE;
123  virtual void OnLayerAnimationAborted(
124      LayerAnimationSequence* sequence) OVERRIDE;
125  virtual void OnLayerAnimationScheduled(
126      LayerAnimationSequence* sequence) OVERRIDE;
127  virtual void OnAttachedToSequence(
128      LayerAnimationSequence* sequence) OVERRIDE;
129  virtual void OnDetachedFromSequence(
130      LayerAnimationSequence* sequence) OVERRIDE;
131
132  // OnImplicitAnimationsCompleted is not fired unless the observer is active.
133  bool active() const { return active_; }
134  void SetActive(bool active);
135
136  void CheckCompleted();
137
138  void UpdatePropertyAnimationStatus(LayerAnimationSequence* sequence,
139                                     AnimationStatus status);
140  AnimationStatus AnimationStatusForProperty(
141      LayerAnimationElement::AnimatableProperty property) const;
142
143  bool active_;
144
145  // Set to true in the destructor (if non-NULL). Used to detect deletion while
146  // calling out.
147  bool* destroyed_;
148
149  typedef std::map<LayerAnimationElement::AnimatableProperty,
150                   AnimationStatus> PropertyAnimationStatusMap;
151  PropertyAnimationStatusMap property_animation_status_;
152
153  // True if OnLayerAnimationScheduled() has been called at least once.
154  bool first_sequence_scheduled_;
155};
156
157}  // namespace ui
158
159#endif  // UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
160