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