2016-02-24 10:08:20 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/AnimationCollection.h"
|
|
|
|
|
2017-02-13 06:21:33 +03:00
|
|
|
#include "mozilla/RestyleManager.h"
|
2020-05-06 01:41:02 +03:00
|
|
|
#include "nsDOMMutationObserver.h" // For nsAutoAnimationMutationBatch
|
2020-05-06 01:41:04 +03:00
|
|
|
#include "mozilla/dom/CSSAnimation.h" // For dom::CSSAnimation
|
2020-05-06 01:41:02 +03:00
|
|
|
#include "mozilla/dom/CSSTransition.h" // For dom::CSSTransition
|
2016-02-24 10:08:20 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2016-03-09 06:55:39 +03:00
|
|
|
template <class AnimationType>
|
|
|
|
/* static */ void AnimationCollection<AnimationType>::PropertyDtor(
|
2017-10-03 01:05:19 +03:00
|
|
|
void* aObject, nsAtom* aPropertyName, void* aPropertyValue, void* aData) {
|
2016-02-24 10:08:20 +03:00
|
|
|
AnimationCollection* collection =
|
|
|
|
static_cast<AnimationCollection*>(aPropertyValue);
|
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(!collection->mCalledPropertyDtor, "can't call dtor twice");
|
|
|
|
collection->mCalledPropertyDtor = true;
|
|
|
|
#endif
|
2019-04-18 09:49:25 +03:00
|
|
|
|
|
|
|
PostRestyleMode postRestyle = collection->mCalledDestroy
|
|
|
|
? PostRestyleMode::IfNeeded
|
|
|
|
: PostRestyleMode::Never;
|
2016-02-24 10:08:20 +03:00
|
|
|
{
|
|
|
|
nsAutoAnimationMutationBatch mb(collection->mElement->OwnerDoc());
|
|
|
|
|
|
|
|
for (size_t animIdx = collection->mAnimations.Length(); animIdx-- != 0;) {
|
2019-04-18 09:49:25 +03:00
|
|
|
collection->mAnimations[animIdx]->CancelFromStyle(postRestyle);
|
2016-02-24 10:08:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
delete collection;
|
|
|
|
}
|
2016-03-09 06:55:39 +03:00
|
|
|
|
|
|
|
template <class AnimationType>
|
|
|
|
/* static */ AnimationCollection<AnimationType>*
|
|
|
|
AnimationCollection<AnimationType>::GetAnimationCollection(
|
2019-02-19 16:44:33 +03:00
|
|
|
const dom::Element* aElement, PseudoStyleType aPseudoType) {
|
2016-03-09 06:55:39 +03:00
|
|
|
if (!aElement->MayHaveAnimations()) {
|
|
|
|
// Early return for the most common case.
|
|
|
|
return nullptr;
|
2016-03-09 06:55:39 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* propName = GetPropertyAtomForPseudoType(aPseudoType);
|
2016-03-09 06:55:39 +03:00
|
|
|
if (!propName) {
|
2016-03-09 06:55:39 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-09 06:55:39 +03:00
|
|
|
return static_cast<AnimationCollection<AnimationType>*>(
|
|
|
|
aElement->GetProperty(propName));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class AnimationType>
|
|
|
|
/* static */ AnimationCollection<AnimationType>*
|
|
|
|
AnimationCollection<AnimationType>::GetAnimationCollection(
|
|
|
|
const nsIFrame* aFrame) {
|
2016-03-21 11:49:50 +03:00
|
|
|
Maybe<NonOwningAnimationTarget> pseudoElement =
|
2016-03-09 06:55:39 +03:00
|
|
|
EffectCompositor::GetAnimationElementAndPseudoForFrame(aFrame);
|
|
|
|
if (!pseudoElement) {
|
2016-03-09 06:55:39 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-09 06:55:39 +03:00
|
|
|
|
2016-03-21 11:49:50 +03:00
|
|
|
if (!pseudoElement->mElement->MayHaveAnimations()) {
|
2016-03-09 06:55:39 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-21 11:49:50 +03:00
|
|
|
return GetAnimationCollection(pseudoElement->mElement,
|
|
|
|
pseudoElement->mPseudoType);
|
2016-03-09 06:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class AnimationType>
|
|
|
|
/* static */ AnimationCollection<AnimationType>*
|
|
|
|
AnimationCollection<AnimationType>::GetOrCreateAnimationCollection(
|
2019-02-19 16:44:33 +03:00
|
|
|
dom::Element* aElement, PseudoStyleType aPseudoType,
|
2016-03-09 06:55:39 +03:00
|
|
|
bool* aCreatedCollection) {
|
|
|
|
MOZ_ASSERT(aCreatedCollection);
|
|
|
|
*aCreatedCollection = false;
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* propName = GetPropertyAtomForPseudoType(aPseudoType);
|
2016-03-09 06:55:39 +03:00
|
|
|
MOZ_ASSERT(propName,
|
|
|
|
"Should only try to create animations for one of the"
|
|
|
|
" recognized pseudo types");
|
|
|
|
|
2016-03-09 06:55:39 +03:00
|
|
|
auto collection = static_cast<AnimationCollection<AnimationType>*>(
|
|
|
|
aElement->GetProperty(propName));
|
2016-03-09 06:55:39 +03:00
|
|
|
if (!collection) {
|
2016-03-09 06:55:39 +03:00
|
|
|
// FIXME: Consider arena-allocating?
|
|
|
|
collection = new AnimationCollection<AnimationType>(aElement, propName);
|
|
|
|
nsresult rv = aElement->SetProperty(
|
|
|
|
propName, collection, &AnimationCollection<AnimationType>::PropertyDtor,
|
|
|
|
false);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("SetProperty failed");
|
|
|
|
// The collection must be destroyed via PropertyDtor, otherwise
|
|
|
|
// mCalledPropertyDtor assertion is triggered in destructor.
|
|
|
|
AnimationCollection<AnimationType>::PropertyDtor(aElement, propName,
|
|
|
|
collection, nullptr);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-09 06:55:39 +03:00
|
|
|
*aCreatedCollection = true;
|
2016-03-09 06:55:39 +03:00
|
|
|
aElement->SetMayHaveAnimations();
|
|
|
|
}
|
|
|
|
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
|
2016-03-09 06:55:39 +03:00
|
|
|
template <class AnimationType>
|
2017-10-03 01:05:19 +03:00
|
|
|
/*static*/ nsAtom*
|
2016-03-09 06:55:39 +03:00
|
|
|
AnimationCollection<AnimationType>::GetPropertyAtomForPseudoType(
|
2019-02-19 16:44:33 +03:00
|
|
|
PseudoStyleType aPseudoType) {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* propName = nullptr;
|
2016-03-09 06:55:39 +03:00
|
|
|
|
2019-02-19 16:44:33 +03:00
|
|
|
if (aPseudoType == PseudoStyleType::NotPseudo) {
|
2016-03-09 06:55:39 +03:00
|
|
|
propName = TraitsType::ElementPropertyAtom();
|
2019-02-19 16:44:33 +03:00
|
|
|
} else if (aPseudoType == PseudoStyleType::before) {
|
2016-03-09 06:55:39 +03:00
|
|
|
propName = TraitsType::BeforePropertyAtom();
|
2019-02-19 16:44:33 +03:00
|
|
|
} else if (aPseudoType == PseudoStyleType::after) {
|
2016-03-09 06:55:39 +03:00
|
|
|
propName = TraitsType::AfterPropertyAtom();
|
2019-03-26 07:48:26 +03:00
|
|
|
} else if (aPseudoType == PseudoStyleType::marker) {
|
|
|
|
propName = TraitsType::MarkerPropertyAtom();
|
2016-03-09 06:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return propName;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:08:40 +03:00
|
|
|
template <class AnimationType>
|
|
|
|
void AnimationCollection<AnimationType>::Destroy() {
|
|
|
|
mCalledDestroy = true;
|
|
|
|
|
|
|
|
// This will call our destructor.
|
|
|
|
mElement->RemoveProperty(mElementProperty);
|
|
|
|
}
|
|
|
|
|
2016-03-09 06:55:39 +03:00
|
|
|
// Explicit class instantiations
|
|
|
|
|
|
|
|
template class AnimationCollection<dom::CSSAnimation>;
|
|
|
|
template class AnimationCollection<dom::CSSTransition>;
|
|
|
|
|
2016-02-24 10:08:20 +03:00
|
|
|
} // namespace mozilla
|