2011-04-12 10:18:43 +04:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-04-12 10:18:43 +04:00
|
|
|
|
|
|
|
#ifndef mozilla_css_AnimationCommon_h
|
|
|
|
#define mozilla_css_AnimationCommon_h
|
|
|
|
|
2015-09-15 08:05:49 +03:00
|
|
|
#include <algorithm> // For <std::stable_sort>
|
2016-02-24 10:08:20 +03:00
|
|
|
#include "mozilla/AnimationCollection.h"
|
2015-09-15 08:05:49 +03:00
|
|
|
#include "mozilla/AnimationComparator.h"
|
2015-07-29 04:57:39 +03:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2015-08-18 07:13:14 +03:00
|
|
|
#include "mozilla/LinkedList.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2015-04-21 04:22:09 +03:00
|
|
|
#include "mozilla/dom/Animation.h"
|
2017-03-26 23:46:00 +03:00
|
|
|
#include "mozilla/AnimationTarget.h"
|
2016-02-24 10:08:20 +03:00
|
|
|
#include "mozilla/Attributes.h" // For MOZ_NON_OWNING_REF
|
2014-10-17 09:14:02 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2017-02-20 05:02:52 +03:00
|
|
|
#include "mozilla/TimingParams.h"
|
2015-06-09 05:13:54 +03:00
|
|
|
#include "nsContentUtils.h"
|
2013-10-22 16:14:41 +04:00
|
|
|
#include "nsCSSPseudoElements.h"
|
2014-07-16 04:02:30 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-10-23 00:55:14 +04:00
|
|
|
|
2013-10-22 14:30:45 +04:00
|
|
|
class nsIFrame;
|
2014-06-12 21:09:31 +04:00
|
|
|
class nsPresContext;
|
2011-04-12 10:18:43 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2017-03-06 07:19:09 +03:00
|
|
|
enum class CSSPseudoElementType : uint8_t;
|
2014-06-24 10:29:54 +04:00
|
|
|
|
2016-02-24 10:08:20 +03:00
|
|
|
namespace dom {
|
|
|
|
class Element;
|
|
|
|
}
|
2014-06-24 10:29:54 +04:00
|
|
|
|
2016-03-09 06:55:39 +03:00
|
|
|
template <class AnimationType>
|
2016-01-15 09:15:47 +03:00
|
|
|
class CommonAnimationManager {
|
2011-04-12 10:18:43 +04:00
|
|
|
public:
|
2016-03-09 06:55:39 +03:00
|
|
|
explicit CommonAnimationManager(nsPresContext *aPresContext)
|
|
|
|
: mPresContext(aPresContext)
|
|
|
|
{
|
|
|
|
}
|
2011-04-12 10:18:43 +04:00
|
|
|
|
2015-04-01 01:05:54 +03:00
|
|
|
// NOTE: This can return null after Disconnect().
|
|
|
|
nsPresContext* PresContext() const { return mPresContext; }
|
|
|
|
|
2011-04-12 10:18:43 +04:00
|
|
|
/**
|
|
|
|
* Notify the manager that the pres context is going away.
|
|
|
|
*/
|
2016-03-09 06:55:39 +03:00
|
|
|
void Disconnect()
|
|
|
|
{
|
|
|
|
// Content nodes might outlive the transition or animation manager.
|
|
|
|
RemoveAllElementCollections();
|
|
|
|
|
|
|
|
mPresContext = nullptr;
|
|
|
|
}
|
2011-04-12 10:18:43 +04:00
|
|
|
|
2017-03-06 07:19:09 +03:00
|
|
|
/**
|
|
|
|
* Stop animations on the element. This method takes the real element
|
|
|
|
* rather than the element for the generated content for animations on
|
|
|
|
* ::before and ::after.
|
|
|
|
*/
|
|
|
|
void StopAnimationsForElement(dom::Element* aElement,
|
|
|
|
CSSPseudoElementType aPseudoType)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
AnimationCollection<AnimationType>* collection =
|
|
|
|
AnimationCollection<AnimationType>::GetAnimationCollection(aElement,
|
|
|
|
aPseudoType);
|
|
|
|
if (!collection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoAnimationMutationBatch mb(aElement->OwnerDoc());
|
|
|
|
collection->Destroy();
|
|
|
|
}
|
|
|
|
|
2011-04-12 10:18:43 +04:00
|
|
|
protected:
|
2016-03-09 06:55:39 +03:00
|
|
|
virtual ~CommonAnimationManager()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mPresContext, "Disconnect should have been called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddElementCollection(AnimationCollection<AnimationType>* aCollection)
|
|
|
|
{
|
|
|
|
mElementCollections.insertBack(aCollection);
|
|
|
|
}
|
|
|
|
void RemoveAllElementCollections()
|
|
|
|
{
|
|
|
|
while (AnimationCollection<AnimationType>* head =
|
|
|
|
mElementCollections.getFirst()) {
|
|
|
|
head->Destroy(); // Note: this removes 'head' from mElementCollections.
|
|
|
|
}
|
|
|
|
}
|
2011-04-12 10:18:43 +04:00
|
|
|
|
2016-03-09 06:55:39 +03:00
|
|
|
LinkedList<AnimationCollection<AnimationType>> mElementCollections;
|
2011-04-12 10:18:43 +04:00
|
|
|
nsPresContext *mPresContext; // weak (non-null from ctor to Disconnect)
|
|
|
|
};
|
|
|
|
|
2015-06-09 05:13:54 +03:00
|
|
|
/**
|
|
|
|
* Utility class for referencing the element that created a CSS animation or
|
|
|
|
* transition. It is non-owning (i.e. it uses a raw pointer) since it is only
|
|
|
|
* expected to be set by the owned animation while it actually being managed
|
|
|
|
* by the owning element.
|
|
|
|
*
|
|
|
|
* This class also abstracts the comparison of an element/pseudo-class pair
|
|
|
|
* for the sake of composite ordering since this logic is common to both CSS
|
|
|
|
* animations and transitions.
|
|
|
|
*
|
|
|
|
* (We call this OwningElementRef instead of just OwningElement so that we can
|
|
|
|
* call the getter on CSSAnimation/CSSTransition OwningElement() without
|
|
|
|
* clashing with this object's contructor.)
|
|
|
|
*/
|
|
|
|
class OwningElementRef final
|
|
|
|
{
|
|
|
|
public:
|
2017-03-26 23:46:00 +03:00
|
|
|
OwningElementRef() = default;
|
|
|
|
|
|
|
|
explicit OwningElementRef(const NonOwningAnimationTarget& aTarget)
|
|
|
|
: mTarget(aTarget)
|
2015-06-09 05:13:54 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
OwningElementRef(dom::Element& aElement,
|
2016-02-17 23:37:00 +03:00
|
|
|
CSSPseudoElementType aPseudoType)
|
2017-03-26 23:46:00 +03:00
|
|
|
: mTarget(&aElement, aPseudoType)
|
2015-06-09 05:13:54 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
bool Equals(const OwningElementRef& aOther) const
|
|
|
|
{
|
2017-03-26 23:46:00 +03:00
|
|
|
return mTarget == aOther.mTarget;
|
2015-06-09 05:13:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LessThan(const OwningElementRef& aOther) const
|
|
|
|
{
|
2017-03-26 23:46:00 +03:00
|
|
|
MOZ_ASSERT(mTarget.mElement && aOther.mTarget.mElement,
|
2015-06-09 05:13:54 +03:00
|
|
|
"Elements to compare should not be null");
|
|
|
|
|
2017-03-26 23:46:00 +03:00
|
|
|
if (mTarget.mElement != aOther.mTarget.mElement) {
|
|
|
|
return nsContentUtils::PositionIsBefore(mTarget.mElement,
|
|
|
|
aOther.mTarget.mElement);
|
2015-06-09 05:13:54 +03:00
|
|
|
}
|
|
|
|
|
2017-03-26 23:46:00 +03:00
|
|
|
return mTarget.mPseudoType == CSSPseudoElementType::NotPseudo ||
|
|
|
|
(mTarget.mPseudoType == CSSPseudoElementType::before &&
|
|
|
|
aOther.mTarget.mPseudoType == CSSPseudoElementType::after);
|
2015-06-09 05:13:54 +03:00
|
|
|
}
|
|
|
|
|
2017-03-26 23:46:00 +03:00
|
|
|
bool IsSet() const { return !!mTarget.mElement; }
|
2015-06-09 05:13:54 +03:00
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
void GetElement(dom::Element*& aElement,
|
2017-03-26 23:46:00 +03:00
|
|
|
CSSPseudoElementType& aPseudoType) const
|
|
|
|
{
|
|
|
|
aElement = mTarget.mElement;
|
|
|
|
aPseudoType = mTarget.mPseudoType;
|
2015-07-29 04:57:39 +03:00
|
|
|
}
|
|
|
|
|
2015-06-09 05:13:54 +03:00
|
|
|
private:
|
2017-03-26 23:46:00 +03:00
|
|
|
NonOwningAnimationTarget mTarget;
|
2015-06-09 05:13:54 +03:00
|
|
|
};
|
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
template <class EventInfo>
|
|
|
|
class DelayedEventDispatcher
|
|
|
|
{
|
|
|
|
public:
|
2015-09-15 08:05:49 +03:00
|
|
|
DelayedEventDispatcher() : mIsSorted(true) { }
|
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
void QueueEvent(EventInfo&& aEventInfo)
|
|
|
|
{
|
2015-09-15 08:05:49 +03:00
|
|
|
mPendingEvents.AppendElement(Forward<EventInfo>(aEventInfo));
|
|
|
|
mIsSorted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is exposed as a separate method so that when we are dispatching
|
|
|
|
// *both* transition events and animation events we can sort both lists
|
|
|
|
// once using the current state of the document before beginning any
|
|
|
|
// dispatch.
|
|
|
|
void SortEvents()
|
|
|
|
{
|
|
|
|
if (mIsSorted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Replace with mPendingEvents.StableSort when bug 1147091 is
|
|
|
|
// fixed.
|
|
|
|
std::stable_sort(mPendingEvents.begin(), mPendingEvents.end(),
|
|
|
|
EventInfoLessThan());
|
|
|
|
mIsSorted = true;
|
2015-07-29 04:57:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a reference to the owning manager's pres context so it can
|
|
|
|
// detect if the pres context is destroyed while dispatching one of
|
|
|
|
// the events.
|
2015-09-15 08:05:49 +03:00
|
|
|
//
|
|
|
|
// This will call SortEvents automatically if it has not already been
|
|
|
|
// called.
|
2015-07-29 04:57:39 +03:00
|
|
|
void DispatchEvents(nsPresContext* const & aPresContext)
|
|
|
|
{
|
|
|
|
if (!aPresContext || mPendingEvents.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-15 08:05:49 +03:00
|
|
|
SortEvents();
|
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
EventArray events;
|
|
|
|
mPendingEvents.SwapElements(events);
|
2015-09-15 08:05:49 +03:00
|
|
|
// mIsSorted will be set to true by SortEvents above, and we leave it
|
|
|
|
// that way since mPendingEvents is now empty
|
2015-07-29 04:57:39 +03:00
|
|
|
for (EventInfo& info : events) {
|
|
|
|
EventDispatcher::Dispatch(info.mElement, aPresContext, &info.mEvent);
|
|
|
|
|
|
|
|
if (!aPresContext) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 08:05:49 +03:00
|
|
|
void ClearEventQueue()
|
|
|
|
{
|
|
|
|
mPendingEvents.Clear();
|
|
|
|
mIsSorted = true;
|
|
|
|
}
|
2015-07-29 04:57:39 +03:00
|
|
|
bool HasQueuedEvents() const { return !mPendingEvents.IsEmpty(); }
|
|
|
|
|
|
|
|
// Methods for supporting cycle-collection
|
|
|
|
void Traverse(nsCycleCollectionTraversalCallback* aCallback,
|
|
|
|
const char* aName)
|
|
|
|
{
|
|
|
|
for (EventInfo& info : mPendingEvents) {
|
|
|
|
ImplCycleCollectionTraverse(*aCallback, info.mElement, aName);
|
2015-09-15 08:05:44 +03:00
|
|
|
ImplCycleCollectionTraverse(*aCallback, info.mAnimation, aName);
|
2015-07-29 04:57:39 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-15 08:05:49 +03:00
|
|
|
void Unlink() { ClearEventQueue(); }
|
2015-07-29 04:57:39 +03:00
|
|
|
|
|
|
|
protected:
|
2015-09-15 08:05:49 +03:00
|
|
|
class EventInfoLessThan
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator()(const EventInfo& a, const EventInfo& b) const
|
|
|
|
{
|
|
|
|
if (a.mTimeStamp != b.mTimeStamp) {
|
|
|
|
// Null timestamps sort first
|
|
|
|
if (a.mTimeStamp.IsNull() || b.mTimeStamp.IsNull()) {
|
|
|
|
return a.mTimeStamp.IsNull();
|
|
|
|
} else {
|
|
|
|
return a.mTimeStamp < b.mTimeStamp;
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 04:57:39 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
AnimationPtrComparator<RefPtr<dom::Animation>> comparator;
|
2015-09-15 08:05:49 +03:00
|
|
|
return comparator.LessThan(a.mAnimation, b.mAnimation);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef nsTArray<EventInfo> EventArray;
|
2015-07-29 04:57:39 +03:00
|
|
|
EventArray mPendingEvents;
|
2015-09-15 08:05:49 +03:00
|
|
|
bool mIsSorted;
|
2015-07-29 04:57:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class EventInfo>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(DelayedEventDispatcher<EventInfo>& aField)
|
|
|
|
{
|
|
|
|
aField.Unlink();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class EventInfo>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
DelayedEventDispatcher<EventInfo>& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
aField.Traverse(&aCallback, aName);
|
|
|
|
}
|
|
|
|
|
2017-02-10 06:32:44 +03:00
|
|
|
// Return the TransitionPhase or AnimationPhase to use when the animation
|
|
|
|
// doesn't have a target effect.
|
|
|
|
template <typename PhaseType>
|
|
|
|
PhaseType GetAnimationPhaseWithoutEffect(const dom::Animation& aAnimation)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aAnimation.GetEffect(),
|
|
|
|
"Should only be called when we do not have an effect");
|
|
|
|
|
|
|
|
Nullable<TimeDuration> currentTime = aAnimation.GetCurrentTime();
|
|
|
|
if (currentTime.IsNull()) {
|
|
|
|
return PhaseType::Idle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a target effect, the duration will be zero so the phase is
|
|
|
|
// 'before' if the current time is less than zero.
|
|
|
|
return currentTime.Value() < TimeDuration()
|
|
|
|
? PhaseType::Before
|
|
|
|
: PhaseType::After;
|
|
|
|
};
|
|
|
|
|
2017-02-20 05:02:52 +03:00
|
|
|
inline TimingParams
|
|
|
|
TimingParamsFromCSSParams(float aDuration, float aDelay,
|
|
|
|
float aIterationCount,
|
|
|
|
dom::PlaybackDirection aDirection,
|
|
|
|
dom::FillMode aFillMode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aIterationCount >= 0.0 && !IsNaN(aIterationCount),
|
|
|
|
"aIterations should be nonnegative & finite, as ensured by "
|
|
|
|
"CSSParser");
|
|
|
|
|
|
|
|
TimingParams timing;
|
|
|
|
timing.mDuration.emplace(StickyTimeDuration::FromMilliseconds(aDuration));
|
|
|
|
timing.mDelay = TimeDuration::FromMilliseconds(aDelay);
|
|
|
|
timing.mIterations = aIterationCount;
|
|
|
|
timing.mDirection = aDirection;
|
|
|
|
timing.mFill = aFillMode;
|
|
|
|
|
|
|
|
return timing;
|
|
|
|
}
|
|
|
|
|
2015-06-09 05:13:54 +03:00
|
|
|
} // namespace mozilla
|
2011-04-12 10:18:43 +04:00
|
|
|
|
|
|
|
#endif /* !defined(mozilla_css_AnimationCommon_h) */
|