1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/compositor/layer_animation_sequence.h"
#include <algorithm>
#include <iterator>
#include "base/debug/trace_event.h"
#include "ui/compositor/layer_animation_delegate.h"
#include "ui/compositor/layer_animation_element.h"
#include "ui/compositor/layer_animation_observer.h"
namespace ui {
LayerAnimationSequence::LayerAnimationSequence()
: is_cyclic_(false),
last_element_(0) {
}
LayerAnimationSequence::LayerAnimationSequence(LayerAnimationElement* element)
: is_cyclic_(false),
last_element_(0) {
AddElement(element);
}
LayerAnimationSequence::~LayerAnimationSequence() {
FOR_EACH_OBSERVER(LayerAnimationObserver,
observers_,
DetachedFromSequence(this, true));
}
void LayerAnimationSequence::Progress(base::TimeDelta elapsed,
LayerAnimationDelegate* delegate) {
bool redraw_required = false;
if (elements_.empty())
return;
size_t current_index = last_element_ % elements_.size();
base::TimeDelta element_duration;
while ((is_cyclic_ || last_element_ < elements_.size()) &&
elements_[current_index]->IsFinished(elapsed - last_start_,
&element_duration)) {
// Let the element we're passing finish.
if (elements_[current_index]->Progress(elapsed - last_start_, delegate))
redraw_required = true;
last_start_ += element_duration;
++last_element_;
current_index = last_element_ % elements_.size();
}
if (is_cyclic_ || last_element_ < elements_.size()) {
if (elements_[current_index]->Progress(elapsed - last_start_, delegate))
redraw_required = true;
}
// Since the delegate may be deleted due to the notifications below, it is
// important that we schedule a draw before sending them.
if (redraw_required)
delegate->ScheduleDrawForAnimation();
if (!is_cyclic_ && last_element_ == elements_.size()) {
last_element_ = 0;
last_start_ = base::TimeDelta::FromMilliseconds(0);
NotifyEnded();
}
}
bool LayerAnimationSequence::IsFinished(base::TimeDelta elapsed) {
if (is_cyclic_)
return false;
if (elements_.empty())
return true;
base::TimeDelta current_start = last_start_;
size_t current_index = last_element_;
base::TimeDelta element_duration;
while ((current_index < elements_.size()) &&
elements_[current_index]->IsFinished(elapsed - current_start,
&element_duration)) {
current_start += element_duration;
++current_index;
}
return (current_index == elements_.size());
}
void LayerAnimationSequence::ProgressToEnd(LayerAnimationDelegate* delegate) {
bool redraw_required = false;
if (elements_.empty())
return;
size_t current_index = last_element_ % elements_.size();
while (current_index < elements_.size()) {
if (elements_[current_index]->ProgressToEnd(delegate))
redraw_required = true;
++current_index;
++last_element_;
}
if (redraw_required)
delegate->ScheduleDrawForAnimation();
if (!is_cyclic_) {
last_element_ = 0;
last_start_ = base::TimeDelta::FromMilliseconds(0);
NotifyEnded();
}
}
void LayerAnimationSequence::GetTargetValue(
LayerAnimationElement::TargetValue* target) const {
if (is_cyclic_)
return;
for (size_t i = last_element_; i < elements_.size(); ++i)
elements_[i]->GetTargetValue(target);
}
void LayerAnimationSequence::Abort() {
size_t current_index = last_element_ % elements_.size();
while (current_index < elements_.size()) {
elements_[current_index]->Abort();
++current_index;
}
last_element_ = 0;
last_start_ = base::TimeDelta::FromMilliseconds(0);
NotifyAborted();
}
void LayerAnimationSequence::AddElement(LayerAnimationElement* element) {
properties_.insert(element->properties().begin(),
element->properties().end());
elements_.push_back(make_linked_ptr(element));
}
bool LayerAnimationSequence::HasCommonProperty(
const LayerAnimationElement::AnimatableProperties& other) const {
LayerAnimationElement::AnimatableProperties intersection;
std::insert_iterator<LayerAnimationElement::AnimatableProperties> ii(
intersection, intersection.begin());
std::set_intersection(properties_.begin(), properties_.end(),
other.begin(), other.end(),
ii);
return intersection.size() > 0;
}
void LayerAnimationSequence::AddObserver(LayerAnimationObserver* observer) {
if (!observers_.HasObserver(observer)) {
observers_.AddObserver(observer);
observer->AttachedToSequence(this);
}
}
void LayerAnimationSequence::RemoveObserver(LayerAnimationObserver* observer) {
observers_.RemoveObserver(observer);
observer->DetachedFromSequence(this, true);
}
void LayerAnimationSequence::OnScheduled() {
NotifyScheduled();
}
void LayerAnimationSequence::OnAnimatorDestroyed() {
if (observers_.might_have_observers()) {
ObserverListBase<LayerAnimationObserver>::Iterator it(observers_);
LayerAnimationObserver* obs;
while ((obs = it.GetNext()) != NULL) {
if (!obs->RequiresNotificationWhenAnimatorDestroyed()) {
// Remove the observer, but do not allow notifications to be sent.
observers_.RemoveObserver(obs);
obs->DetachedFromSequence(this, false);
}
}
}
}
void LayerAnimationSequence::NotifyScheduled() {
FOR_EACH_OBSERVER(LayerAnimationObserver,
observers_,
OnLayerAnimationScheduled(this));
}
void LayerAnimationSequence::NotifyEnded() {
FOR_EACH_OBSERVER(LayerAnimationObserver,
observers_,
OnLayerAnimationEnded(this));
}
void LayerAnimationSequence::NotifyAborted() {
FOR_EACH_OBSERVER(LayerAnimationObserver,
observers_,
OnLayerAnimationAborted(this));
}
} // namespace ui
|