animation_sequence_block.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2021 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_VIEWS_ANIMATION_ANIMATION_SEQUENCE_BLOCK_H_
  5. #define UI_VIEWS_ANIMATION_ANIMATION_SEQUENCE_BLOCK_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/callback_forward.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/time/time.h"
  11. #include "base/types/pass_key.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #include "third_party/abseil-cpp/absl/types/variant.h"
  14. #include "third_party/skia/include/core/SkColor.h"
  15. #include "ui/gfx/geometry/transform.h"
  16. #include "ui/views/animation/animation_key.h"
  17. #include "ui/views/views_export.h"
  18. namespace gfx {
  19. class Rect;
  20. class RoundedCornersF;
  21. class LinearGradient;
  22. } // namespace gfx
  23. namespace ui {
  24. class Layer;
  25. class LayerOwner;
  26. } // namespace ui
  27. namespace views {
  28. class AnimationBuilder;
  29. // An animation sequence block is a single unit of a larger animation sequence,
  30. // which has a start time, duration, and zero or more (target, property)
  31. // animations. There may be multiple properties animating on a single target,
  32. // and/or multiple targets animating, but the same property on the same target
  33. // may only be animated at most once per block. Animations can be added by
  34. // calling SetXXX(). Calling At(), Offset(), or Then() create a new block.
  35. class VIEWS_EXPORT AnimationSequenceBlock {
  36. public:
  37. AnimationSequenceBlock(base::PassKey<AnimationBuilder> builder_key,
  38. AnimationBuilder* owner,
  39. base::TimeDelta start,
  40. bool repeating);
  41. AnimationSequenceBlock(AnimationSequenceBlock&& other) = delete;
  42. AnimationSequenceBlock& operator=(AnimationSequenceBlock&& other) = delete;
  43. ~AnimationSequenceBlock();
  44. // Sets the duration of this block. The duration may be set at most once and
  45. // will be zero if unspecified.
  46. AnimationSequenceBlock& SetDuration(base::TimeDelta duration);
  47. // Adds animation elements to this block. Each (target, property) pair may be
  48. // added at most once.
  49. AnimationSequenceBlock& SetBounds(
  50. ui::Layer* target,
  51. const gfx::Rect& bounds,
  52. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  53. AnimationSequenceBlock& SetBounds(
  54. ui::LayerOwner* target,
  55. const gfx::Rect& bounds,
  56. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  57. AnimationSequenceBlock& SetBrightness(
  58. ui::Layer* target,
  59. float brightness,
  60. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  61. AnimationSequenceBlock& SetBrightness(
  62. ui::LayerOwner* target,
  63. float brightness,
  64. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  65. AnimationSequenceBlock& SetClipRect(
  66. ui::Layer* target,
  67. const gfx::Rect& clip_rect,
  68. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  69. AnimationSequenceBlock& SetClipRect(
  70. ui::LayerOwner* target,
  71. const gfx::Rect& clip_rect,
  72. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  73. AnimationSequenceBlock& SetColor(
  74. ui::Layer* target,
  75. SkColor color,
  76. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  77. AnimationSequenceBlock& SetColor(
  78. ui::LayerOwner* target,
  79. SkColor color,
  80. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  81. AnimationSequenceBlock& SetGrayscale(
  82. ui::Layer* target,
  83. float grayscale,
  84. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  85. AnimationSequenceBlock& SetGrayscale(
  86. ui::LayerOwner* target,
  87. float grayscale,
  88. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  89. AnimationSequenceBlock& SetOpacity(
  90. ui::Layer* target,
  91. float opacity,
  92. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  93. AnimationSequenceBlock& SetOpacity(
  94. ui::LayerOwner* target,
  95. float opacity,
  96. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  97. AnimationSequenceBlock& SetTransform(
  98. ui::Layer* target,
  99. gfx::Transform transform,
  100. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  101. AnimationSequenceBlock& SetTransform(
  102. ui::LayerOwner* target,
  103. gfx::Transform transform,
  104. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  105. AnimationSequenceBlock& SetRoundedCorners(
  106. ui::Layer* target,
  107. const gfx::RoundedCornersF& rounded_corners,
  108. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  109. AnimationSequenceBlock& SetRoundedCorners(
  110. ui::LayerOwner* target,
  111. const gfx::RoundedCornersF& rounded_corners,
  112. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  113. AnimationSequenceBlock& SetGradientMask(
  114. ui::Layer* target,
  115. const gfx::LinearGradient& gradient_mask,
  116. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  117. AnimationSequenceBlock& SetGradientMask(
  118. ui::LayerOwner* target,
  119. const gfx::LinearGradient& gradient_mask,
  120. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  121. AnimationSequenceBlock& SetVisibility(
  122. ui::Layer* target,
  123. bool visible,
  124. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  125. AnimationSequenceBlock& SetVisibility(
  126. ui::LayerOwner* target,
  127. bool visible,
  128. gfx::Tween::Type tween_type = gfx::Tween::LINEAR);
  129. // Creates a new block.
  130. AnimationSequenceBlock& At(base::TimeDelta since_sequence_start);
  131. AnimationSequenceBlock& Offset(base::TimeDelta since_last_block_start);
  132. AnimationSequenceBlock& Then();
  133. private:
  134. using AnimationValue = absl::variant<gfx::Rect,
  135. float,
  136. SkColor,
  137. gfx::RoundedCornersF,
  138. gfx::LinearGradient,
  139. bool,
  140. gfx::Transform>;
  141. // Data for the animation of a given AnimationKey.
  142. struct Element {
  143. Element(AnimationValue animation_value, gfx::Tween::Type tween_type);
  144. ~Element();
  145. Element(Element&&);
  146. Element& operator=(Element&&);
  147. AnimationValue animation_value_;
  148. gfx::Tween::Type tween_type_;
  149. };
  150. AnimationSequenceBlock& AddAnimation(AnimationKey key, Element element);
  151. // Called when the block is ended by At(), EndSequence(), or a variant
  152. // thereof. Converts `elements_` to LayerAnimationElements on the `owner_`.
  153. void TerminateBlock();
  154. base::PassKey<AnimationBuilder> builder_key_;
  155. raw_ptr<AnimationBuilder> owner_;
  156. base::TimeDelta start_;
  157. // The block duration. This will contain nullopt (interpreted as zero) until
  158. // explicitly set by the caller, at which point it may not be reset.
  159. absl::optional<base::TimeDelta> duration_;
  160. // The animation element data for this block. LayerAnimationElements are not
  161. // used directly because they must be created with a duration, whereas blocks
  162. // support setting the duration after creating elements. The conversion is
  163. // done in TerminateBlock().
  164. std::map<AnimationKey, Element> elements_;
  165. // Is this block part of a repeating sequence?
  166. bool repeating_ = false;
  167. // True when this block has been terminated or used to create another block.
  168. // At this point, it's an error to use the block further.
  169. bool finalized_ = false;
  170. };
  171. } // namespace views
  172. #endif // UI_VIEWS_ANIMATION_ANIMATION_SEQUENCE_BLOCK_H_