ink_drop_highlight.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "ui/views/animation/ink_drop_highlight.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "third_party/skia/include/core/SkColor.h"
  10. #include "ui/compositor/layer.h"
  11. #include "ui/gfx/animation/animation.h"
  12. #include "ui/gfx/geometry/insets.h"
  13. #include "ui/gfx/geometry/size_conversions.h"
  14. #include "ui/views/animation/animation_builder.h"
  15. #include "ui/views/animation/ink_drop_highlight_observer.h"
  16. #include "ui/views/animation/ink_drop_painted_layer_delegates.h"
  17. #include "ui/views/animation/ink_drop_util.h"
  18. namespace views {
  19. namespace {
  20. // The opacity of the highlight when it is not visible.
  21. constexpr float kHiddenOpacity = 0.0f;
  22. } // namespace
  23. std::string ToString(InkDropHighlight::AnimationType animation_type) {
  24. switch (animation_type) {
  25. case InkDropHighlight::AnimationType::kFadeIn:
  26. return std::string("FADE_IN");
  27. case InkDropHighlight::AnimationType::kFadeOut:
  28. return std::string("FADE_OUT");
  29. }
  30. }
  31. InkDropHighlight::InkDropHighlight(
  32. const gfx::PointF& center_point,
  33. std::unique_ptr<BasePaintedLayerDelegate> layer_delegate)
  34. : center_point_(center_point),
  35. layer_delegate_(std::move(layer_delegate)),
  36. layer_(std::make_unique<ui::Layer>()) {
  37. const gfx::RectF painted_bounds = layer_delegate_->GetPaintedBounds();
  38. size_ = painted_bounds.size();
  39. layer_->SetBounds(gfx::ToEnclosingRect(painted_bounds));
  40. layer_->SetFillsBoundsOpaquely(false);
  41. layer_->set_delegate(layer_delegate_.get());
  42. layer_->SetVisible(false);
  43. layer_->SetMasksToBounds(false);
  44. layer_->SetName("InkDropHighlight:layer");
  45. }
  46. InkDropHighlight::InkDropHighlight(const gfx::SizeF& size,
  47. int corner_radius,
  48. const gfx::PointF& center_point,
  49. SkColor color)
  50. : InkDropHighlight(
  51. center_point,
  52. std::make_unique<RoundedRectangleLayerDelegate>(color,
  53. size,
  54. corner_radius)) {
  55. layer_->SetOpacity(visible_opacity_);
  56. }
  57. InkDropHighlight::InkDropHighlight(const gfx::Size& size,
  58. int corner_radius,
  59. const gfx::PointF& center_point,
  60. SkColor color)
  61. : InkDropHighlight(gfx::SizeF(size), corner_radius, center_point, color) {}
  62. InkDropHighlight::InkDropHighlight(const gfx::SizeF& size, SkColor base_color)
  63. : size_(size), layer_(std::make_unique<ui::Layer>(ui::LAYER_SOLID_COLOR)) {
  64. layer_->SetColor(base_color);
  65. layer_->SetBounds(gfx::Rect(gfx::ToRoundedSize(size)));
  66. layer_->SetVisible(false);
  67. layer_->SetMasksToBounds(false);
  68. layer_->SetOpacity(visible_opacity_);
  69. layer_->SetName("InkDropHighlight:solid_color_layer");
  70. }
  71. InkDropHighlight::~InkDropHighlight() {
  72. // Explicitly aborting all the animations ensures all callbacks are invoked
  73. // while this instance still exists.
  74. animation_abort_handle_.reset();
  75. }
  76. bool InkDropHighlight::IsFadingInOrVisible() const {
  77. return last_animation_initiated_was_fade_in_;
  78. }
  79. void InkDropHighlight::FadeIn(const base::TimeDelta& duration) {
  80. layer_->SetOpacity(kHiddenOpacity);
  81. layer_->SetVisible(true);
  82. AnimateFade(AnimationType::kFadeIn, duration);
  83. }
  84. void InkDropHighlight::FadeOut(const base::TimeDelta& duration) {
  85. AnimateFade(AnimationType::kFadeOut, duration);
  86. }
  87. test::InkDropHighlightTestApi* InkDropHighlight::GetTestApi() {
  88. return nullptr;
  89. }
  90. void InkDropHighlight::AnimateFade(AnimationType animation_type,
  91. const base::TimeDelta& duration) {
  92. last_animation_initiated_was_fade_in_ =
  93. animation_type == AnimationType::kFadeIn;
  94. layer_->SetTransform(CalculateTransform());
  95. const base::TimeDelta effective_duration =
  96. gfx::Animation::ShouldRenderRichAnimation() ? duration
  97. : base::TimeDelta();
  98. const float opacity = animation_type == AnimationType::kFadeIn
  99. ? visible_opacity_
  100. : kHiddenOpacity;
  101. views::AnimationBuilder builder;
  102. if (effective_duration.is_positive())
  103. animation_abort_handle_ = builder.GetAbortHandle();
  104. builder
  105. .SetPreemptionStrategy(
  106. ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
  107. .OnStarted(base::BindOnce(&InkDropHighlight::AnimationStartedCallback,
  108. base::Unretained(this), animation_type))
  109. .OnEnded(base::BindOnce(&InkDropHighlight::AnimationEndedCallback,
  110. base::Unretained(this), animation_type,
  111. InkDropAnimationEndedReason::SUCCESS))
  112. .OnAborted(base::BindOnce(&InkDropHighlight::AnimationEndedCallback,
  113. base::Unretained(this), animation_type,
  114. InkDropAnimationEndedReason::PRE_EMPTED))
  115. .Once()
  116. .SetDuration(effective_duration)
  117. .SetOpacity(layer_.get(), opacity, gfx::Tween::EASE_IN_OUT);
  118. }
  119. gfx::Transform InkDropHighlight::CalculateTransform() const {
  120. gfx::Transform transform;
  121. // No transform needed for a solid color layer.
  122. if (!layer_delegate_)
  123. return transform;
  124. transform.Translate(center_point_.x(), center_point_.y());
  125. gfx::Vector2dF layer_offset = layer_delegate_->GetCenteringOffset();
  126. transform.Translate(-layer_offset.x(), -layer_offset.y());
  127. // Add subpixel correction to the transform.
  128. transform.ConcatTransform(
  129. GetTransformSubpixelCorrection(transform, layer_->device_scale_factor()));
  130. return transform;
  131. }
  132. void InkDropHighlight::AnimationStartedCallback(AnimationType animation_type) {
  133. if (observer_)
  134. observer_->AnimationStarted(animation_type);
  135. }
  136. void InkDropHighlight::AnimationEndedCallback(
  137. AnimationType animation_type,
  138. InkDropAnimationEndedReason reason) {
  139. // AnimationEndedCallback() may be invoked when this is being destroyed and
  140. // |layer_| may be null.
  141. if (animation_type == AnimationType::kFadeOut && layer_)
  142. layer_->SetVisible(false);
  143. if (observer_)
  144. observer_->AnimationEnded(animation_type, reason);
  145. }
  146. } // namespace views