layer_animation_observer.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 <set>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "ui/compositor/compositor_export.h"
13
14namespace ui {
15
16class LayerAnimationSequence;
17class ScopedLayerAnimationSettings;
18class ImplicitAnimationObserver;
19
20// LayerAnimationObservers are notified when animations complete.
21class COMPOSITOR_EXPORT LayerAnimationObserver  {
22 public:
23  // Called when the |sequence| ends. Not called if |sequence| is aborted.
24  virtual void OnLayerAnimationEnded(
25      LayerAnimationSequence* sequence) = 0;
26
27  // Called if |sequence| is aborted for any reason. Should never do anything
28  // that may cause another animation to be started.
29  virtual void OnLayerAnimationAborted(
30      LayerAnimationSequence* sequence) = 0;
31
32  // Called when the animation is scheduled.
33  virtual void OnLayerAnimationScheduled(
34      LayerAnimationSequence* sequence) = 0;
35
36 protected:
37  typedef std::set<LayerAnimationSequence*> AttachedSequences;
38
39  LayerAnimationObserver();
40  virtual ~LayerAnimationObserver();
41
42  // If the animator is destroyed during an animation, the animations are
43  // aborted. The resulting NotifyAborted notifications will NOT be sent to
44  // this observer if this function returns false. NOTE: IF YOU OVERRIDE THIS
45  // FUNCTION TO RETURN TRUE, YOU MUST REMEMBER TO REMOVE YOURSELF AS AN
46  // OBSERVER WHEN YOU ARE DESTROYED.
47  virtual bool RequiresNotificationWhenAnimatorDestroyed() const;
48
49  // Called when |this| is added to |sequence|'s observer list.
50  virtual void OnAttachedToSequence(LayerAnimationSequence* sequence);
51
52  // Called when |this| is removed to |sequence|'s observer list.
53  virtual void OnDetachedFromSequence(LayerAnimationSequence* sequence);
54
55  // Detaches this observer from all sequences it is currently observing.
56  void StopObserving();
57
58  const AttachedSequences& attached_sequences() const {
59    return attached_sequences_;
60  }
61
62 private:
63  friend class LayerAnimationSequence;
64
65  // Called when |this| is added to |sequence|'s observer list.
66  void AttachedToSequence(LayerAnimationSequence* sequence);
67
68  // Called when |this| is removed to |sequence|'s observer list.
69  // This will only result in notifications if |send_notification| is true.
70  void DetachedFromSequence(LayerAnimationSequence* sequence,
71                            bool send_notification);
72
73  AttachedSequences attached_sequences_;
74};
75
76// An implicit animation observer is intended to be used in conjunction with a
77// ScopedLayerAnimationSettings object in order to receive a notification when
78// all implicit animations complete.
79class COMPOSITOR_EXPORT ImplicitAnimationObserver
80    : public LayerAnimationObserver {
81 public:
82  ImplicitAnimationObserver();
83  virtual ~ImplicitAnimationObserver();
84
85  // Called when the first animation sequence has started.
86  virtual void OnImplicitAnimationsScheduled() {}
87
88  // Do not "delete this" in your implementation.  Consider using something
89  // like MessageLoop::current()->DeleteSoon() instead.
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 private:
98  friend class ScopedLayerAnimationSettings;
99
100  // LayerAnimationObserver implementation
101  virtual void OnLayerAnimationEnded(
102      LayerAnimationSequence* sequence) OVERRIDE;
103  virtual void OnLayerAnimationAborted(
104      LayerAnimationSequence* sequence) OVERRIDE;
105  virtual void OnLayerAnimationScheduled(
106      LayerAnimationSequence* sequence) OVERRIDE;
107  virtual void OnAttachedToSequence(
108      LayerAnimationSequence* sequence) OVERRIDE;
109  virtual void OnDetachedFromSequence(
110      LayerAnimationSequence* sequence) OVERRIDE;
111
112  // OnImplicitAnimationsCompleted is not fired unless the observer is active.
113  bool active() const { return active_; }
114  void SetActive(bool active);
115
116  void CheckCompleted();
117
118  bool active_;
119
120  // Set to true in the destructor (if non-NULL). Used to detect deletion while
121  // calling out.
122  bool* destroyed_;
123
124  // True if OnLayerAnimationScheduled() has been called at least once.
125  bool first_sequence_scheduled_;
126};
127
128}  // namespace ui
129
130#endif  // UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
131