square_ink_drop_ripple.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2016 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_SQUARE_INK_DROP_RIPPLE_H_
  5. #define UI_VIEWS_ANIMATION_SQUARE_INK_DROP_RIPPLE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback_list.h"
  9. #include "base/time/time.h"
  10. #include "third_party/skia/include/core/SkColor.h"
  11. #include "ui/compositor/layer.h"
  12. #include "ui/compositor/layer_animator.h"
  13. #include "ui/gfx/animation/tween.h"
  14. #include "ui/gfx/geometry/point.h"
  15. #include "ui/gfx/geometry/size.h"
  16. #include "ui/gfx/geometry/transform.h"
  17. #include "ui/views/animation/ink_drop_ripple.h"
  18. #include "ui/views/animation/ink_drop_state.h"
  19. #include "ui/views/views_export.h"
  20. namespace ui {
  21. class Layer;
  22. } // namespace ui
  23. namespace views {
  24. class CircleLayerDelegate;
  25. class RectangleLayerDelegate;
  26. namespace test {
  27. class SquareInkDropRippleTestApi;
  28. } // namespace test
  29. // An ink drop ripple that smoothly animates between a circle and a rounded
  30. // rectangle of different sizes for each of the different InkDropStates. The
  31. // final frame for each InkDropState will be bounded by either a |large_size_|
  32. // rectangle or a |small_size_| rectangle.
  33. //
  34. // The valid InkDropState transitions are defined below:
  35. //
  36. // {All InkDropStates} => HIDDEN
  37. // HIDDEN => ACTION_PENDING
  38. // HIDDEN, ACTION_PENDING => ACTION_TRIGGERED
  39. // ACTION_PENDING => ALTERNATE_ACTION_PENDING
  40. // ALTERNATE_ACTION_PENDING => ALTERNATE_ACTION_TRIGGERED
  41. // {All InkDropStates} => ACTIVATED
  42. // {All InkDropStates} => DEACTIVATED
  43. //
  44. class VIEWS_EXPORT SquareInkDropRipple : public InkDropRipple {
  45. public:
  46. // The shape to use for the ACTIVATED/DEACTIVATED states.
  47. enum class ActivatedShape { kCircle, kRoundedRect };
  48. SquareInkDropRipple(const gfx::Size& large_size,
  49. int large_corner_radius,
  50. const gfx::Size& small_size,
  51. int small_corner_radius,
  52. const gfx::Point& center_point,
  53. SkColor color,
  54. float visible_opacity);
  55. SquareInkDropRipple(const SquareInkDropRipple&) = delete;
  56. SquareInkDropRipple& operator=(const SquareInkDropRipple&) = delete;
  57. ~SquareInkDropRipple() override;
  58. void set_activated_shape(ActivatedShape shape) { activated_shape_ = shape; }
  59. // InkDropRipple:
  60. void SnapToActivated() override;
  61. ui::Layer* GetRootLayer() override;
  62. private:
  63. friend class test::SquareInkDropRippleTestApi;
  64. // Enumeration of the different shapes that compose the ink drop.
  65. enum PaintedShape {
  66. TOP_LEFT_CIRCLE = 0,
  67. TOP_RIGHT_CIRCLE,
  68. BOTTOM_RIGHT_CIRCLE,
  69. BOTTOM_LEFT_CIRCLE,
  70. HORIZONTAL_RECT,
  71. VERTICAL_RECT,
  72. // The total number of shapes, not an actual shape.
  73. PAINTED_SHAPE_COUNT
  74. };
  75. // Returns a human readable string for the |painted_shape| value.
  76. static std::string ToLayerName(PaintedShape painted_shape);
  77. // Type that contains a gfx::Tansform for each of the layers required by the
  78. // ink drop.
  79. typedef gfx::Transform InkDropTransforms[PAINTED_SHAPE_COUNT];
  80. float GetCurrentOpacity() const;
  81. // InkDropRipple:
  82. void AnimateStateChange(InkDropState old_ink_drop_state,
  83. InkDropState new_ink_drop_state) override;
  84. void SetStateToHidden() override;
  85. void AbortAllAnimations() override;
  86. // Sets the |transforms| on all of the shape layers. Note that this does not
  87. // perform any animation.
  88. void SetTransforms(const InkDropTransforms transforms);
  89. // Sets the opacity of the ink drop. Note that this does not perform any
  90. // animation.
  91. void SetOpacity(float opacity);
  92. // Updates all of the Transforms in |transforms_out| for a circle of the given
  93. // |size|.
  94. void CalculateCircleTransforms(const gfx::Size& desired_size,
  95. InkDropTransforms* transforms_out) const;
  96. // Updates all of the Transforms in |transforms_out| for a rounded rectangle
  97. // of the given |desired_size| and |corner_radius|. The effective size may
  98. // differ from |desired_size| at certain scale factors to make sure the ripple
  99. // is pixel-aligned.
  100. void CalculateRectTransforms(const gfx::Size& desired_size,
  101. float corner_radius,
  102. InkDropTransforms* transforms_out) const;
  103. // Calculates a Transform for a circle layer.
  104. gfx::Transform CalculateCircleTransform(float scale,
  105. float target_center_x,
  106. float target_center_y) const;
  107. // Calculates a Transform for a rectangle layer.
  108. gfx::Transform CalculateRectTransform(float x_scale, float y_scale) const;
  109. // Updates all of the Transforms in |transforms_out| to the current Transforms
  110. // of the painted shape Layers.
  111. void GetCurrentTransforms(InkDropTransforms* transforms_out) const;
  112. // Updates all of the Transforms in |transforms_out| with the target
  113. // Transforms for the ACTIVATED animation.
  114. void GetActivatedTargetTransforms(InkDropTransforms* transforms_out) const;
  115. // Updates all of the Transforms in |transforms_out| with the target
  116. // Transforms for the DEACTIVATED animation.
  117. void GetDeactivatedTargetTransforms(InkDropTransforms* transforms_out) const;
  118. // Adds and configures a new |painted_shape| layer to |painted_layers_|.
  119. void AddPaintLayer(PaintedShape painted_shape);
  120. // Called from LayerAnimator when a new LayerAnimationSequence is scheduled
  121. // which allows for assigning the observer to the sequence.
  122. void OnLayerAnimationSequenceScheduled(ui::LayerAnimationSequence* sequence);
  123. // The shape used for the ACTIVATED/DEACTIVATED states.
  124. ActivatedShape activated_shape_;
  125. // Ink drop opacity when it is visible.
  126. float visible_opacity_;
  127. // Maximum size that an ink drop will be drawn to for any InkDropState whose
  128. // final frame should be large.
  129. const gfx::Size large_size_;
  130. // Corner radius used to draw the rounded rectangles corner for any
  131. // InkDropState whose final frame should be large.
  132. const int large_corner_radius_;
  133. // Maximum size that an ink drop will be drawn to for any InkDropState whose
  134. // final frame should be small.
  135. const gfx::Size small_size_;
  136. // Corner radius used to draw the rounded rectangles corner for any
  137. // InkDropState whose final frame should be small.
  138. const int small_corner_radius_;
  139. // The center point of the ripple, relative to the root layer's origin.
  140. gfx::Point center_point_;
  141. // ui::LayerDelegate to paint circles for all the circle layers.
  142. std::unique_ptr<CircleLayerDelegate> circle_layer_delegate_;
  143. // ui::LayerDelegate to paint rectangles for all the rectangle layers.
  144. std::unique_ptr<RectangleLayerDelegate> rect_layer_delegate_;
  145. // The root layer that parents the animating layers. The root layer is used to
  146. // manipulate opacity and location, and its children are used to manipulate
  147. // the different painted shapes that compose the ink drop.
  148. ui::Layer root_layer_;
  149. // Sequence scheduled callback subscription for the root layer.
  150. base::CallbackListSubscription root_callback_subscription_;
  151. // ui::Layers for all of the painted shape layers that compose the ink drop.
  152. std::unique_ptr<ui::Layer> painted_layers_[PAINTED_SHAPE_COUNT];
  153. // Sequence scheduled callback subscriptions for the painted layers.
  154. base::CallbackListSubscription callback_subscriptions_[PAINTED_SHAPE_COUNT];
  155. };
  156. } // namespace views
  157. #endif // UI_VIEWS_ANIMATION_SQUARE_INK_DROP_RIPPLE_H_