1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/animation/linear_animation.h"
9 #include "ui/gfx/animation/animation_container.h"
10 #include "ui/gfx/animation/animation_delegate.h"
13 using base::TimeDelta;
17 static TimeDelta CalculateInterval(int frame_rate) {
18 int timer_interval = 1000000 / frame_rate;
19 if (timer_interval < 10000)
20 timer_interval = 10000;
21 return TimeDelta::FromMicroseconds(timer_interval);
24 LinearAnimation::LinearAnimation(int frame_rate,
25 AnimationDelegate* delegate)
26 : Animation(CalculateInterval(frame_rate)),
29 set_delegate(delegate);
32 LinearAnimation::LinearAnimation(int duration,
34 AnimationDelegate* delegate)
35 : Animation(CalculateInterval(frame_rate)),
36 duration_(TimeDelta::FromMilliseconds(duration)),
39 set_delegate(delegate);
40 SetDuration(duration);
43 double LinearAnimation::GetCurrentValue() const {
44 // Default is linear relationship, subclass to adapt.
48 void LinearAnimation::SetCurrentValue(double new_value) {
49 new_value = std::max(0.0, std::min(1.0, new_value));
50 base::TimeDelta time_delta = base::TimeDelta::FromMicroseconds(
51 duration_.InMicroseconds() * (new_value - state_));
52 SetStartTime(start_time() - time_delta);
56 void LinearAnimation::End() {
60 // NOTE: We don't use AutoReset here as Stop may end up deleting us (by way
66 void LinearAnimation::SetDuration(int duration) {
67 duration_ = TimeDelta::FromMilliseconds(duration);
68 if (duration_ < timer_interval())
69 duration_ = timer_interval();
71 SetStartTime(container()->last_tick_time());
74 void LinearAnimation::Step(base::TimeTicks time_now) {
75 TimeDelta elapsed_time = time_now - start_time();
76 state_ = static_cast<double>(elapsed_time.InMicroseconds()) /
77 static_cast<double>(duration_.InMicroseconds());
81 AnimateToState(state_);
84 delegate()->AnimationProgressed(this);
90 void LinearAnimation::AnimationStarted() {
94 void LinearAnimation::AnimationStopped() {
99 // Set state_ to ensure we send ended to delegate and not canceled.
104 bool LinearAnimation::ShouldSendCanceledFromStop() {