• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "config.h"
30 
31 #include "AnimationController.h"
32 #include "CompositeAnimation.h"
33 #include "CSSPropertyNames.h"
34 #include "EventNames.h"
35 #include "ImplicitAnimation.h"
36 #include "KeyframeAnimation.h"
37 #include "RenderObject.h"
38 
39 namespace WebCore {
40 
ImplicitAnimation(const Animation * transition,int animatingProperty,RenderObject * renderer,CompositeAnimation * compAnim,RenderStyle * fromStyle)41 ImplicitAnimation::ImplicitAnimation(const Animation* transition, int animatingProperty, RenderObject* renderer, CompositeAnimation* compAnim, RenderStyle* fromStyle)
42     : AnimationBase(transition, renderer, compAnim)
43     , m_transitionProperty(transition->property())
44     , m_animatingProperty(animatingProperty)
45     , m_overridden(false)
46     , m_fromStyle(fromStyle)
47 {
48     ASSERT(animatingProperty != cAnimateAll);
49 }
50 
~ImplicitAnimation()51 ImplicitAnimation::~ImplicitAnimation()
52 {
53     // Do the cleanup here instead of in the base class so the specialized methods get called
54     if (!postActive())
55         updateStateMachine(AnimationStateInputEndAnimation, -1);
56 }
57 
shouldSendEventForListener(Document::ListenerType inListenerType)58 bool ImplicitAnimation::shouldSendEventForListener(Document::ListenerType inListenerType)
59 {
60     return m_object->document()->hasListenerType(inListenerType);
61 }
62 
animate(CompositeAnimation *,RenderObject *,RenderStyle *,RenderStyle * targetStyle,RefPtr<RenderStyle> & animatedStyle)63 void ImplicitAnimation::animate(CompositeAnimation*, RenderObject*, RenderStyle*, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle)
64 {
65     // If we get this far and the animation is done, it means we are cleaning up a just finished animation.
66     // So just return. Everything is already all cleaned up.
67     if (postActive())
68         return;
69 
70     // Reset to start the transition if we are new
71     if (isNew())
72         reset(targetStyle);
73 
74     // Run a cycle of animation.
75     // We know we will need a new render style, so make one if needed
76     if (!animatedStyle)
77         animatedStyle = RenderStyle::clone(targetStyle);
78 
79     if (blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0)))
80         setAnimating();
81 
82     // Fire the start timeout if needed
83     fireAnimationEventsIfNeeded();
84 }
85 
onAnimationEnd(double elapsedTime)86 void ImplicitAnimation::onAnimationEnd(double elapsedTime)
87 {
88     // If we have a keyframe animation on this property, this transition is being overridden. The keyframe
89     // animation keeps an unanimated style in case a transition starts while the keyframe animation is
90     // running. But now that the transition has completed, we need to update this style with its new
91     // destination. If we didn't, the next time through we would think a transition had started
92     // (comparing the old unanimated style with the new final style of the transition).
93     RefPtr<KeyframeAnimation> keyframeAnim = m_compAnim->getAnimationForProperty(m_animatingProperty);
94     if (keyframeAnim)
95         keyframeAnim->setUnanimatedStyle(m_toStyle);
96 
97     if (!sendTransitionEvent(eventNames().webkitTransitionEndEvent, elapsedTime)) {
98         // We didn't dispatch an event, which would call endAnimation(), so we'll just call it here.
99         endAnimation(true);
100     }
101 }
102 
sendTransitionEvent(const AtomicString & eventType,double elapsedTime)103 bool ImplicitAnimation::sendTransitionEvent(const AtomicString& eventType, double elapsedTime)
104 {
105     if (eventType == eventNames().webkitTransitionEndEvent) {
106         Document::ListenerType listenerType = Document::TRANSITIONEND_LISTENER;
107 
108         if (shouldSendEventForListener(listenerType)) {
109             String propertyName;
110             if (m_animatingProperty != cAnimateAll)
111                 propertyName = getPropertyName(static_cast<CSSPropertyID>(m_animatingProperty));
112 
113             // Dispatch the event
114             RefPtr<Element> element = 0;
115             if (m_object->node() && m_object->node()->isElementNode())
116                 element = static_cast<Element*>(m_object->node());
117 
118             ASSERT(!element || element->document() && !element->document()->inPageCache());
119             if (!element)
120                 return false;
121 
122             // Schedule event handling
123             m_object->animation()->addEventToDispatch(element, eventType, propertyName, elapsedTime);
124 
125             // Restore the original (unanimated) style
126             if (eventType == eventNames().webkitTransitionEndEvent && element->renderer())
127                 setChanged(element.get());
128 
129             return true; // Did dispatch an event
130         }
131     }
132 
133     return false; // Didn't dispatch an event
134 }
135 
reset(RenderStyle * to)136 void ImplicitAnimation::reset(RenderStyle* to)
137 {
138     ASSERT(to);
139     ASSERT(m_fromStyle);
140 
141 
142     m_toStyle = to;
143 
144     // Restart the transition
145     if (m_fromStyle && m_toStyle)
146         updateStateMachine(AnimationStateInputRestartAnimation, -1);
147 
148     // set the transform animation list
149     validateTransformFunctionList();
150 }
151 
setOverridden(bool b)152 void ImplicitAnimation::setOverridden(bool b)
153 {
154     if (b == m_overridden)
155         return;
156 
157     m_overridden = b;
158     updateStateMachine(m_overridden ? AnimationStateInputPauseOverride : AnimationStateInputResumeOverride, -1);
159 }
160 
affectsProperty(int property) const161 bool ImplicitAnimation::affectsProperty(int property) const
162 {
163     return (m_animatingProperty == property);
164 }
165 
isTargetPropertyEqual(int prop,const RenderStyle * targetStyle)166 bool ImplicitAnimation::isTargetPropertyEqual(int prop, const RenderStyle* targetStyle)
167 {
168     return propertiesEqual(prop, m_toStyle.get(), targetStyle);
169 }
170 
blendPropertyValueInStyle(int prop,RenderStyle * currentStyle)171 void ImplicitAnimation::blendPropertyValueInStyle(int prop, RenderStyle* currentStyle)
172 {
173     blendProperties(this, prop, currentStyle, m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
174 }
175 
validateTransformFunctionList()176 void ImplicitAnimation::validateTransformFunctionList()
177 {
178     m_transformFunctionListValid = false;
179 
180     if (!m_fromStyle || !m_toStyle)
181         return;
182 
183     const TransformOperations* val = &m_fromStyle->transform();
184     const TransformOperations* toVal = &m_toStyle->transform();
185 
186     if (val->operations().isEmpty())
187         val = toVal;
188 
189     if (val->operations().isEmpty())
190         return;
191 
192     // See if the keyframes are valid
193     if (val != toVal) {
194         // A list of length 0 matches anything
195         if (!toVal->operations().isEmpty()) {
196             // If the sizes of the function lists don't match, the lists don't match
197             if (val->operations().size() != toVal->operations().size())
198                 return;
199 
200             // If the types of each function are not the same, the lists don't match
201             for (size_t j = 0; j < val->operations().size(); ++j) {
202                 if (!val->operations()[j]->isSameType(*toVal->operations()[j]))
203                     return;
204             }
205         }
206     }
207 
208     // Keyframes are valid
209     m_transformFunctionListValid = true;
210 }
211 
212 } // namespace WebCore
213