// https://w3c.github.io/web-animations/#finish-an-animation void Animation::Finish(ErrorResult& aRv) { if (mPlaybackRate == 0 || (mPlaybackRate > 0 && EffectEnd() == TimeDuration::Forever())) { aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); return; } AutoMutationBatchForAnimation mb(*this); // Seek to the end TimeDuration limit = mPlaybackRate > 0 ? TimeDuration(EffectEnd()) : TimeDuration(0); bool didChange = GetCurrentTime() != Nullable<TimeDuration>(limit); SilentlySetCurrentTime(limit); // If we are paused or play-pending we need to fill in the start time in // order to transition to the finished state. // // We only do this, however, if we have an active timeline. If we have an // inactive timeline we can't transition into the finished state just like // we can't transition to the running state (this finished state is really // a substate of the running state). if (mStartTime.IsNull() && mTimeline && !mTimeline->GetCurrentTime().IsNull()) { mStartTime.SetValue(mTimeline->GetCurrentTime().Value() - limit.MultDouble(1.0 / mPlaybackRate)); didChange = true; } // If we just resolved the start time for a pause or play-pending // animation, we need to clear the task. We don't do this as a branch of // the above however since we can have a play-pending animation with a // resolved start time if we aborted a pause operation. if (!mStartTime.IsNull() && (mPendingState == PendingState::PlayPending || mPendingState == PendingState::PausePending)) { if (mPendingState == PendingState::PausePending) { mHoldTime.SetNull(); } CancelPendingTasks(); didChange = true; if (mReady) { mReady->MaybeResolve(this); } } UpdateTiming(SeekFlag::DidSeek, SyncNotifyFlag::Sync); if (didChange && IsRelevant()) { nsNodeUtils::AnimationChanged(this); } PostUpdate(); }
// https://w3c.github.io/web-animations/#pause-an-animation void Animation::DoPause(ErrorResult& aRv) { if (IsPausedOrPausing()) { return; } AutoMutationBatchForAnimation mb(*this); // If we are transitioning from idle, fill in the current time if (GetCurrentTime().IsNull()) { if (mPlaybackRate >= 0.0) { mHoldTime.SetValue(TimeDuration(0)); } else { if (EffectEnd() == TimeDuration::Forever()) { aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); return; } mHoldTime.SetValue(TimeDuration(EffectEnd())); } } bool reuseReadyPromise = false; if (mPendingState == PendingState::PlayPending) { CancelPendingTasks(); reuseReadyPromise = true; } if (!reuseReadyPromise) { // Clear ready promise. We'll create a new one lazily. mReady = nullptr; } mPendingState = PendingState::PausePending; nsIDocument* doc = GetRenderedDocument(); if (doc) { PendingAnimationTracker* tracker = doc->GetOrCreatePendingAnimationTracker(); tracker->AddPausePending(*this); } else { TriggerOnNextTick(Nullable<TimeDuration>()); } UpdateTiming(SeekFlag::NoSeek, SyncNotifyFlag::Async); if (IsRelevant()) { nsNodeUtils::AnimationChanged(this); } }
// http://w3c.github.io/web-animations/#pause-an-animation void Animation::DoPause(ErrorResult& aRv) { if (IsPausedOrPausing()) { return; } // If we are transitioning from idle, fill in the current time if (GetCurrentTime().IsNull()) { if (mPlaybackRate >= 0.0) { mHoldTime.SetValue(TimeDuration(0)); } else { if (EffectEnd() == TimeDuration::Forever()) { aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); return; } mHoldTime.SetValue(TimeDuration(EffectEnd())); } } bool reuseReadyPromise = false; if (mPendingState == PendingState::PlayPending) { CancelPendingTasks(); reuseReadyPromise = true; } // Mark this as no longer running on the compositor so that next time // we update animations we won't throttle them and will have a chance // to remove the animation from any layer it might be on. mIsRunningOnCompositor = false; if (!reuseReadyPromise) { // Clear ready promise. We'll create a new one lazily. mReady = nullptr; } mPendingState = PendingState::PausePending; nsIDocument* doc = GetRenderedDocument(); if (doc) { PendingAnimationTracker* tracker = doc->GetOrCreatePendingAnimationTracker(); tracker->AddPausePending(*this); } else { TriggerOnNextTick(Nullable<TimeDuration>()); } UpdateTiming(SeekFlag::NoSeek, SyncNotifyFlag::Async); }
bool Animation::IsFinished() const { // Unfortunately there's some weirdness in the spec at the moment where if // you're finished and paused, the playState is paused. This prevents us // from just checking |PlayState() == AnimationPlayState::Finished| here, // and we need this much more messy check to see if we're finished. Nullable<TimeDuration> currentTime = GetCurrentTime(); return !currentTime.IsNull() && ((mPlaybackRate > 0.0 && currentTime.Value() >= EffectEnd()) || (mPlaybackRate < 0.0 && currentTime.Value().ToMilliseconds() <= 0.0)); }
void Animation::UpdateFinishedState(bool aSeekFlag) { Nullable<TimeDuration> currentTime = GetCurrentTime(); TimeDuration effectEnd = TimeDuration(EffectEnd()); if (!mStartTime.IsNull() && mPendingState == PendingState::NotPending) { if (mPlaybackRate > 0.0 && !currentTime.IsNull() && currentTime.Value() >= effectEnd) { if (aSeekFlag) { mHoldTime = currentTime; } else if (!mPreviousCurrentTime.IsNull()) { mHoldTime.SetValue(std::max(mPreviousCurrentTime.Value(), effectEnd)); } else { mHoldTime.SetValue(effectEnd); } } else if (mPlaybackRate < 0.0 && !currentTime.IsNull() && currentTime.Value().ToMilliseconds() <= 0.0) { if (aSeekFlag) { mHoldTime = currentTime; } else { mHoldTime.SetValue(0); } } else if (mPlaybackRate != 0.0 && !currentTime.IsNull()) { if (aSeekFlag && !mHoldTime.IsNull()) { mStartTime.SetValue(mTimeline->GetCurrentTime().Value() - (mHoldTime.Value().MultDouble(1 / mPlaybackRate))); } mHoldTime.SetNull(); } } bool currentFinishedState = IsFinished(); if (currentFinishedState && !mIsPreviousStateFinished) { if (mFinished) { mFinished->MaybeResolve(this); } } else if (!currentFinishedState && mIsPreviousStateFinished) { // Clear finished promise. We'll create a new one lazily. mFinished = nullptr; if (mEffect->AsTransition()) { mEffect->SetIsFinishedTransition(false); } } mIsPreviousStateFinished = currentFinishedState; // We must recalculate the current time to take account of any mHoldTime // changes the code above made. mPreviousCurrentTime = GetCurrentTime(); }
void Animation::UpdateFinishedState(SeekFlag aSeekFlag, SyncNotifyFlag aSyncNotifyFlag) { Nullable<TimeDuration> currentTime = GetCurrentTime(); TimeDuration effectEnd = TimeDuration(EffectEnd()); if (!mStartTime.IsNull() && mPendingState == PendingState::NotPending) { if (mPlaybackRate > 0.0 && !currentTime.IsNull() && currentTime.Value() >= effectEnd) { if (aSeekFlag == SeekFlag::DidSeek) { mHoldTime = currentTime; } else if (!mPreviousCurrentTime.IsNull()) { mHoldTime.SetValue(std::max(mPreviousCurrentTime.Value(), effectEnd)); } else { mHoldTime.SetValue(effectEnd); } } else if (mPlaybackRate < 0.0 && !currentTime.IsNull() && currentTime.Value().ToMilliseconds() <= 0.0) { if (aSeekFlag == SeekFlag::DidSeek) { mHoldTime = currentTime; } else { mHoldTime.SetValue(0); } } else if (mPlaybackRate != 0.0 && !currentTime.IsNull() && mTimeline && !mTimeline->GetCurrentTime().IsNull()) { if (aSeekFlag == SeekFlag::DidSeek && !mHoldTime.IsNull()) { mStartTime.SetValue(mTimeline->GetCurrentTime().Value() - (mHoldTime.Value().MultDouble(1 / mPlaybackRate))); } mHoldTime.SetNull(); } } bool currentFinishedState = PlayState() == AnimationPlayState::Finished; if (currentFinishedState && !mFinishedIsResolved) { DoFinishNotification(aSyncNotifyFlag); } else if (!currentFinishedState && mFinishedIsResolved) { ResetFinishedPromise(); if (mEffect->AsTransition()) { mEffect->SetIsFinishedTransition(false); } } // We must recalculate the current time to take account of any mHoldTime // changes the code above made. mPreviousCurrentTime = GetCurrentTime(); }
// https://w3c.github.io/web-animations/#finish-an-animation void Animation::Finish(ErrorResult& aRv) { if (mPlaybackRate == 0 || (mPlaybackRate > 0 && EffectEnd() == TimeDuration::Forever())) { aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); return; } TimeDuration limit = mPlaybackRate > 0 ? TimeDuration(EffectEnd()) : TimeDuration(0); SetCurrentTime(limit); if (mPendingState == PendingState::PlayPending) { CancelPendingTasks(); if (mReady) { mReady->MaybeResolve(this); } } UpdateFinishedState(true); PostUpdate(); }
AnimationPlayState Animation::PlayState() const { if (mPendingState != PendingState::NotPending) { return AnimationPlayState::Pending; } Nullable<TimeDuration> currentTime = GetCurrentTime(); if (currentTime.IsNull()) { return AnimationPlayState::Idle; } if (mStartTime.IsNull()) { return AnimationPlayState::Paused; } if ((mPlaybackRate > 0.0 && currentTime.Value() >= EffectEnd()) || (mPlaybackRate < 0.0 && currentTime.Value().ToMilliseconds() <= 0.0)) { return AnimationPlayState::Finished; } return AnimationPlayState::Running; }
// http://w3c.github.io/web-animations/#play-an-animation void Animation::DoPlay(LimitBehavior aLimitBehavior) { bool abortedPause = mPendingState == PendingState::PausePending; bool reuseReadyPromise = false; if (mPendingState != PendingState::NotPending) { CancelPendingTasks(); reuseReadyPromise = true; } Nullable<TimeDuration> currentTime = GetCurrentTime(); if (mPlaybackRate > 0.0 && (currentTime.IsNull() || (aLimitBehavior == LimitBehavior::AutoRewind && (currentTime.Value().ToMilliseconds() < 0.0 || currentTime.Value() >= EffectEnd())))) { mHoldTime.SetValue(TimeDuration(0)); } else if (mPlaybackRate < 0.0 && (currentTime.IsNull() || (aLimitBehavior == LimitBehavior::AutoRewind && (currentTime.Value().ToMilliseconds() <= 0.0 || currentTime.Value() > EffectEnd())))) { mHoldTime.SetValue(TimeDuration(EffectEnd())); } else if (mPlaybackRate == 0.0 && currentTime.IsNull()) { mHoldTime.SetValue(TimeDuration(0)); } // If the hold time is null then we're either already playing normally (and // we can ignore this call) or we aborted a pending pause operation (in which // case, for consistency, we need to go through the motions of doing an // asynchronous start even though we already have a resolved start time). if (mHoldTime.IsNull() && !abortedPause) { return; } // Clear the start time until we resolve a new one. We do this except // for the case where we are aborting a pause and don't have a hold time. // // If we're aborting a pause and *do* have a hold time (e.g. because // the animation is finished or we just applied the auto-rewind behavior // above) we should respect it by clearing the start time. If we *don't* // have a hold time we should keep the current start time so that the // the animation continues moving uninterrupted by the aborted pause. // // (If we're not aborting a pause, mHoldTime must be resolved by now // or else we would have returned above.) if (!mHoldTime.IsNull()) { mStartTime.SetNull(); } if (!reuseReadyPromise) { // Clear ready promise. We'll create a new one lazily. mReady = nullptr; } mPendingState = PendingState::PlayPending; nsIDocument* doc = GetRenderedDocument(); if (!doc) { TriggerOnNextTick(Nullable<TimeDuration>()); return; } PendingAnimationTracker* tracker = doc->GetOrCreatePendingAnimationTracker(); tracker->AddPlayPending(*this); // We may have updated the current time when we set the hold time above. UpdateTiming(); }
// https://w3c.github.io/web-animations/#play-an-animation void Animation::PlayNoUpdate(ErrorResult& aRv, LimitBehavior aLimitBehavior) { AutoMutationBatchForAnimation mb(*this); bool abortedPause = mPendingState == PendingState::PausePending; Nullable<TimeDuration> currentTime = GetCurrentTime(); if (mPlaybackRate > 0.0 && (currentTime.IsNull() || (aLimitBehavior == LimitBehavior::AutoRewind && (currentTime.Value() < TimeDuration() || currentTime.Value() >= EffectEnd())))) { mHoldTime.SetValue(TimeDuration(0)); } else if (mPlaybackRate < 0.0 && (currentTime.IsNull() || (aLimitBehavior == LimitBehavior::AutoRewind && (currentTime.Value() <= TimeDuration() || currentTime.Value() > EffectEnd())))) { if (EffectEnd() == TimeDuration::Forever()) { aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); return; } mHoldTime.SetValue(TimeDuration(EffectEnd())); } else if (mPlaybackRate == 0.0 && currentTime.IsNull()) { mHoldTime.SetValue(TimeDuration(0)); } bool reuseReadyPromise = false; if (mPendingState != PendingState::NotPending) { CancelPendingTasks(); reuseReadyPromise = true; } // If the hold time is null then we're either already playing normally (and // we can ignore this call) or we aborted a pending pause operation (in which // case, for consistency, we need to go through the motions of doing an // asynchronous start even though we already have a resolved start time). if (mHoldTime.IsNull() && !abortedPause) { return; } // Clear the start time until we resolve a new one. We do this except // for the case where we are aborting a pause and don't have a hold time. // // If we're aborting a pause and *do* have a hold time (e.g. because // the animation is finished or we just applied the auto-rewind behavior // above) we should respect it by clearing the start time. If we *don't* // have a hold time we should keep the current start time so that the // the animation continues moving uninterrupted by the aborted pause. // // (If we're not aborting a pause, mHoldTime must be resolved by now // or else we would have returned above.) if (!mHoldTime.IsNull()) { mStartTime.SetNull(); } if (!reuseReadyPromise) { // Clear ready promise. We'll create a new one lazily. mReady = nullptr; } mPendingState = PendingState::PlayPending; nsIDocument* doc = GetRenderedDocument(); if (doc) { PendingAnimationTracker* tracker = doc->GetOrCreatePendingAnimationTracker(); tracker->AddPlayPending(*this); } else { TriggerOnNextTick(Nullable<TimeDuration>()); } UpdateTiming(SeekFlag::NoSeek, SyncNotifyFlag::Async); if (IsRelevant()) { nsNodeUtils::AnimationChanged(this); } }