2023-11-04 08:16:37 -07:00
|
|
|
|
/*
|
2024-02-02 15:01:30 -07:00
|
|
|
|
* Copyright (c) 2023-2024, Matthew Olsson <mattco@serenityos.org>.
|
2023-11-04 08:16:37 -07:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2023-11-04 11:58:09 -07:00
|
|
|
|
#include <LibWeb/Animations/Animation.h>
|
2023-11-04 08:16:37 -07:00
|
|
|
|
#include <LibWeb/Animations/AnimationTimeline.h>
|
2024-04-27 12:09:58 +12:00
|
|
|
|
#include <LibWeb/Bindings/AnimationTimelinePrototype.h>
|
2023-11-04 08:16:37 -07:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Web::Animations {
|
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(AnimationTimeline);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
|
2025-07-29 10:15:12 +02:00
|
|
|
|
// https://drafts.csswg.org/web-animations-1/#dom-animationtimeline-currenttime
|
2025-11-30 20:31:57 +13:00
|
|
|
|
Optional<TimeValue> AnimationTimeline::current_time() const
|
2025-07-29 10:15:12 +02:00
|
|
|
|
{
|
|
|
|
|
|
// Returns the current time for this timeline or null if this timeline is inactive.
|
|
|
|
|
|
if (is_inactive())
|
|
|
|
|
|
return {};
|
|
|
|
|
|
return m_current_time;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-30 20:31:57 +13:00
|
|
|
|
void AnimationTimeline::set_current_time(Optional<TimeValue> value)
|
2023-11-04 08:16:37 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (value == m_current_time)
|
2024-02-02 15:01:30 -07:00
|
|
|
|
return;
|
2023-11-04 08:16:37 -07:00
|
|
|
|
|
2025-07-29 10:15:12 +02:00
|
|
|
|
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;
|
2023-11-04 08:16:37 -07:00
|
|
|
|
}
|
2025-07-29 10:15:12 +02:00
|
|
|
|
|
2025-12-16 14:01:48 +13:00
|
|
|
|
m_current_time = value;
|
2023-11-04 08:16:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-28 20:07:13 +13:00
|
|
|
|
// https://drafts.csswg.org/web-animations-2/#timeline-duration
|
|
|
|
|
|
NullableCSSNumberish AnimationTimeline::duration_for_bindings() const
|
|
|
|
|
|
{
|
|
|
|
|
|
// The duration of a timeline gives the maximum value a timeline may generate for its current time. This value is
|
|
|
|
|
|
// used to calculate the intrinsic iteration duration for the target effect of an animation that is associated with
|
|
|
|
|
|
// the timeline when the effect’s iteration duration is "auto". The value is computed such that the effect fills the
|
|
|
|
|
|
// available time. For a monotonic timeline, there is no upper bound on current time, and timeline duration is
|
|
|
|
|
|
// unresolved. For a non-monotonic (e.g. scroll) timeline, the duration has a fixed upper bound. In this case, the
|
|
|
|
|
|
// timeline is a progress-based timeline, and its timeline duration is 100%.
|
2025-12-13 11:12:03 +13:00
|
|
|
|
return NullableCSSNumberish::from_optional_css_numberish_time(realm(), duration());
|
2025-11-28 20:07:13 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
void AnimationTimeline::set_associated_document(GC::Ptr<DOM::Document> document)
|
2023-11-04 08:16:37 -07:00
|
|
|
|
{
|
2023-11-04 08:43:10 -07:00
|
|
|
|
if (document)
|
|
|
|
|
|
document->associate_with_timeline(*this);
|
|
|
|
|
|
if (m_associated_document)
|
|
|
|
|
|
m_associated_document->disassociate_with_timeline(*this);
|
2023-11-04 08:16:37 -07:00
|
|
|
|
m_associated_document = document;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-29 10:15:12 +02:00
|
|
|
|
// https://drafts.csswg.org/web-animations-1/#timeline
|
2023-11-04 08:16:37 -07:00
|
|
|
|
bool AnimationTimeline::is_inactive() const
|
|
|
|
|
|
{
|
2025-07-29 10:15:12 +02:00
|
|
|
|
// A timeline is considered to be inactive when its time value is unresolved, and active otherwise.
|
2023-11-04 08:16:37 -07:00
|
|
|
|
return !m_current_time.has_value();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AnimationTimeline::AnimationTimeline(JS::Realm& realm)
|
|
|
|
|
|
: Bindings::PlatformObject(realm)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-11 15:17:17 +01:00
|
|
|
|
void AnimationTimeline::finalize()
|
2023-11-04 08:43:10 -07:00
|
|
|
|
{
|
2026-01-29 20:46:37 +01:00
|
|
|
|
Base::finalize();
|
2023-11-04 08:43:10 -07:00
|
|
|
|
if (m_associated_document)
|
|
|
|
|
|
m_associated_document->disassociate_with_timeline(*this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-04 08:16:37 -07:00
|
|
|
|
void AnimationTimeline::initialize(JS::Realm& realm)
|
|
|
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(AnimationTimeline);
|
2025-04-20 16:22:57 +02:00
|
|
|
|
Base::initialize(realm);
|
2023-11-04 08:16:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AnimationTimeline::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
|
{
|
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
|
visitor.visit(m_associated_document);
|
2025-12-18 13:53:21 +13:00
|
|
|
|
// We intentionally don't visit m_associated_animations here to avoid keeping Animations alive solely because they
|
|
|
|
|
|
// are associated with a timeline. Animations are disassociated from timelines in Animation::finalize() so we don't
|
|
|
|
|
|
// need to worry about dangling references.
|
2023-11-04 08:16:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|