progress_indicator_animation_registry.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_REGISTRY_H_
  5. #define ASH_SYSTEM_PROGRESS_INDICATOR_PROGRESS_INDICATOR_ANIMATION_REGISTRY_H_
  6. #include <map>
  7. #include <memory>
  8. #include "ash/ash_export.h"
  9. #include "ash/system/progress_indicator/progress_icon_animation.h"
  10. #include "ash/system/progress_indicator/progress_ring_animation.h"
  11. #include "base/callback_forward.h"
  12. #include "base/callback_list.h"
  13. namespace ash {
  14. // A registry for progress indicator animations.
  15. //
  16. // Since animations are owned by the registry, they can be shared across
  17. // different UI components as well have a lifetime which is decoupled from UI
  18. // component lifetime.
  19. //
  20. // Supported animation types:
  21. // * Progress icon animation - independently drive the animation of properties
  22. // for a progress indicator's inner icon, as opposed to progress ring
  23. // animations which independently drive the animation of properties for a
  24. // progress indicator's outer ring.
  25. // * Progress ring animation - independently drive the animation of properties
  26. // for a progress indicator's outer ring, as opposed to progress icon
  27. // animations which independently drive the animation of properties for a
  28. // progress indicator's inner icon.
  29. class ASH_EXPORT ProgressIndicatorAnimationRegistry {
  30. public:
  31. ProgressIndicatorAnimationRegistry();
  32. ProgressIndicatorAnimationRegistry(
  33. const ProgressIndicatorAnimationRegistry&) = delete;
  34. ProgressIndicatorAnimationRegistry& operator=(
  35. const ProgressIndicatorAnimationRegistry&) = delete;
  36. ~ProgressIndicatorAnimationRegistry();
  37. using ProgressIconAnimationChangedCallbackList =
  38. base::RepeatingCallbackList<void(ProgressIconAnimation*)>;
  39. // Adds the specified `callback` to be notified of changes to the progress
  40. // icon animation associated with the specified `key`. The `callback` will
  41. // continue to receive events so long as both `this` and the returned
  42. // subscription exist.
  43. base::CallbackListSubscription AddProgressIconAnimationChangedCallbackForKey(
  44. const void* key,
  45. ProgressIconAnimationChangedCallbackList::CallbackType callback);
  46. using ProgressRingAnimationChangedCallbackList =
  47. base::RepeatingCallbackList<void(ProgressRingAnimation*)>;
  48. // Adds the specified `callback` to be notified of changes to the progress
  49. // ring animation associated with the specified `key`. The `callback` will
  50. // continue to receive events so long as both `this` and the returned
  51. // subscription exist.
  52. base::CallbackListSubscription AddProgressRingAnimationChangedCallbackForKey(
  53. const void* key,
  54. ProgressRingAnimationChangedCallbackList::CallbackType callback);
  55. // Returns the progress icon animation registered for the specified `key`.
  56. // NOTE: This may return `nullptr` if no such animation is registered.
  57. ProgressIconAnimation* GetProgressIconAnimationForKey(const void* key);
  58. // Returns the progress ring animation registered for the specified `key`.
  59. // NOTE: This may return `nullptr` if no such animation is registered.
  60. ProgressRingAnimation* GetProgressRingAnimationForKey(const void* key);
  61. // Sets and returns the progress icon animation registered for the specified
  62. // `key`. NOTE: `animation` may be `nullptr` to unregister `key`.
  63. ProgressIconAnimation* SetProgressIconAnimationForKey(
  64. const void* key,
  65. std::unique_ptr<ProgressIconAnimation> animation);
  66. // Sets and returns the progress ring animation registered for the specified
  67. // `key`. NOTE: `animation` may be `nullptr` to unregister `key`.
  68. ProgressRingAnimation* SetProgressRingAnimationForKey(
  69. const void* key,
  70. std::unique_ptr<ProgressRingAnimation> animation);
  71. // Erases all animations for all keys.
  72. void EraseAllAnimations();
  73. // Erases all animations for the specified `key`.
  74. void EraseAllAnimationsForKey(const void* key);
  75. // Erases all animations for all keys for which the specified `predicate`
  76. // returns `true`.
  77. void EraseAllAnimationsForKeyIf(
  78. base::RepeatingCallback<bool(const void* key)> predicate);
  79. private:
  80. // Mapping of keys to their associated progress icon animations.
  81. std::map<const void*, std::unique_ptr<ProgressIconAnimation>>
  82. icon_animations_by_key_;
  83. // Mapping of keys to their associated icon animation changed callback lists.
  84. // Whenever an animation for a given key is changed, the callback list for
  85. // that key will be notified.
  86. std::map<const void*, ProgressIconAnimationChangedCallbackList>
  87. icon_animation_changed_callback_lists_by_key_;
  88. // Mapping of keys to their associated progress ring animations.
  89. std::map<const void*, std::unique_ptr<ProgressRingAnimation>>
  90. ring_animations_by_key_;
  91. // Mapping of keys to their associated ring animation changed callback lists.
  92. // Whenever an animation for a given key is changed, the callback list for
  93. // that key will be notified.
  94. std::map<const void*, ProgressRingAnimationChangedCallbackList>
  95. ring_animation_changed_callback_lists_by_key_;
  96. };
  97. } // namespace ash
  98. #endif // ASH_SYSTEM_PROGRESS_INDICATOR_PROGRESS_INDICATOR_ANIMATION_REGISTRY_H_