layer_animation_observer.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #ifndef UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
  5. #define UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_
  6. #include <map>
  7. #include <set>
  8. #include "base/memory/weak_ptr.h"
  9. #include "ui/compositor/compositor_export.h"
  10. #include "ui/compositor/layer_animation_element.h"
  11. namespace ui {
  12. namespace test {
  13. class CountCheckingLayerAnimationObserver;
  14. class LayerAnimationObserverTestApi;
  15. } // namespace test
  16. class LayerAnimationSequence;
  17. class ScopedLayerAnimationSettings;
  18. class ImplicitAnimationObserver;
  19. // LayerAnimationObservers are notified when animations complete.
  20. class COMPOSITOR_EXPORT LayerAnimationObserver {
  21. public:
  22. // Called when the |sequence| starts.
  23. virtual void OnLayerAnimationStarted(LayerAnimationSequence* sequence);
  24. // Called when the |sequence| ends. Not called if |sequence| is aborted.
  25. virtual void OnLayerAnimationEnded(
  26. LayerAnimationSequence* sequence) = 0;
  27. // Called when a |sequence| repetition ends and will repeat. Not called if
  28. // |sequence| is aborted.
  29. virtual void OnLayerAnimationWillRepeat(LayerAnimationSequence* sequence) {}
  30. // Called if |sequence| is aborted for any reason. Should never do anything
  31. // that may cause another animation to be started.
  32. virtual void OnLayerAnimationAborted(
  33. LayerAnimationSequence* sequence) = 0;
  34. // Called when the animation is scheduled.
  35. virtual void OnLayerAnimationScheduled(
  36. LayerAnimationSequence* sequence) = 0;
  37. protected:
  38. typedef std::set<LayerAnimationSequence*> AttachedSequences;
  39. LayerAnimationObserver();
  40. virtual ~LayerAnimationObserver();
  41. // If the LayerAnimator is destroyed during an animation, the animations are
  42. // aborted. The resulting NotifyAborted notifications will NOT be sent to
  43. // this observer if this function returns false. An observer who wants to
  44. // receive the NotifyAborted notifications during destruction can override
  45. // this function to return true.
  46. //
  47. // *** IMPORTANT ***: If a class overrides this function to return true and
  48. // that class is a direct or indirect owner of the LayerAnimationSequence
  49. // being observed, then the class must explicitly remove itself as an
  50. // observer during destruction of the LayerAnimationObserver! This is to
  51. // ensure that a partially destroyed observer isn't notified with an
  52. // OnLayerAnimationAborted() call when the LayerAnimator is destroyed.
  53. //
  54. // This opt-in pattern is used because it is common for a class to be the
  55. // observer of a LayerAnimationSequence that it owns indirectly because it
  56. // owns the Layer which owns the LayerAnimator which owns the
  57. // LayerAnimationSequence.
  58. virtual bool RequiresNotificationWhenAnimatorDestroyed() const;
  59. // Called when |this| is added to |sequence|'s observer list.
  60. virtual void OnAttachedToSequence(LayerAnimationSequence* sequence);
  61. // Called when |this| is removed to |sequence|'s observer list.
  62. virtual void OnDetachedFromSequence(LayerAnimationSequence* sequence);
  63. // Called when the relevant animator attaches to an animation timeline.
  64. virtual void OnAnimatorAttachedToTimeline() {}
  65. // Called when the relevant animator detaches from an animation timeline.
  66. virtual void OnAnimatorDetachedFromTimeline() {}
  67. // Detaches this observer from all sequences it is currently observing.
  68. void StopObserving();
  69. const AttachedSequences& attached_sequences() const {
  70. return attached_sequences_;
  71. }
  72. private:
  73. friend class LayerAnimationSequence;
  74. friend class test::CountCheckingLayerAnimationObserver;
  75. friend class test::LayerAnimationObserverTestApi;
  76. // Called when |this| is added to |sequence|'s observer list.
  77. void AttachedToSequence(LayerAnimationSequence* sequence);
  78. // Called when |this| is removed to |sequence|'s observer list.
  79. // This will only result in notifications if |send_notification| is true.
  80. void DetachedFromSequence(LayerAnimationSequence* sequence,
  81. bool send_notification);
  82. AttachedSequences attached_sequences_;
  83. };
  84. // An implicit animation observer is intended to be used in conjunction with a
  85. // ScopedLayerAnimationSettings object in order to receive a notification when
  86. // all implicit animations complete.
  87. // TODO(bruthig): Unify the ImplicitAnimationObserver with the
  88. // CallbackLayerAnimationObserver. (See www.crbug.com/542825).
  89. class COMPOSITOR_EXPORT ImplicitAnimationObserver
  90. : public LayerAnimationObserver {
  91. public:
  92. ImplicitAnimationObserver();
  93. ~ImplicitAnimationObserver() override;
  94. // Called when the first animation sequence has started.
  95. virtual void OnImplicitAnimationsScheduled() {}
  96. virtual void OnImplicitAnimationsCompleted() = 0;
  97. protected:
  98. // Deactivates the observer and clears the collection of animations it is
  99. // waiting for.
  100. void StopObservingImplicitAnimations();
  101. // Returns whether animation for |property| was aborted.
  102. // Note that if the property wasn't animated, then it couldn't have been
  103. // aborted, so this will return false for that property.
  104. bool WasAnimationAbortedForProperty(
  105. LayerAnimationElement::AnimatableProperty property) const;
  106. // Returns whether animation for |property| was completed successfully.
  107. // Note that if the property wasn't animated, then it couldn't have been
  108. // completed, so this will return false for that property.
  109. bool WasAnimationCompletedForProperty(
  110. LayerAnimationElement::AnimatableProperty property) const;
  111. private:
  112. enum AnimationStatus {
  113. ANIMATION_STATUS_UNKNOWN,
  114. ANIMATION_STATUS_COMPLETED,
  115. ANIMATION_STATUS_ABORTED,
  116. };
  117. friend class ScopedLayerAnimationSettings;
  118. // LayerAnimationObserver implementation
  119. void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override;
  120. void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override;
  121. void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override;
  122. void OnAttachedToSequence(LayerAnimationSequence* sequence) override;
  123. void OnDetachedFromSequence(LayerAnimationSequence* sequence) override;
  124. // OnImplicitAnimationsCompleted is not fired unless the observer is active.
  125. bool active() const { return active_; }
  126. void SetActive(bool active);
  127. void CheckCompleted();
  128. void UpdatePropertyAnimationStatus(LayerAnimationSequence* sequence,
  129. AnimationStatus status);
  130. AnimationStatus AnimationStatusForProperty(
  131. LayerAnimationElement::AnimatableProperty property) const;
  132. bool active_ = false;
  133. typedef std::map<LayerAnimationElement::AnimatableProperty,
  134. AnimationStatus> PropertyAnimationStatusMap;
  135. PropertyAnimationStatusMap property_animation_status_;
  136. // True if OnLayerAnimationScheduled() has been called at least once.
  137. bool first_sequence_scheduled_ = false;
  138. // For tracking whether this object has been destroyed. Must be last.
  139. base::WeakPtrFactory<ImplicitAnimationObserver> weak_factory_{this};
  140. };
  141. } // namespace ui
  142. #endif // UI_COMPOSITOR_LAYER_ANIMATION_OBSERVER_H_