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