1 // Copyright (c) 2012 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.
4 
5 #include "ui/gfx/animation/throb_animation.h"
6 
7 #include <limits>
8 
9 namespace gfx {
10 
ThrobAnimation(AnimationDelegate * target)11 ThrobAnimation::ThrobAnimation(AnimationDelegate* target)
12     : SlideAnimation(target) {}
13 
StartThrobbing(int cycles_til_stop)14 void ThrobAnimation::StartThrobbing(int cycles_til_stop) {
15   if (cycles_til_stop < 0)
16     cycles_til_stop = std::numeric_limits<int>::max();
17   cycles_remaining_ = cycles_til_stop;
18   throbbing_ = true;
19   SlideAnimation::SetSlideDuration(throb_duration_);
20   if (is_animating())
21     return;  // We're already running, we'll cycle when current loop finishes.
22 
23   if (IsShowing())
24     SlideAnimation::Hide();
25   else
26     SlideAnimation::Show();
27 }
28 
Reset(double value)29 void ThrobAnimation::Reset(double value) {
30   StopThrobbing();
31   SlideAnimation::Reset(value);
32 }
33 
Show()34 void ThrobAnimation::Show() {
35   StopThrobbing();
36   SlideAnimation::Show();
37 }
38 
Hide()39 void ThrobAnimation::Hide() {
40   StopThrobbing();
41   SlideAnimation::Hide();
42 }
43 
SetSlideDuration(base::TimeDelta duration)44 void ThrobAnimation::SetSlideDuration(base::TimeDelta duration) {
45   slide_duration_ = duration;
46 }
47 
Step(base::TimeTicks time_now)48 void ThrobAnimation::Step(base::TimeTicks time_now) {
49   LinearAnimation::Step(time_now);
50 
51   if (!is_animating() && throbbing_) {
52     // Were throbbing a finished a cycle. Start the next cycle unless we're at
53     // the end of the cycles, in which case we stop.
54     cycles_remaining_--;
55     if (IsShowing()) {
56       // We want to stop hidden, hence this doesn't check cycles_remaining_.
57       SlideAnimation::Hide();
58     } else if (cycles_remaining_ > 0) {
59       SlideAnimation::Show();
60     } else {
61       // We're done throbbing.
62       throbbing_ = false;
63     }
64   }
65 }
66 
StopThrobbing()67 void ThrobAnimation::StopThrobbing() {
68   SlideAnimation::SetSlideDuration(slide_duration_);
69   cycles_remaining_ = 0;
70   throbbing_ = false;
71 }
72 
73 }  // namespace gfx
74