ladybird/Userland/Libraries/LibWeb/Animations/DocumentTimeline.cpp
Shannon Booth bad44f8fc9 LibWeb: Remove Bindings/Forward.h from LibWeb/Forward.h
This was resulting in a whole lot of rebuilding whenever a new IDL
interface was added.

Instead, just directly include the prototype in every C++ file which
needs it. While we only really need a forward declaration in each cpp
file; including the full prototype header (which itself only includes
LibJS/Object.h, which is already transitively brought in by
PlatformObject) - it seems like a small price to pay compared to what
feels like a full rebuild of LibWeb whenever a new IDL file is added.

Given all of these includes are only needed for the ::initialize
method, there is probably a smart way of avoiding this problem
altogether. I've considered both using some macro trickery or generating
these functions somehow instead.
2024-04-27 18:29:35 -04:00

86 lines
3.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Animations/DocumentTimeline.h>
#include <LibWeb/Bindings/DocumentTimelinePrototype.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HighResolutionTime/Performance.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::Animations {
JS_DEFINE_ALLOCATOR(DocumentTimeline);
JS::NonnullGCPtr<DocumentTimeline> DocumentTimeline::create(JS::Realm& realm, DOM::Document& document, HighResolutionTime::DOMHighResTimeStamp origin_time)
{
auto timeline = realm.heap().allocate<DocumentTimeline>(realm, realm, document, origin_time);
auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
VERIFY(window_or_worker);
timeline->set_current_time(window_or_worker->performance()->now());
return timeline;
}
// https://www.w3.org/TR/web-animations-1/#dom-documenttimeline-documenttimeline
WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentTimeline>> DocumentTimeline::construct_impl(JS::Realm& realm, DocumentTimelineOptions options)
{
// Creates a new DocumentTimeline. The Document with which the timeline is associated is the Document associated
// with the Window that is the current global object.
auto& window = verify_cast<HTML::Window>(realm.global_object());
return create(realm, window.associated_document(), options.origin_time);
}
// https://www.w3.org/TR/web-animations-1/#ref-for-timeline-time-to-origin-relative-time
Optional<double> DocumentTimeline::convert_a_timeline_time_to_an_origin_relative_time(Optional<double> timeline_time)
{
// To convert a timeline time, timeline time, to an origin-relative time for a document timeline, timeline, return
// the sum of the timeline time and timelines origin time. If timeline is inactive, return an unresolved time
// value.
if (is_inactive() || !timeline_time.has_value())
return {};
return timeline_time.value() + m_origin_time;
}
// https://www.w3.org/TR/web-animations-1/#origin-time
void DocumentTimeline::set_current_time(Optional<double> current_time)
{
// A document timeline is a type of timeline that is associated with a document and whose current time is calculated
// as a fixed offset from the now timestamp provided each time the update animations and send events procedure is
// run. This fixed offset is referred to as the document timelines origin time.
if (!current_time.has_value())
Base::set_current_time({});
else
Base::set_current_time(current_time.value() - m_origin_time);
// After a document timeline becomes active, it is monotonically increasing.
if (!is_inactive())
VERIFY(is_monotonically_increasing());
}
// https://www.w3.org/TR/web-animations-1/#document-timelines
bool DocumentTimeline::is_inactive() const
{
// A document timeline that is associated with a Document which is not an active document is also considered to be
// inactive.
return Base::is_inactive() || !associated_document()->is_active();
}
DocumentTimeline::DocumentTimeline(JS::Realm& realm, DOM::Document& document, HighResolutionTime::DOMHighResTimeStamp origin_time)
: AnimationTimeline(realm)
, m_origin_time(origin_time)
{
set_associated_document(document);
}
void DocumentTimeline::initialize(JS::Realm& realm)
{
Base::initialize(realm);
WEB_SET_PROTOTYPE_FOR_INTERFACE(DocumentTimeline);
}
}