File: animation_sequence_block.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (336 lines) | stat: -rw-r--r-- 12,308 bytes parent folder | download | duplicates (5)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2021 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/animation_sequence_block.h"

#include <map>
#include <memory>
#include <optional>
#include <utility>
#include <variant>

#include "base/check.h"
#include "base/functional/callback.h"
#include "base/time/time.h"
#include "base/types/pass_key.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/compositor/layer_animation_element.h"
#include "ui/compositor/layer_owner.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/interpolated_transform.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/animation/animation_key.h"

namespace views {

using PassKey = base::PassKey<AnimationSequenceBlock>;

AnimationSequenceBlock::AnimationSequenceBlock(
    base::PassKey<AnimationBuilder> builder_key,
    AnimationBuilder* owner,
    base::TimeDelta start,
    bool repeating)
    : builder_key_(builder_key),
      owner_(owner),
      start_(start),
      repeating_(repeating) {}

AnimationSequenceBlock::~AnimationSequenceBlock() {
  if (!finalized_) {
    TerminateBlock();
    owner_->TerminateSequence(PassKey(), repeating_);
  }
}

AnimationSequenceBlock& AnimationSequenceBlock::SetDuration(
    base::TimeDelta duration) {
  DCHECK(!finalized_) << "Do not access old blocks after creating new ones.";
  DCHECK(!duration_.has_value()) << "Duration may be set at most once.";
  duration_ = duration;
  return *this;
}

AnimationSequenceBlock& AnimationSequenceBlock::SetBounds(
    ui::Layer* target,
    const gfx::Rect& bounds,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::BOUNDS},
                      Element(bounds, tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetBounds(
    ui::LayerOwner* target,
    const gfx::Rect& bounds,
    gfx::Tween::Type tween_type) {
  return SetBounds(target->layer(), bounds, tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::SetBrightness(
    ui::Layer* target,
    float brightness,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::BRIGHTNESS},
                      Element(brightness, tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetBrightness(
    ui::LayerOwner* target,
    float brightness,
    gfx::Tween::Type tween_type) {
  return SetBrightness(target->layer(), brightness, tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::SetClipRect(
    ui::Layer* target,
    const gfx::Rect& clip_rect,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::CLIP},
                      Element(clip_rect, tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetClipRect(
    ui::LayerOwner* target,
    const gfx::Rect& clip_rect,
    gfx::Tween::Type tween_type) {
  return SetClipRect(target->layer(), clip_rect, tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::SetColor(
    ui::Layer* target,
    SkColor4f color,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::COLOR},
                      Element(color, tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetColor(
    ui::LayerOwner* target,
    SkColor4f color,
    gfx::Tween::Type tween_type) {
  return SetColor(target->layer(), color, tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::SetGrayscale(
    ui::Layer* target,
    float grayscale,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::GRAYSCALE},
                      Element(grayscale, tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetGrayscale(
    ui::LayerOwner* target,
    float grayscale,
    gfx::Tween::Type tween_type) {
  return SetGrayscale(target->layer(), grayscale, tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::SetOpacity(
    ui::Layer* target,
    float opacity,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::OPACITY},
                      Element(opacity, tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetOpacity(
    ui::LayerOwner* target,
    float opacity,
    gfx::Tween::Type tween_type) {
  return SetOpacity(target->layer(), opacity, tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::SetTransform(
    ui::Layer* target,
    gfx::Transform transform,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::TRANSFORM},
                      Element(std::move(transform), tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetTransform(
    ui::LayerOwner* target,
    gfx::Transform transform,
    gfx::Tween::Type tween_type) {
  return SetTransform(target->layer(), std::move(transform), tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::SetRoundedCorners(
    ui::Layer* target,
    const gfx::RoundedCornersF& rounded_corners,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::ROUNDED_CORNERS},
                      Element(rounded_corners, tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetRoundedCorners(
    ui::LayerOwner* target,
    const gfx::RoundedCornersF& rounded_corners,
    gfx::Tween::Type tween_type) {
  return SetRoundedCorners(target->layer(), rounded_corners, tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::SetGradientMask(
    ui::Layer* target,
    const gfx::LinearGradient& gradient_mask,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::GRADIENT_MASK},
                      Element(gradient_mask, tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetGradientMask(
    ui::LayerOwner* target,
    const gfx::LinearGradient& gradient_mask,
    gfx::Tween::Type tween_type) {
  return SetGradientMask(target->layer(), gradient_mask, tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::SetVisibility(
    ui::Layer* target,
    bool visible,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::VISIBILITY},
                      Element(visible, tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetVisibility(
    ui::LayerOwner* target,
    bool visible,
    gfx::Tween::Type tween_type) {
  return SetVisibility(target->layer(), visible, tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::SetInterpolatedTransform(
    ui::Layer* target,
    std::unique_ptr<ui::InterpolatedTransform> interpolated_transform,
    gfx::Tween::Type tween_type) {
  return AddAnimation({target, ui::LayerAnimationElement::TRANSFORM},
                      Element(std::move(interpolated_transform), tween_type));
}

AnimationSequenceBlock& AnimationSequenceBlock::SetInterpolatedTransform(
    ui::LayerOwner* target,
    std::unique_ptr<ui::InterpolatedTransform> interpolated_transform,
    gfx::Tween::Type tween_type) {
  return SetInterpolatedTransform(
      target->layer(), std::move(interpolated_transform), tween_type);
}

AnimationSequenceBlock& AnimationSequenceBlock::At(
    base::TimeDelta since_sequence_start) {
  // NOTE: at the end of this function, this object is destroyed.

  DCHECK(!finalized_) << "Do not access old blocks after creating new ones.";
  TerminateBlock();
  finalized_ = true;

  // NOTE: `old_sequence` is actually the sequence block itself. Do not destruct
  // this object until the function end.
  auto old_sequence = owner_->SwapCurrentSequence(
      PassKey(), std::make_unique<AnimationSequenceBlock>(
                     builder_key_, owner_, since_sequence_start, repeating_));

  return owner_->GetCurrentSequence();
}

AnimationSequenceBlock& AnimationSequenceBlock::Offset(
    base::TimeDelta since_last_block_start) {
  return At(start_ + since_last_block_start);
}

AnimationSequenceBlock& AnimationSequenceBlock::Then() {
  return Offset(duration_.value_or(base::TimeDelta()));
}

AnimationSequenceBlock::Element::Element(AnimationValue animation_value,
                                         gfx::Tween::Type tween_type)
    : animation_value_(std::move(animation_value)), tween_type_(tween_type) {}
AnimationSequenceBlock::Element::~Element() = default;
AnimationSequenceBlock::Element::Element(Element&&) = default;
AnimationSequenceBlock::Element& AnimationSequenceBlock::Element::operator=(
    Element&&) = default;

AnimationSequenceBlock& AnimationSequenceBlock::AddAnimation(AnimationKey key,
                                                             Element element) {
  DCHECK(!finalized_) << "Do not access old blocks after creating new ones.";
  DCHECK(key.target) << "Animation targets must paint to a layer.";
  const auto result =
      elements_.insert(std::make_pair(std::move(key), std::move(element)));
  DCHECK(result.second) << "Animate (target, property) at most once per block.";
  return *this;
}

void AnimationSequenceBlock::TerminateBlock() {
  const auto duration = duration_.value_or(base::TimeDelta());
  for (auto& pair : elements_) {
    std::unique_ptr<ui::LayerAnimationElement> element;
    switch (pair.first.property) {
      case ui::LayerAnimationElement::TRANSFORM:
        if (std::holds_alternative<std::unique_ptr<ui::InterpolatedTransform>>(
                pair.second.animation_value_)) {
          element =
              ui::LayerAnimationElement::CreateInterpolatedTransformElement(
                  std::get<std::unique_ptr<ui::InterpolatedTransform>>(
                      std::move(pair.second.animation_value_)),
                  duration);
        } else {
          DCHECK(std::holds_alternative<gfx::Transform>(
              pair.second.animation_value_));
          element = ui::LayerAnimationElement::CreateTransformElement(
              std::get<gfx::Transform>(std::move(pair.second.animation_value_)),
              duration);
        }
        break;
      case ui::LayerAnimationElement::BOUNDS:
        element = ui::LayerAnimationElement::CreateBoundsElement(
            std::get<gfx::Rect>(pair.second.animation_value_), duration);
        break;
      case ui::LayerAnimationElement::OPACITY:
        element = ui::LayerAnimationElement::CreateOpacityElement(
            std::get<float>(pair.second.animation_value_), duration);
        break;
      case ui::LayerAnimationElement::VISIBILITY:
        element = ui::LayerAnimationElement::CreateVisibilityElement(
            std::get<bool>(pair.second.animation_value_), duration);
        break;
      case ui::LayerAnimationElement::BRIGHTNESS:
        element = ui::LayerAnimationElement::CreateBrightnessElement(
            std::get<float>(pair.second.animation_value_), duration);
        break;
      case ui::LayerAnimationElement::GRAYSCALE:
        element = ui::LayerAnimationElement::CreateGrayscaleElement(
            std::get<float>(pair.second.animation_value_), duration);
        break;
      case ui::LayerAnimationElement::COLOR:
        element = ui::LayerAnimationElement::CreateColorElement(
            std::get<SkColor4f>(pair.second.animation_value_), duration);
        break;
      case ui::LayerAnimationElement::CLIP:
        element = ui::LayerAnimationElement::CreateClipRectElement(
            std::get<gfx::Rect>(pair.second.animation_value_), duration);
        break;
      case ui::LayerAnimationElement::ROUNDED_CORNERS:
        element = ui::LayerAnimationElement::CreateRoundedCornersElement(
            std::get<gfx::RoundedCornersF>(pair.second.animation_value_),
            duration);
        break;
      case ui::LayerAnimationElement::GRADIENT_MASK:
        element = ui::LayerAnimationElement::CreateGradientMaskElement(
            std::get<gfx::LinearGradient>(pair.second.animation_value_),
            duration);
        break;
      default:
        NOTREACHED();
    }
    element->set_tween_type(pair.second.tween_type_);
    owner_->AddLayerAnimationElement(PassKey(), pair.first, start_, duration,
                                     std::move(element));
  }

  owner_->BlockEndedAt(PassKey(), start_ + duration);
  elements_.clear();
}

}  // namespace views