#include "ash/system/progress_indicator/progress_indicator_animation.h"
#include "base/task/sequenced_task_runner.h"
#include "ui/gfx/animation/slide_animation.h"
#include "ui/gfx/scoped_animation_duration_scale_mode.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(false);
}
bool ProgressIndicatorAnimation::HasAnimated() const {
return animator_ != nullptr;
}
bool ProgressIndicatorAnimation::IsAnimating() const {
if (animator_ && animator_->is_animating()) {
return true;
}
return is_cyclic_ && !start_time_.is_null();
}
void ProgressIndicatorAnimation::Init() {
UpdateAnimatableProperties(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;
}
if (gfx::ScopedAnimationDurationScaleMode::duration_multiplier() == 0.f) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&ProgressIndicatorAnimation::StartInternal,
weak_factory_.GetWeakPtr(), true));
return;
}
StartInternal(true);
}
void ProgressIndicatorAnimation::StartInternal(bool is_cyclic_restart) {
animator_ = std::make_unique<gfx::SlideAnimation>(this);
animator_->SetSlideDuration(
gfx::ScopedAnimationDurationScaleMode::duration_multiplier() * duration_);
animator_->SetTweenType(gfx::Tween::Type::LINEAR);
if (!is_cyclic_restart)
start_time_ = base::TimeTicks::Now();
animator_->Show();
}
}