File: ink_drop_highlight.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (179 lines) | stat: -rw-r--r-- 6,336 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/animation/ink_drop_highlight.h"

#include <memory>
#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/animation/animation.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/animation/ink_drop_highlight_observer.h"
#include "ui/views/animation/ink_drop_painted_layer_delegates.h"
#include "ui/views/animation/ink_drop_util.h"

namespace views {

namespace {

// The opacity of the highlight when it is not visible.
constexpr float kHiddenOpacity = 0.0f;

}  // namespace

std::string ToString(InkDropHighlight::AnimationType animation_type) {
  switch (animation_type) {
    case InkDropHighlight::AnimationType::kFadeIn:
      return std::string("FADE_IN");
    case InkDropHighlight::AnimationType::kFadeOut:
      return std::string("FADE_OUT");
  }
}

InkDropHighlight::InkDropHighlight(
    const gfx::PointF& center_point,
    std::unique_ptr<BasePaintedLayerDelegate> layer_delegate)
    : center_point_(center_point),
      layer_delegate_(std::move(layer_delegate)),
      layer_(std::make_unique<ui::Layer>()) {
  const gfx::RectF painted_bounds = layer_delegate_->GetPaintedBounds();
  size_ = painted_bounds.size();

  layer_->SetBounds(gfx::ToEnclosingRect(painted_bounds));
  layer_->SetFillsBoundsOpaquely(false);
  layer_->set_delegate(layer_delegate_.get());
  layer_->SetVisible(false);
  layer_->SetMasksToBounds(false);
  layer_->SetName("InkDropHighlight:layer");
}

InkDropHighlight::InkDropHighlight(const gfx::SizeF& size,
                                   int corner_radius,
                                   const gfx::PointF& center_point,
                                   SkColor color)
    : InkDropHighlight(
          center_point,
          std::make_unique<RoundedRectangleLayerDelegate>(color,
                                                          size,
                                                          corner_radius)) {
  layer_->SetOpacity(visible_opacity_);
}

InkDropHighlight::InkDropHighlight(const gfx::Size& size,
                                   int corner_radius,
                                   const gfx::PointF& center_point,
                                   SkColor color)
    : InkDropHighlight(gfx::SizeF(size), corner_radius, center_point, color) {}

InkDropHighlight::InkDropHighlight(const gfx::SizeF& size, SkColor base_color)
    : size_(size), layer_(std::make_unique<ui::Layer>(ui::LAYER_SOLID_COLOR)) {
  layer_->SetColor(base_color);
  layer_->SetBounds(gfx::Rect(gfx::ToRoundedSize(size)));
  layer_->SetVisible(false);
  layer_->SetMasksToBounds(false);
  layer_->SetOpacity(visible_opacity_);
  layer_->SetName("InkDropHighlight:solid_color_layer");
}

InkDropHighlight::~InkDropHighlight() {
  // Explicitly aborting all the animations ensures all callbacks are invoked
  // while this instance still exists.
  animation_abort_handle_.reset();
}

bool InkDropHighlight::IsFadingInOrVisible() const {
  return last_animation_initiated_was_fade_in_;
}

void InkDropHighlight::FadeIn(const base::TimeDelta& duration) {
  layer_->SetOpacity(kHiddenOpacity);
  layer_->SetVisible(true);
  AnimateFade(AnimationType::kFadeIn, duration);
}

void InkDropHighlight::FadeOut(const base::TimeDelta& duration) {
  AnimateFade(AnimationType::kFadeOut, duration);
}

test::InkDropHighlightTestApi* InkDropHighlight::GetTestApi() {
  return nullptr;
}

void InkDropHighlight::AnimateFade(AnimationType animation_type,
                                   const base::TimeDelta& duration) {
  last_animation_initiated_was_fade_in_ =
      animation_type == AnimationType::kFadeIn;

  layer_->SetTransform(CalculateTransform());

  const base::TimeDelta effective_duration =
      gfx::Animation::RichAnimationDuration(duration);
  const float opacity = animation_type == AnimationType::kFadeIn
                            ? visible_opacity_
                            : kHiddenOpacity;
  views::AnimationBuilder builder;
  if (effective_duration.is_positive()) {
    animation_abort_handle_ = builder.GetAbortHandle();
  }
  builder
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .OnStarted(base::BindOnce(&InkDropHighlight::AnimationStartedCallback,
                                base::Unretained(this), animation_type))
      .OnEnded(base::BindOnce(&InkDropHighlight::AnimationEndedCallback,
                              base::Unretained(this), animation_type,
                              InkDropAnimationEndedReason::SUCCESS))
      .OnAborted(base::BindOnce(&InkDropHighlight::AnimationEndedCallback,
                                base::Unretained(this), animation_type,
                                InkDropAnimationEndedReason::PRE_EMPTED))
      .Once()
      .SetDuration(effective_duration)
      .SetOpacity(layer_.get(), opacity, gfx::Tween::EASE_IN_OUT);
}

gfx::Transform InkDropHighlight::CalculateTransform() const {
  gfx::Transform transform;
  // No transform needed for a solid color layer.
  if (!layer_delegate_) {
    return transform;
  }

  transform.Translate(center_point_.x(), center_point_.y());
  gfx::Vector2dF layer_offset = layer_delegate_->GetCenteringOffset();
  transform.Translate(-layer_offset.x(), -layer_offset.y());

  // Add subpixel correction to the transform.
  transform.PostConcat(
      GetTransformSubpixelCorrection(transform, layer_->device_scale_factor()));

  return transform;
}

void InkDropHighlight::AnimationStartedCallback(AnimationType animation_type) {
  if (observer_) {
    observer_->AnimationStarted(animation_type);
  }
}

void InkDropHighlight::AnimationEndedCallback(
    AnimationType animation_type,
    InkDropAnimationEndedReason reason) {
  // AnimationEndedCallback() may be invoked when this is being destroyed and
  // |layer_| may be null.
  if (animation_type == AnimationType::kFadeOut && layer_) {
    layer_->SetVisible(false);
  }

  if (observer_) {
    observer_->AnimationEnded(animation_type, reason);
  }
}

}  // namespace views