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

ash / system / progress_indicator / progress_indicator_animation.cc [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/progress_indicator/progress_indicator_animation.h"

#include "base/task/sequenced_task_runner.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/gfx/animation/slide_animation.h"

namespace ash {

ProgressIndicatorAnimation::ProgressIndicatorAnimation(base::TimeDelta duration,
                                                       bool is_cyclic)
    : duration_(duration), is_cyclic_(is_cyclic) {}

ProgressIndicatorAnimation::~ProgressIndicatorAnimation() = default;

base::CallbackListSubscription
ProgressIndicatorAnimation::AddAnimationUpdatedCallback(
    base::RepeatingClosureList::CallbackType callback) {
  return animation_updated_callback_list_.Add(std::move(callback));
}

void ProgressIndicatorAnimation::AddUnsafeAnimationUpdatedCallback(
    base::RepeatingClosureList::CallbackType callback) {
  animation_updated_callback_list_.AddUnsafe(std::move(callback));
}

void ProgressIndicatorAnimation::Start() {
  StartInternal(/*is_cyclic_restart=*/false);
}

bool ProgressIndicatorAnimation::HasAnimated() const {
  return animator_ != nullptr;
}

bool ProgressIndicatorAnimation::IsAnimating() const {
  if (animator_ && animator_->is_animating()) {
    return true;
  }

  // Cyclic animations, such as indeterminate ring animations, repeat forever
  // and never finish from the user's perspective. Therefore, this function
  // returns true if a cyclic animation has started.
  return is_cyclic_ && !start_time_.is_null();
}

void ProgressIndicatorAnimation::Init() {
  UpdateAnimatableProperties(/*fraction=*/0.f);
}

void ProgressIndicatorAnimation::AnimationProgressed(
    const gfx::Animation* animation) {
  UpdateAnimatableProperties(animation->GetCurrentValue());
  animation_updated_callback_list_.Notify();
}

void ProgressIndicatorAnimation::AnimationEnded(
    const gfx::Animation* animation) {
  if (!is_cyclic_) {
    animation_updated_callback_list_.Notify();
    return;
  }

  // In tests, animations may be scaled such that duration is zero. When this
  // happens, we need to post cyclic restarts rather than restarting them
  // immediately. Otherwise the animation will loop endlessly without providing
  // other code an opportunity to run.
  if (ui::ScopedAnimationDurationScaleMode::duration_multiplier() == 0.f) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE,
        base::BindOnce(&ProgressIndicatorAnimation::StartInternal,
                       weak_factory_.GetWeakPtr(), /*is_cyclic_restart=*/true));
    return;
  }

  StartInternal(/*is_cyclic_restart=*/true);
}

void ProgressIndicatorAnimation::StartInternal(bool is_cyclic_restart) {
  animator_ = std::make_unique<gfx::SlideAnimation>(this);
  animator_->SetSlideDuration(
      ui::ScopedAnimationDurationScaleMode::duration_multiplier() * duration_);
  animator_->SetTweenType(gfx::Tween::Type::LINEAR);

  if (!is_cyclic_restart)
    start_time_ = base::TimeTicks::Now();

  animator_->Show();
}

}  // namespace ash