progress_indicator_animation.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2022 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 ASH_SYSTEM_PROGRESS_INDICATOR_PROGRESS_INDICATOR_ANIMATION_H_
  5. #define ASH_SYSTEM_PROGRESS_INDICATOR_PROGRESS_INDICATOR_ANIMATION_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "base/callback_list.h"
  9. #include "base/time/time.h"
  10. #include "ui/gfx/animation/animation_delegate.h"
  11. namespace gfx {
  12. class SlideAnimation;
  13. } // namespace gfx
  14. namespace ash {
  15. // An animation for a `ProgressIndicator`.
  16. class ASH_EXPORT ProgressIndicatorAnimation : public gfx::AnimationDelegate {
  17. public:
  18. ProgressIndicatorAnimation(const ProgressIndicatorAnimation&) = delete;
  19. ProgressIndicatorAnimation& operator=(const ProgressIndicatorAnimation&) =
  20. delete;
  21. ~ProgressIndicatorAnimation() override;
  22. // Adds the specified `callback` to be notified of animation updates. The
  23. // `callback` will continue to receive events so long as both `this` and the
  24. // returned subscription exist.
  25. base::CallbackListSubscription AddAnimationUpdatedCallback(
  26. base::RepeatingClosureList::CallbackType callback);
  27. // Adds the specified `callback` to be notified of animation updates.
  28. // NOTE: Because no subscription is returned by which to remove `callback`,
  29. // this method should only be used where the `callback` is guaranteed to live
  30. // at least as long as `this`. When lifetime is in doubt, prefer the use of
  31. // `AddAnimationUpdatedCallback()`.
  32. void AddUnsafeAnimationUpdatedCallback(
  33. base::RepeatingClosureList::CallbackType callback);
  34. // Immediately starts this animation.
  35. void Start();
  36. // Returns whether this animation has ever run.
  37. bool HasAnimated() const;
  38. // Returns whether this animation is currently running.
  39. bool IsAnimating() const;
  40. // Returns the time at which this animation was `Start()`-ed.
  41. base::TimeTicks start_time() const { return start_time_; }
  42. protected:
  43. ProgressIndicatorAnimation(base::TimeDelta duration, bool is_cyclic);
  44. // Implementing classes should update any desired animatable properties as
  45. // appropriate for the specified animation `fraction`.
  46. virtual void UpdateAnimatableProperties(double fraction) = 0;
  47. private:
  48. // gfx::AnimationDelegate:
  49. void AnimationProgressed(const gfx::Animation* animation) override;
  50. void AnimationEnded(const gfx::Animation* animation) override;
  51. // Immediately start this animation. If `is_cyclic_restart` is `true`, this
  52. // animation is being restarted after completion of a full animation cycle.
  53. void StartInternal(bool is_cyclic_restart);
  54. // The duration for this animation.
  55. const base::TimeDelta duration_;
  56. // Whether this animation should loop on completion.
  57. const bool is_cyclic_;
  58. // The underlying animator which drives animation progress.
  59. std::unique_ptr<gfx::SlideAnimation> animator_;
  60. // The time at which this animation was `Start()`-ed.
  61. base::TimeTicks start_time_;
  62. // The list of callbacks for which to notify animation updates.
  63. base::RepeatingClosureList animation_updated_callback_list_;
  64. base::WeakPtrFactory<ProgressIndicatorAnimation> weak_factory_{this};
  65. };
  66. } // namespace ash
  67. #endif // ASH_SYSTEM_PROGRESS_INDICATOR_PROGRESS_INDICATOR_ANIMATION_H_