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
|
|
|
|
|
Optional<double> AnimationTimeline::current_time() const
|
|
|
|
|
{
|
|
|
|
|
// Returns the current time for this timeline or null if this timeline is inactive.
|
|
|
|
|
if (is_inactive())
|
|
|
|
|
return {};
|
|
|
|
|
return m_current_time;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 15:01:30 -07:00
|
|
|
void AnimationTimeline::set_current_time(Optional<double> 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
|
|
|
}
|
|
|
|
|
m_current_time = value;
|
2025-07-29 10:15:12 +02:00
|
|
|
|
2024-12-27 16:10:18 -05:00
|
|
|
// The loop might modify the content of m_associated_animations, so let's iterate over a copy.
|
|
|
|
|
auto temporary_copy = GC::RootVector<GC::Ref<Animation>>(vm().heap());
|
|
|
|
|
temporary_copy.extend(m_associated_animations.values());
|
2025-07-29 10:15:12 +02:00
|
|
|
for (auto& animation : temporary_copy)
|
2024-02-02 15:01:30 -07:00
|
|
|
animation->notify_timeline_time_did_change();
|
2023-11-04 08:16:37 -07: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
|
|
|
{
|
|
|
|
|
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);
|
2024-04-15 13:58:21 +02:00
|
|
|
visitor.visit(m_associated_animations);
|
2023-11-04 08:16:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|