moebius#89: DOM - implement animationcancel event

Issue #55
This commit is contained in:
janekptacijarabaci 2018-03-14 11:45:38 +01:00 • committed by Roy Tam
commit 901d2a277f
11 changed files with 99 additions and 73 deletions

View file

@ -251,6 +251,26 @@ ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
aField.Traverse(&aCallback, aName);
}
// 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;
};
} // namespace mozilla
#endif /* !defined(mozilla_css_AnimationCommon_h) */

View file

@ -158,12 +158,8 @@ CSSAnimation::HasLowerCompositeOrderThan(const CSSAnimation& aOther) const
}
void
CSSAnimation::QueueEvents()
CSSAnimation::QueueEvents(StickyTimeDuration aActiveTime)
{
if (!mEffect) {
return;
}
// If the animation is pending, we ignore animation events until we finish
// pending.
if (mPendingState != PendingState::NotPending) {
@ -198,39 +194,60 @@ CSSAnimation::QueueEvents()
}
nsAnimationManager* manager = presContext->AnimationManager();
ComputedTiming computedTiming = mEffect->GetComputedTiming();
ComputedTiming::AnimationPhase currentPhase = computedTiming.mPhase;
uint64_t currentIteration = computedTiming.mCurrentIteration;
if (currentPhase == mPreviousPhase &&
currentIteration == mPreviousIteration) {
return;
}
const StickyTimeDuration zeroDuration;
StickyTimeDuration intervalStartTime =
std::max(std::min(StickyTimeDuration(-mEffect->SpecifiedTiming().mDelay),
computedTiming.mActiveDuration),
zeroDuration);
StickyTimeDuration intervalEndTime =
std::max(std::min((EffectEnd() - mEffect->SpecifiedTiming().mDelay),
computedTiming.mActiveDuration),
zeroDuration);
uint64_t currentIteration = 0;
ComputedTiming::AnimationPhase currentPhase;
StickyTimeDuration intervalStartTime;
StickyTimeDuration intervalEndTime;
StickyTimeDuration iterationStartTime;
uint64_t iterationBoundary = mPreviousIteration > currentIteration
? currentIteration + 1
: currentIteration;
StickyTimeDuration iterationStartTime =
computedTiming.mDuration.MultDouble(
(iterationBoundary - computedTiming.mIterationStart));
if (!mEffect) {
currentPhase = GetAnimationPhaseWithoutEffect
<ComputedTiming::AnimationPhase>(*this);
} else {
ComputedTiming computedTiming = mEffect->GetComputedTiming();
currentPhase = computedTiming.mPhase;
currentIteration = computedTiming.mCurrentIteration;
if (currentPhase == mPreviousPhase &&
currentIteration == mPreviousIteration) {
return;
}
intervalStartTime =
std::max(std::min(StickyTimeDuration(-mEffect->SpecifiedTiming().mDelay),
computedTiming.mActiveDuration),
zeroDuration);
intervalEndTime =
std::max(std::min((EffectEnd() - mEffect->SpecifiedTiming().mDelay),
computedTiming.mActiveDuration),
zeroDuration);
uint64_t iterationBoundary = mPreviousIteration > currentIteration
? currentIteration + 1
: currentIteration;
iterationStartTime =
computedTiming.mDuration.MultDouble(
(iterationBoundary - computedTiming.mIterationStart));
}
TimeStamp startTimeStamp = ElapsedTimeToTimeStamp(intervalStartTime);
TimeStamp endTimeStamp = ElapsedTimeToTimeStamp(intervalEndTime);
TimeStamp iterationTimeStamp = ElapsedTimeToTimeStamp(iterationStartTime);
AutoTArray<AnimationEventParams, 2> events;
// Handle cancel event first
if ((mPreviousPhase != AnimationPhase::Idle &&
mPreviousPhase != AnimationPhase::After) &&
currentPhase == AnimationPhase::Idle) {
TimeStamp activeTimeStamp = ElapsedTimeToTimeStamp(aActiveTime);
events.AppendElement(AnimationEventParams{ eAnimationCancel,
aActiveTime,
activeTimeStamp });
}
switch (mPreviousPhase) {
case AnimationPhase::Null:
case AnimationPhase::Idle:
case AnimationPhase::Before:
if (currentPhase == AnimationPhase::Active) {
events.AppendElement(AnimationEventParams{ eAnimationStart,

View file

@ -76,7 +76,7 @@ public:
, mIsStylePaused(false)
, mPauseShouldStick(false)
, mNeedsNewAnimationIndexWhenRun(false)
, mPreviousPhase(ComputedTiming::AnimationPhase::Null)
, mPreviousPhase(ComputedTiming::AnimationPhase::Idle)
, mPreviousIteration(0)
{
// We might need to drop this assertion once we add a script-accessible
@ -110,8 +110,6 @@ public:
void PauseFromStyle();
void CancelFromStyle() override
{
mOwningElement = OwningElementRef();
// When an animation is disassociated with style it enters an odd state
// where its composite order is undefined until it first transitions
// out of the idle state.
@ -126,10 +124,15 @@ public:
mNeedsNewAnimationIndexWhenRun = true;
Animation::CancelFromStyle();
// We need to do this *after* calling CancelFromStyle() since
// CancelFromStyle might synchronously trigger a cancel event for which
// we need an owning element to target the event at.
mOwningElement = OwningElementRef();
}
void Tick() override;
void QueueEvents();
void QueueEvents(StickyTimeDuration aActiveTime = StickyTimeDuration());
bool IsStylePaused() const { return mIsStylePaused; }
@ -158,6 +161,10 @@ public:
// reflect changes to that markup.
bool IsTiedToMarkup() const { return mOwningElement.IsSet(); }
void MaybeQueueCancelEvent(StickyTimeDuration aActiveTime) override {
QueueEvents(aActiveTime);
}
protected:
virtual ~CSSAnimation()
{

View file

@ -46,8 +46,6 @@ using mozilla::dom::KeyframeEffectReadOnly;
using namespace mozilla;
using namespace mozilla::css;
typedef mozilla::ComputedTiming::AnimationPhase AnimationPhase;
namespace {
struct TransitionEventParams {
EventMessage mMessage;
@ -203,7 +201,7 @@ CSSTransition::QueueEvents(StickyTimeDuration aActiveTime)
StickyTimeDuration intervalEndTime;
if (!mEffect) {
currentPhase = GetTransitionPhaseWithoutEffect();
currentPhase = GetAnimationPhaseWithoutEffect<TransitionPhase>(*this);
intervalStartTime = zeroDuration;
intervalEndTime = zeroDuration;
} else {
@ -329,23 +327,6 @@ CSSTransition::QueueEvents(StickyTimeDuration aActiveTime)
}
}
CSSTransition::TransitionPhase
CSSTransition::GetTransitionPhaseWithoutEffect() const
{
MOZ_ASSERT(!mEffect, "Should only be called when we do not have an effect");
Nullable<TimeDuration> currentTime = GetCurrentTime();
if (currentTime.IsNull()) {
return TransitionPhase::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()
? TransitionPhase::Before
: TransitionPhase::After;
}
void
CSSTransition::Tick()
{

View file

@ -162,18 +162,6 @@ public:
Animation::CancelFromStyle();
// The above call to Animation::CancelFromStyle may cause a transitioncancel
// event to be queued. However, it will also remove the transition from its
// timeline. If this transition was the last animation attached to
// the timeline, the timeline will stop observing the refresh driver and
// there may be no subsequent tick fro dispatching animation events.
//
// To ensure the cancel event is dispatched we tell the timeline it needs to
// observe the refresh driver for at least one more tick.
if (mTimeline) {
mTimeline->NotifyAnimationUpdated(*this);
}
// It is important we do this *after* calling CancelFromStyle().
// This is because CancelFromStyle() will end up posting a restyle and
// that restyle should target the *transitions* level of the cascade.
@ -245,9 +233,6 @@ protected:
enum class TransitionPhase;
// Return the TransitionPhase to use when the transition doesn't have a target
// effect.
TransitionPhase GetTransitionPhaseWithoutEffect() const;
// The (pseudo-)element whose computed transition-property refers to this
// transition (if any).
@ -272,7 +257,7 @@ protected:
// to be queued on this tick.
// See: https://drafts.csswg.org/css-transitions-2/#transition-phase
enum class TransitionPhase {
Idle = static_cast<int>(ComputedTiming::AnimationPhase::Null),
Idle = static_cast<int>(ComputedTiming::AnimationPhase::Idle),
Before = static_cast<int>(ComputedTiming::AnimationPhase::Before),
Active = static_cast<int>(ComputedTiming::AnimationPhase::Active),
After = static_cast<int>(ComputedTiming::AnimationPhase::After),