Home | History | Annotate | Download | only in animation
      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.
      4 
      5 #include "ui/base/animation/animation_container.h"
      6 
      7 #include "ui/base/animation/animation_container_element.h"
      8 #include "ui/base/animation/animation_container_observer.h"
      9 
     10 using base::TimeDelta;
     11 using base::TimeTicks;
     12 
     13 namespace ui {
     14 
     15 AnimationContainer::AnimationContainer()
     16     : last_tick_time_(TimeTicks::Now()),
     17       observer_(NULL) {
     18 }
     19 
     20 AnimationContainer::~AnimationContainer() {
     21   // The animations own us and stop themselves before being deleted. If
     22   // elements_ is not empty, something is wrong.
     23   DCHECK(elements_.empty());
     24 }
     25 
     26 void AnimationContainer::Start(AnimationContainerElement* element) {
     27   DCHECK(elements_.count(element) == 0);  // Start should only be invoked if the
     28                                           // element isn't running.
     29 
     30   if (elements_.empty()) {
     31     last_tick_time_ = TimeTicks::Now();
     32     SetMinTimerInterval(element->GetTimerInterval());
     33   } else if (element->GetTimerInterval() < min_timer_interval_) {
     34     SetMinTimerInterval(element->GetTimerInterval());
     35   }
     36 
     37   element->SetStartTime(last_tick_time_);
     38   elements_.insert(element);
     39 }
     40 
     41 void AnimationContainer::Stop(AnimationContainerElement* element) {
     42   DCHECK(elements_.count(element) > 0);  // The element must be running.
     43 
     44   elements_.erase(element);
     45 
     46   if (elements_.empty()) {
     47     timer_.Stop();
     48     if (observer_)
     49       observer_->AnimationContainerEmpty(this);
     50   } else {
     51     TimeDelta min_timer_interval = GetMinInterval();
     52     if (min_timer_interval > min_timer_interval_)
     53       SetMinTimerInterval(min_timer_interval);
     54   }
     55 }
     56 
     57 void AnimationContainer::Run() {
     58   // We notify the observer after updating all the elements. If all the elements
     59   // are deleted as a result of updating then our ref count would go to zero and
     60   // we would be deleted before we notify our observer. We add a reference to
     61   // ourself here to make sure we're still valid after running all the elements.
     62   scoped_refptr<AnimationContainer> this_ref(this);
     63 
     64   TimeTicks current_time = TimeTicks::Now();
     65 
     66   last_tick_time_ = current_time;
     67 
     68   // Make a copy of the elements to iterate over so that if any elements are
     69   // removed as part of invoking Step there aren't any problems.
     70   Elements elements = elements_;
     71 
     72   for (Elements::const_iterator i = elements.begin();
     73        i != elements.end(); ++i) {
     74     // Make sure the element is still valid.
     75     if (elements_.find(*i) != elements_.end())
     76       (*i)->Step(current_time);
     77   }
     78 
     79   if (observer_)
     80     observer_->AnimationContainerProgressed(this);
     81 }
     82 
     83 void AnimationContainer::SetMinTimerInterval(base::TimeDelta delta) {
     84   // This doesn't take into account how far along the current element is, but
     85   // that shouldn't be a problem for uses of Animation/AnimationContainer.
     86   timer_.Stop();
     87   min_timer_interval_ = delta;
     88   timer_.Start(FROM_HERE, min_timer_interval_, this, &AnimationContainer::Run);
     89 }
     90 
     91 TimeDelta AnimationContainer::GetMinInterval() {
     92   DCHECK(!elements_.empty());
     93 
     94   TimeDelta min;
     95   Elements::const_iterator i = elements_.begin();
     96   min = (*i)->GetTimerInterval();
     97   for (++i; i != elements_.end(); ++i) {
     98     if ((*i)->GetTimerInterval() < min)
     99       min = (*i)->GetTimerInterval();
    100   }
    101   return min;
    102 }
    103 
    104 }  // namespace ui
    105