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.
5 #include "app/animation_container.h"
10 AnimationContainer::AnimationContainer()
11 : last_tick_time_(TimeTicks::Now()),
15 AnimationContainer::~AnimationContainer() {
16 // The animations own us and stop themselves before being deleted. If
17 // elements_ is not empty, something is wrong.
18 DCHECK(elements_
.empty());
21 void AnimationContainer::Start(Element
* element
) {
22 DCHECK(elements_
.count(element
) == 0); // Start should only be invoked if the
23 // element isn't running.
25 if (elements_
.empty()) {
26 last_tick_time_
= TimeTicks::Now();
27 SetMinTimerInterval(element
->GetTimerInterval());
28 } else if (element
->GetTimerInterval() < min_timer_interval_
) {
29 SetMinTimerInterval(element
->GetTimerInterval());
32 element
->SetStartTime(last_tick_time_
);
33 elements_
.insert(element
);
36 void AnimationContainer::Stop(Element
* element
) {
37 DCHECK(elements_
.count(element
) > 0); // The element must be running.
39 elements_
.erase(element
);
41 if (elements_
.empty()) {
44 observer_
->AnimationContainerEmpty(this);
46 TimeDelta min_timer_interval
= GetMinInterval();
47 if (min_timer_interval
> min_timer_interval_
)
48 SetMinTimerInterval(min_timer_interval
);
52 void AnimationContainer::Run() {
53 // We notify the observer after updating all the elements. If all the elements
54 // are deleted as a result of updating then our ref count would go to zero and
55 // we would be deleted before we notify our observer. We add a reference to
56 // ourself here to make sure we're still valid after running all the elements.
57 scoped_refptr
<AnimationContainer
> this_ref(this);
59 TimeTicks current_time
= TimeTicks::Now();
61 last_tick_time_
= current_time
;
63 // Make a copy of the elements to iterate over so that if any elements are
64 // removed as part of invoking Step there aren't any problems.
65 Elements elements
= elements_
;
67 for (Elements::const_iterator i
= elements
.begin();
68 i
!= elements
.end(); ++i
) {
69 // Make sure the element is still valid.
70 if (elements_
.find(*i
) != elements_
.end())
71 (*i
)->Step(current_time
);
75 observer_
->AnimationContainerProgressed(this);
78 void AnimationContainer::SetMinTimerInterval(base::TimeDelta delta
) {
79 // This doesn't take into account how far along the current element is, but
80 // that shouldn't be a problem for uses of Animation/AnimationContainer.
82 min_timer_interval_
= delta
;
83 timer_
.Start(min_timer_interval_
, this, &AnimationContainer::Run
);
86 TimeDelta
AnimationContainer::GetMinInterval() {
87 DCHECK(!elements_
.empty());
90 Elements::const_iterator i
= elements_
.begin();
91 min
= (*i
)->GetTimerInterval();
92 for (++i
; i
!= elements_
.end(); ++i
) {
93 if ((*i
)->GetTimerInterval() < min
)
94 min
= (*i
)->GetTimerInterval();