File: bounds_animator.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 (377 lines) | stat: -rw-r--r-- 12,567 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2012 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/bounds_animator.h"

#include <memory>
#include <utility>

#include "base/containers/contains.h"
#include "base/observer_list.h"
#include "ui/gfx/animation/animation_container.h"
#include "ui/gfx/animation/slide_animation.h"
#include "ui/gfx/animation/tween.h"
#include "ui/gfx/geometry/transform_util.h"
#include "ui/views/animation/bounds_animator_observer.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

#include "base/win/windows_h_disallowed.h"

namespace views {

BoundsAnimator::BoundsAnimator(View* parent, bool use_transforms)
    : AnimationDelegateViews(parent),
      parent_(parent),
      use_transforms_(use_transforms),
      container_(new gfx::AnimationContainer()) {}

BoundsAnimator::~BoundsAnimator() {
  // Delete all the animations, but don't remove any child views. We assume the
  // view owns us and is going to be deleted anyway. However, deleting a
  // delegate may results in removing a child view, so empty the data_ first so
  // that it won't call AnimationCanceled().
  ViewToDataMap data;
  data.swap(data_);
  for (auto& entry : data) {
    CleanupData(false, &entry.second);
  }
}

void BoundsAnimator::AnimateViewTo(
    View* view,
    const gfx::Rect& target,
    std::unique_ptr<gfx::AnimationDelegate> delegate) {
  DCHECK(view);
  DCHECK_EQ(view->parent(), parent_);

  const bool is_animating = IsAnimating(view);

  // Return early if the existing animation on |view| has the same target
  // bounds.
  if (is_animating && target == data_[view].target_bounds) {
    // If this animation specifies a different delegate, swap them out.
    if (delegate && delegate != data_[view].delegate) {
      SetAnimationDelegate(view, std::move(delegate));
    }

    return;
  }

  Data existing_data;
  if (is_animating) {
    DCHECK(base::Contains(data_, view));
    const bool used_transforms = data_[view].target_transform.has_value();
    if (used_transforms) {
      // Using transforms means a view does not have the proper bounds until an
      // animation is complete or canceled. So here we cancel the animation so
      // that the bounds can be updated. Note that this means that callers who
      // want to set bounds (i.e. View::SetBoundsRect()) directly before calling
      // this function will have to explicitly call StopAnimatingView() before
      // doing so.
      StopAnimatingView(view);
    } else {
      // Don't immediately delete the animation, that might trigger a callback
      // from the animation container.
      existing_data = RemoveFromMaps(view);
    }
  }

  // NOTE: we don't check if the view is already at the target location. Doing
  // so leads to odd cases where no animations may be present after invoking
  // AnimateViewTo. AnimationProgressed does nothing when the bounds of the
  // view don't change.

  Data& data = data_[view];
  data.start_bounds = view->bounds();
  data.target_bounds = target;
  data.animation = CreateAnimation();
  data.delegate = std::move(delegate);

  // If the start bounds are empty we cannot derive a transform from start to
  // target. Views with existing transforms are not supported. Default back to
  // using the bounds update animation in these cases.
  // Note that transform is not used if bounds animation requires scaling.
  // Because for some views, their children cannot be scaled with the same scale
  // factor. For example, ShelfAppButton's size in normal state and dense state
  // is 56 and 48 respectively while the size of icon image, the child of
  // ShelfAppButton, is 44 and 36 respectively.
  if (use_transforms_ && !data.start_bounds.IsEmpty() &&
      view->GetTransform().IsIdentity() &&
      data.start_bounds.size() == data.target_bounds.size()) {
    // Calculate the target transform. Note that we don't reset the transform if
    // there already was one, otherwise users will end up with visual bounds
    // different than what they set.
    // Note that View::SetTransform() does not handle RTL, which is different
    // from View::SetBounds(). So mirror the start bounds and target bounds
    // manually if necessary.
    const gfx::Transform target_transform = gfx::TransformBetweenRects(
        gfx::RectF(parent_->GetMirroredRect(data.start_bounds)),
        gfx::RectF(parent_->GetMirroredRect(data.target_bounds)));
    data.target_transform = target_transform;
  }

  animation_to_view_[data.animation.get()] = view;

  data.animation->Show();

  CleanupData(true, &existing_data);
}

void BoundsAnimator::SetTargetBounds(View* view, const gfx::Rect& target) {
  const auto i = data_.find(view);
  if (i == data_.end()) {
    AnimateViewTo(view, target);
  } else {
    i->second.target_bounds = target;
  }
}

gfx::Rect BoundsAnimator::GetTargetBounds(const View* view) const {
  const auto i = data_.find(view);
  return (i == data_.end()) ? view->bounds() : i->second.target_bounds;
}

const gfx::SlideAnimation* BoundsAnimator::GetAnimationForView(View* view) {
  const auto i = data_.find(view);
  return (i == data_.end()) ? nullptr : i->second.animation.get();
}

void BoundsAnimator::SetAnimationDelegate(
    View* view,
    std::unique_ptr<AnimationDelegate> delegate) {
  const auto i = data_.find(view);
  CHECK(i != data_.end());

  i->second.delegate = std::move(delegate);
}

void BoundsAnimator::StopAnimatingView(View* view) {
  const auto i = data_.find(view);
  if (i != data_.end()) {
    i->second.animation->Stop();
  }
}

bool BoundsAnimator::IsAnimating(View* view) const {
  return data_.find(view) != data_.end();
}

bool BoundsAnimator::IsAnimating() const {
  return !data_.empty();
}

void BoundsAnimator::Complete() {
  if (data_.empty()) {
    return;
  }

  while (!data_.empty()) {
    data_.begin()->second.animation->End();
  }

  // Invoke AnimationContainerProgressed to force a repaint and notify delegate.
  AnimationContainerProgressed(container_.get());
}

void BoundsAnimator::Cancel() {
  if (data_.empty()) {
    return;
  }

  while (!data_.empty()) {
    data_.begin()->second.animation->Stop();
  }

  // Invoke AnimationContainerProgressed to force a repaint and notify delegate.
  AnimationContainerProgressed(container_.get());
}

void BoundsAnimator::SetAnimationDuration(base::TimeDelta duration) {
  animation_duration_ = duration;
}

void BoundsAnimator::AddObserver(BoundsAnimatorObserver* observer) {
  observers_.AddObserver(observer);
}

void BoundsAnimator::RemoveObserver(BoundsAnimatorObserver* observer) {
  observers_.RemoveObserver(observer);
}

std::unique_ptr<gfx::SlideAnimation> BoundsAnimator::CreateAnimation() {
  auto animation = std::make_unique<gfx::SlideAnimation>(this);
  animation->SetContainer(container_.get());
  animation->SetSlideDuration(animation_duration_);
  animation->SetTweenType(tween_type_);
  return animation;
}

BoundsAnimator::Data::Data() = default;
BoundsAnimator::Data::Data(Data&&) = default;
BoundsAnimator::Data& BoundsAnimator::Data::operator=(Data&&) = default;
BoundsAnimator::Data::~Data() = default;

BoundsAnimator::Data BoundsAnimator::RemoveFromMaps(View* view) {
  const auto i = data_.find(view);
  CHECK(i != data_.end());
  DCHECK_GT(animation_to_view_.count(i->second.animation.get()), 0u);

  Data old_data = std::move(i->second);
  data_.erase(view);
  animation_to_view_.erase(old_data.animation.get());
  return old_data;
}

void BoundsAnimator::CleanupData(bool send_cancel, Data* data) {
  if (send_cancel && data->delegate) {
    data->delegate->AnimationCanceled(data->animation.get());
  }

  data->delegate.reset();

  if (data->animation) {
    data->animation->set_delegate(nullptr);
    data->animation.reset();
  }
}

std::unique_ptr<gfx::Animation> BoundsAnimator::ResetAnimationForView(
    View* view) {
  const auto i = data_.find(view);
  if (i == data_.end()) {
    return nullptr;
  }

  std::unique_ptr<gfx::Animation> old_animation =
      std::move(i->second.animation);
  animation_to_view_.erase(old_animation.get());
  // Reset the delegate so that we don't attempt any processing when the
  // animation calls us back.
  old_animation->set_delegate(nullptr);
  return old_animation;
}

void BoundsAnimator::AnimationEndedOrCanceled(const gfx::Animation* animation,
                                              AnimationEndType type) {
  DCHECK(animation_to_view_.find(animation) != animation_to_view_.end());

  View* view = animation_to_view_[animation];
  DCHECK(view);

  // Notify the delegate so it has a chance to paint the final state of a
  // completed animation.
  if (type == AnimationEndType::kEnded) {
    DCHECK_EQ(animation->GetCurrentValue(), 1.0);
    AnimationProgressed(animation);
  }

  // Save the data for later clean up.
  Data data = RemoveFromMaps(view);

  if (data.target_transform) {
    if (type == AnimationEndType::kEnded) {
      // Set the bounds at the end of the animation and reset the transform.
      view->SetBoundsRect(data.target_bounds);
    } else {
      DCHECK_EQ(AnimationEndType::kCanceled, type);
      // Get the existing transform and apply it to the start bounds which is
      // the current bounds of the view. This will place the bounds at the place
      // where the animation stopped. See comment in AnimateViewTo() for details
      // as to why GetMirroredRect() is used.
      const gfx::Transform transform = view->GetTransform();
      gfx::Rect bounds = parent_->GetMirroredRect(view->bounds());
      bounds = gfx::ToRoundedRect(transform.MapRect(gfx::RectF(bounds)));
      view->SetBoundsRect(parent_->GetMirroredRect(bounds));
    }
    view->SetTransform(gfx::Transform());
  }

  if (data.delegate) {
    if (type == AnimationEndType::kEnded) {
      data.delegate->AnimationEnded(animation);
    } else {
      DCHECK_EQ(AnimationEndType::kCanceled, type);
      data.delegate->AnimationCanceled(animation);
    }
  }

  CleanupData(false, &data);
}

void BoundsAnimator::AnimationProgressed(const gfx::Animation* animation) {
  DCHECK(animation_to_view_.find(animation) != animation_to_view_.end());

  View* view = animation_to_view_[animation];
  DCHECK(view);
  const Data& data = data_[view];

  if (data.target_transform) {
    const gfx::Transform current_transform = gfx::Tween::TransformValueBetween(
        animation->GetCurrentValue(), gfx::Transform(), *data.target_transform);
    view->SetTransform(current_transform);
  } else {
    gfx::Rect new_bounds =
        animation->CurrentValueBetween(data.start_bounds, data.target_bounds);
    if (new_bounds != view->bounds()) {
      gfx::Rect total_bounds = gfx::UnionRects(new_bounds, view->bounds());

      // Build up the region to repaint in repaint_bounds_. We'll do the repaint
      // when all animations complete (in AnimationContainerProgressed).
      repaint_bounds_.Union(total_bounds);

      view->SetBoundsRect(new_bounds);
    }
  }

  if (data.delegate) {
    data.delegate->AnimationProgressed(animation);
  }
}

void BoundsAnimator::AnimationEnded(const gfx::Animation* animation) {
  AnimationEndedOrCanceled(animation, AnimationEndType::kEnded);
}

void BoundsAnimator::AnimationCanceled(const gfx::Animation* animation) {
  AnimationEndedOrCanceled(animation, AnimationEndType::kCanceled);
}

void BoundsAnimator::AnimationContainerProgressed(
    gfx::AnimationContainer* container) {
  if (!repaint_bounds_.IsEmpty()) {
    // Adjust for rtl.
    repaint_bounds_.set_x(parent_->GetMirroredXWithWidthInView(
        repaint_bounds_.x(), repaint_bounds_.width()));
    parent_->SchedulePaintInRect(repaint_bounds_);
    repaint_bounds_.SetRect(0, 0, 0, 0);
  }

  observers_.Notify(&BoundsAnimatorObserver::OnBoundsAnimatorProgressed, this);

  if (!IsAnimating()) {
    // Notify here rather than from AnimationXXX to avoid deleting the animation
    // while the animation is calling us.
    observers_.Notify(&BoundsAnimatorObserver::OnBoundsAnimatorDone, this);
  }
}

void BoundsAnimator::AnimationContainerEmpty(
    gfx::AnimationContainer* container) {}

void BoundsAnimator::OnChildViewRemoved(views::View* observed_view,
                                        views::View* removed) {
  DCHECK_EQ(parent_, observed_view);
  const auto iter = data_.find(removed);
  if (iter == data_.end()) {
    return;
  }
  AnimationCanceled(iter->second.animation.get());
}

base::TimeDelta BoundsAnimator::GetAnimationDurationForReporting() const {
  return GetAnimationDuration();
}

}  // namespace views