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