LibWeb: Update animations in AnimationTimeline::set_current_time

These steps need to be run whenever update_current_time is called and in
a future commit that will be more than just the one place currently.

This also removes the early return in `set_current_time` if the new
`current time` is the same as the old one, since we want to update
animations regardless (e.g. to run pending tasks)
This commit is contained in:
Callum Law 2026-04-23 20:52:16 +12:00 committed by Alexander Kalenik
parent 468a372b66
commit 34f0eea89a
5 changed files with 24 additions and 32 deletions

View file

@ -24,15 +24,32 @@ Optional<TimeValue> AnimationTimeline::current_time() const
void AnimationTimeline::set_current_time(Optional<TimeValue> value)
{
if (value == m_current_time)
return;
if (m_is_monotonically_increasing && m_current_time.has_value() && (!value.has_value() || *value < *m_current_time)) {
dbgln("AnimationTimeline::set_current_time({}): monotonically increasing timeline can only move forward", value);
return;
}
m_current_time = value;
update_associated_animations_and_dispatch_events();
}
void AnimationTimeline::update_associated_animations_and_dispatch_events()
{
// https://drafts.csswg.org/web-animations-1/#animation-frame-loop
// Note: Due to the hierarchical nature of the timing model, updating the current time of a timeline also involves:
// - Updating the current time of any animations associated with the timeline.
// - Running the update an animation's finished state procedure for any animations whose current time has been
// updated.
// - Queueing animation events for any such animations.
for (auto& animation : m_associated_animations)
animation.update();
auto animations = GC::RootVector<GC::Ref<Animations::Animation>> { heap() };
for (auto& animation : m_associated_animations)
animations.append(animation);
for (auto& animation : animations)
m_associated_document->dispatch_events_for_animation_if_necessary(animation);
}
// https://drafts.csswg.org/web-animations-2/#timeline-duration

View file

@ -54,6 +54,7 @@ protected:
virtual void finalize() override;
void set_current_time(Optional<TimeValue> value);
void update_associated_animations_and_dispatch_events();
// https://www.w3.org/TR/web-animations-1/#dom-animationtimeline-currenttime
Optional<TimeValue> m_current_time {};

View file

@ -6100,25 +6100,9 @@ void Document::update_animations_and_send_events(double timestamp)
{
HTML::TemporaryExecutionContext temporary_execution_context { realm() };
// 1. Update the current time of all timelines associated with doc passing now as the timestamp.
//
// Note: Due to the hierarchical nature of the timing model, updating the current time of a timeline also involves:
// - Updating the current time of any animations associated with the timeline.
// - Running the update an animations finished state procedure for any animations whose current time has been
// updated.
// - Queueing animation events for any such animations.
for (auto const& timeline : timelines_to_update) {
for (auto const& timeline : timelines_to_update)
timeline->update_current_time(timestamp);
for (auto& animation : timeline->associated_animations())
animation.update();
auto animations = GC::RootVector<GC::Ref<Animations::Animation>> { heap() };
for (auto& animation : timeline->associated_animations())
animations.append(animation);
for (auto& animation : animations)
dispatch_events_for_animation_if_necessary(animation);
}
// 2. Remove replaced animations for doc.
remove_replaced_animations();

View file

@ -824,6 +824,7 @@ public:
};
void append_pending_animation_event(PendingAnimationEvent const&);
void update_animations_and_send_events(double timestamp);
void dispatch_events_for_animation_if_necessary(GC::Ref<Animations::Animation>);
void remove_replaced_animations();
WebIDL::ExceptionOr<Vector<GC::Ref<Animations::Animation>>> get_animations();
@ -1146,7 +1147,6 @@ private:
Element* find_a_potential_indicated_element(FlyString const& fragment) const;
void dispatch_events_for_transition(GC::Ref<CSS::CSSTransition>);
void dispatch_events_for_animation_if_necessary(GC::Ref<Animations::Animation>);
template<typename GetNotifier, typename... Args>
void notify_each_document_observer(GetNotifier&& get_notifier, Args&&... args)

View file

@ -17,22 +17,12 @@ GC_DEFINE_ALLOCATOR(InternalAnimationTimeline);
void InternalAnimationTimeline::update_current_time(double)
{
// Do nothing
update_associated_animations_and_dispatch_events();
}
void InternalAnimationTimeline::set_time(Optional<double> time)
{
set_current_time(time.map([](double value) -> Animations::TimeValue { return { Animations::TimeValue::Type::Milliseconds, value }; }));
// https://drafts.csswg.org/web-animations-1/#animation-frame-loop
// Note: Due to the hierarchical nature of the timing model, updating the current time of a timeline also involves:
// - Updating the current time of any animations associated with the timeline.
// - Running the update an animation's finished state procedure for any animations whose current time has been
// updated.
// - Queueing animation events for any such animations.
// NB: This mirrors what the event loop does for DocumentTimeline in Document::update_animations_and_send_events().
for (auto& animation : associated_animations())
animation.update();
}
InternalAnimationTimeline::InternalAnimationTimeline(JS::Realm& realm, GC::Ref<DOM::Document> document)