Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / animations / document_timeline.lucb

#==============================================================================================
#
#   document_timeline - Animations::DocumentTimeline: the default document timeline
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Animations/DocumentTimeline.cpp (lines 1-94) with the inline
#       members of DocumentTimeline.h, by region p2c (see animation_timeline). The JS
#       constructor (construct_impl) is phase 3.
#
#       Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Animations/DocumentTimeline.cpp:21 Web::Animations::DocumentTimeline::create.
pub func animations_document_timeline_create(realm: Realm*, document: DomDocument*, origin_time: f64) -> AnimationsDocumentTimeline*:
    let timeline = realm_create_animations_document_timeline(realm, realm, document, origin_time)
    var current_time = dom_document_last_animation_frame_timestamp(document)
    if current_time == none:
        # The document hasn't processed an animation frame yet, we use the navigation start time, which is either the time
        # that the previous document started to be unloaded or the creation time of the current document.
        current_time = high_resolution_time_relative_high_resolution_time(dom_document_load_timing_info(document).navigation_start_time, realm_global_object(realm))
    animations_animation_timeline_update_current_time(timeline.animations_animation_timeline(), current_time else trap("unreachable: set above"))
    return timeline

## Ported from Libraries/LibWeb/Animations/DocumentTimeline.cpp:44 Web::Animations::DocumentTimeline::convert_a_timeline_time_to_an_origin_relative_time (override).
# https://www.w3.org/TR/web-animations-1/#ref-for-timeline-time-to-origin-relative-time
pub func animations_document_timeline_convert_a_timeline_time_to_an_origin_relative_time_impl(base: AnimationsAnimationTimeline*, timeline_time: AnimationsTimeValue?) -> f64?:
    let this = (AnimationsDocumentTimeline*)base
    # 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 timeline’s origin time. If timeline is inactive, return an unresolved time
    # value.
    if animations_animation_timeline_is_inactive(this.animations_animation_timeline()):
        return none
    let time = timeline_time else return none

    # NB: We know the timeline time of a DocumentTimeline is always in milliseconds because it's only ever set from
    #     update_current_time()
    assert(time.type_ == AnimationsTimeValueType.milliseconds)
    return time.value + this.m_origin_time

## Ported from Libraries/LibWeb/Animations/DocumentTimeline.h:35 Web::Animations::DocumentTimeline::can_convert_a_timeline_time_to_an_origin_relative_time (override).
pub func animations_document_timeline_can_convert_a_timeline_time_to_an_origin_relative_time_impl(base: AnimationsAnimationTimeline*) -> bool:
    return true

## Ported from Libraries/LibWeb/Animations/DocumentTimeline.cpp:58 Web::Animations::DocumentTimeline::update_current_time (override).
# https://drafts.csswg.org/web-animations-1/#document-timeline
pub func animations_document_timeline_update_current_time_impl(base: AnimationsAnimationTimeline*, timestamp: f64):
    let this = (AnimationsDocumentTimeline*)base
    # 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 equal to the current time of the default document timeline when this timeline’s current
    # time was zero, and is thus referred to as the document timeline’s origin time.
    animations_animation_timeline_set_current_time(base, AnimationsTimeValue(type_ = AnimationsTimeValueType.milliseconds, value = timestamp - this.m_origin_time))

    # https://drafts.csswg.org/web-animations-1/#ref-for-active-timeline
    # After a document timeline becomes active, it is monotonically increasing.
    if not base.m_is_monotonically_increasing and not animations_animation_timeline_is_inactive(base):
        base.m_is_monotonically_increasing = true

## Ported from Libraries/LibWeb/Animations/DocumentTimeline.h:30 Web::Animations::DocumentTimeline::duration (override).
pub func animations_document_timeline_duration_impl(base: AnimationsAnimationTimeline*) -> AnimationsTimeValue?:
    return none

## Ported from Libraries/LibWeb/Animations/DocumentTimeline.cpp:73 Web::Animations::DocumentTimeline::is_inactive (override).
# https://www.w3.org/TR/web-animations-1/#document-timelines
pub func animations_document_timeline_is_inactive_impl(base: AnimationsAnimationTimeline*) -> bool:
    # A document timeline that is associated with a Document which is not an active document is also considered to be
    # inactive.
    let document = base.m_associated_document else trap("VERIFY: the document timeline's document")
    return animations_animation_timeline_is_inactive_impl(base) or not dom_document_is_active(document)

## Ported from Libraries/LibWeb/Animations/DocumentTimeline.cpp:80 Web::Animations::DocumentTimeline::DocumentTimeline (private).
func animations_document_timeline_construct(this: AnimationsDocumentTimeline*, realm: Realm*, document: DomDocument*, origin_time: f64):
    animations_animation_timeline_construct(this.animations_animation_timeline(), realm)
    this.m_origin_time = origin_time
    animations_animation_timeline_set_associated_document(this.animations_animation_timeline(), document)

## Ported from Libraries/LibWeb/Animations/DocumentTimeline.cpp:87 Web::Animations::DocumentTimeline::initialize (override).
func animations_document_timeline_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.document_timeline)
    animations_animation_timeline_initialize_impl(base, realm)