callback_layer_animation_observer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2015 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_CALLBACK_LAYER_ANIMATION_OBSERVER_H_
  5. #define UI_COMPOSITOR_CALLBACK_LAYER_ANIMATION_OBSERVER_H_
  6. #include "base/callback.h"
  7. #include "ui/compositor/compositor_export.h"
  8. #include "ui/compositor/layer_animation_observer.h"
  9. namespace ui {
  10. class LayerAnimationSequence;
  11. // A LayerAnimationObserver that invokes a Callback when all observed
  12. // LayerAnimationSequence's have started and finished.
  13. //
  14. // Example usage:
  15. // class Foobar {
  16. // // The Callback that will be invoked when all the animation sequences have
  17. // // started.
  18. // void AnimationStartedCallback(
  19. // const CallbackLayerAnimationObserver& observer) {
  20. // // Do stuff.
  21. // }
  22. //
  23. // // The Callback that will be invoked when all the animation sequences have
  24. // // finished.
  25. // bool AnimationEndedCallback(
  26. // const CallbackLayerAnimationObserver& observer) {
  27. // // Do stuff.
  28. // return true; // Returns true so that |observer| destroys itself.
  29. // }
  30. //
  31. // // Example method that uses the CallbackLayerAnimationObserver.
  32. // void Animate() {
  33. // ui::LayerAnimator* animator_1 = layer_1->GetAnimator();
  34. // ui::LayerAnimator* animator_2 = layer_2->GetAnimator();
  35. // CallbackLayerAnimationObserver* observer =
  36. // new CallbackLayerAnimationObserver(
  37. // base::BindRepeating(&Foobar::AnimationStartedCallback),
  38. // base::Unretained(this));
  39. // base::BindRepeating(&Foobar::AnimationEndedCallback),
  40. // base::Unretained(this));
  41. // animator_1->AddObserver(observer);
  42. // animator_2->AddObserver(observer);
  43. //
  44. // // Set up animation sequences on |animator_1| and |animator_2|.
  45. //
  46. // // The callback won't be invoked until SetActive() is called.
  47. // observer->SetActive();
  48. // }
  49. // }
  50. //
  51. // TODO(bruthig): Unify the CallbackLayerAnimationObserver with the
  52. // ImplicitAnimationObserver. (See www.crbug.com/542825).
  53. class COMPOSITOR_EXPORT CallbackLayerAnimationObserver
  54. : public ui::LayerAnimationObserver {
  55. public:
  56. // The Callback type that will be invoked when all animation sequences have
  57. // been started.
  58. using AnimationStartedCallback =
  59. base::RepeatingCallback<void(const CallbackLayerAnimationObserver&)>;
  60. // The Callback type that will be invoked when all animation sequences have
  61. // finished. |this| will be destroyed after invoking the Callback if it
  62. // returns true.
  63. using AnimationEndedCallback =
  64. base::RepeatingCallback<bool(const CallbackLayerAnimationObserver&)>;
  65. // A dummy no-op AnimationStartedCallback.
  66. static void DummyAnimationStartedCallback(
  67. const CallbackLayerAnimationObserver&);
  68. // A dummy no-op AnimationEndedCallback that will return
  69. // |should_delete_observer|.
  70. //
  71. // Example usage:
  72. //
  73. // ui::CallbackLayerAnimationObserver* animation_observer =
  74. // new ui::CallbackLayerAnimationObserver(
  75. // base::BindRepeating(&ui::CallbackLayerAnimationObserver::
  76. // DummyAnimationStartedCallback),
  77. // base::BindRepeating(&ui::CallbackLayerAnimationObserver::
  78. // DummyAnimationEndedCallback, false));
  79. static bool DummyAnimationEndedCallback(
  80. bool should_delete_observer,
  81. const CallbackLayerAnimationObserver&);
  82. // Create an instance that will invoke the |animation_started_callback| when
  83. // the animation has started and the |animation_ended_callback| when the
  84. // animation has ended.
  85. CallbackLayerAnimationObserver(
  86. AnimationStartedCallback animation_started_callback,
  87. AnimationEndedCallback animation_ended_callback);
  88. // Create an instance that will invoke the |animation_started_callback| when
  89. // the animation has started and will delete |this| when the animations have
  90. // ended if |should_delete_observer| is true.
  91. CallbackLayerAnimationObserver(
  92. AnimationStartedCallback animation_started_callback,
  93. bool should_delete_observer);
  94. // Create an instance that will invoke the |animation_ended_callback| when the
  95. // animations have ended.
  96. explicit CallbackLayerAnimationObserver(
  97. AnimationEndedCallback animation_ended_callback);
  98. CallbackLayerAnimationObserver(const CallbackLayerAnimationObserver&) =
  99. delete;
  100. CallbackLayerAnimationObserver& operator=(
  101. const CallbackLayerAnimationObserver&) = delete;
  102. ~CallbackLayerAnimationObserver() override;
  103. bool active() const { return active_; }
  104. // The callbacks will not be invoked until SetActive() has been called. This
  105. // allows each sequence to be attached before checking if the sequences have
  106. // finished.
  107. void SetActive();
  108. int aborted_count() const { return aborted_count_; }
  109. int successful_count() const { return successful_count_; }
  110. // ui::LayerAnimationObserver:
  111. void OnLayerAnimationStarted(ui::LayerAnimationSequence* sequence) override;
  112. void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override;
  113. void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override;
  114. void OnLayerAnimationScheduled(ui::LayerAnimationSequence* sequence) override;
  115. protected:
  116. // ui::LayerAnimationObserver:
  117. bool RequiresNotificationWhenAnimatorDestroyed() const override;
  118. void OnAttachedToSequence(ui::LayerAnimationSequence* sequence) override;
  119. void OnDetachedFromSequence(ui::LayerAnimationSequence* sequence) override;
  120. private:
  121. int GetNumSequencesCompleted();
  122. // Checks if all attached sequences have been started and invokes
  123. // |animation_started_callback_| if |active_| is true.
  124. void CheckAllSequencesStarted();
  125. // Checks if all attached sequences have completed and invokes
  126. // |animation_ended_callback_| if |active_| is true.
  127. void CheckAllSequencesCompleted();
  128. // Allows the callbacks to be invoked when true.
  129. bool active_ = false;
  130. // The total number of animation sequences that have been attached.
  131. int attached_sequence_count_ = 0;
  132. // The total number of animation sequences that have been detached.
  133. int detached_sequence_count_ = 0;
  134. // The number of animation sequences that have been started.
  135. int started_count_ = 0;
  136. // The number of animation sequences that were aborted.
  137. int aborted_count_ = 0;
  138. // The number of animation sequences that completed successfully.
  139. int successful_count_ = 0;
  140. // The callback to invoke once all the animation sequences have been started.
  141. AnimationStartedCallback animation_started_callback_;
  142. // The callback to invoke once all the animation sequences have finished.
  143. AnimationEndedCallback animation_ended_callback_;
  144. // Used to detect deletion while calling out.
  145. base::WeakPtrFactory<CallbackLayerAnimationObserver> weak_factory_{this};
  146. };
  147. } // namespace ui
  148. #endif // UI_COMPOSITOR_CALLBACK_LAYER_ANIMATION_OBSERVER_H_