Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / animations / animation_1.lucb

#==============================================================================================
#
#   animation_1 - Animations::Animation: accessors, creation, the associated effect and the
#                 timeline
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Animations/Animation.cpp (lines 1-217) with the inline members
#       of Animation.h, by region p4a: creating an animation, setting its associated effect and
#       setting its timeline. Part 2 has the times, the play state and replacement; part 3
#       cancel, finish, play, pause and reverse; part 4 the conversions, the finished state
#       and the pending tasks. construct_impl (the JS constructor) is phase 3.
#
#       Copyright (c) 2023-2024, Matthew Olsson <mattco@serenityos.org>.
#       Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Accessors (Animation.h) =================================================================

## Ported from Libraries/LibWeb/Animations/Animation.h:37 Web::Animations::Animation::id.
pub func animations_animation_id(this: AnimationsAnimation*) -> ak.FlyString:
    return this.m_id

## Ported from Libraries/LibWeb/Animations/Animation.h:38 Web::Animations::Animation::set_id.
pub func animations_animation_set_id(this: AnimationsAnimation*, value: ak.FlyString):
    this.m_id = value

## Ported from Libraries/LibWeb/Animations/Animation.h:40 Web::Animations::Animation::effect.
pub func animations_animation_effect(this: AnimationsAnimation*) -> AnimationsAnimationEffect*?:
    return this.m_effect

## Ported from Libraries/LibWeb/Animations/Animation.h:43 Web::Animations::Animation::timeline.
pub func animations_animation_timeline(this: AnimationsAnimation*) -> AnimationsAnimationTimeline*?:
    return this.m_timeline

## Ported from Libraries/LibWeb/Animations/Animation.h:46 Web::Animations::Animation::timeline_for_bindings (virtual).
pub func animations_animation_timeline_for_bindings_impl(this: AnimationsAnimation*) -> AnimationsAnimationTimeline*?:
    return this.m_timeline

## Ported from Libraries/LibWeb/Animations/Animation.h:47 Web::Animations::Animation::set_timeline_for_bindings (virtual).
pub func animations_animation_set_timeline_for_bindings_impl(this: AnimationsAnimation*, timeline: AnimationsAnimationTimeline*?):
    animations_animation_set_timeline(this, timeline)

## Ported from Libraries/LibWeb/Animations/Animation.h:50 Web::Animations::Animation::start_time_for_bindings.
# https://drafts.csswg.org/web-animations-2/#dom-animation-starttime
pub func animations_animation_start_time_for_bindings(this: AnimationsAnimation*) -> AnimationsNullableCssNumberish:
    return animations_nullable_css_numberish_from_optional_css_numberish_time(bind_platform_object_realm(this.bind_platform_object()), animations_animation_start_time(this))

## Ported from Libraries/LibWeb/Animations/Animation.h:54 Web::Animations::Animation::start_time.
pub func animations_animation_start_time(this: AnimationsAnimation*) -> AnimationsTimeValue?:
    return this.m_start_time

## Ported from Libraries/LibWeb/Animations/Animation.h:60 Web::Animations::Animation::current_time_for_bindings.
# https://drafts.csswg.org/web-animations-2/#dom-animation-currenttime
pub func animations_animation_current_time_for_bindings(this: AnimationsAnimation*) -> AnimationsNullableCssNumberish:
    return animations_nullable_css_numberish_from_optional_css_numberish_time(bind_platform_object_realm(this.bind_platform_object()), animations_animation_current_time(this))

## Ported from Libraries/LibWeb/Animations/Animation.h:67 Web::Animations::Animation::playback_rate.
pub func animations_animation_playback_rate(this: AnimationsAnimation*) -> f64:
    return this.m_playback_rate

## Ported from Libraries/LibWeb/Animations/Animation.h:76 Web::Animations::Animation::replace_state.
pub func animations_animation_replace_state(this: AnimationsAnimation*) -> BindAnimationReplaceState:
    return this.m_replace_state

## Ported from Libraries/LibWeb/Animations/Animation.h:80 Web::Animations::Animation::pending.
# https://www.w3.org/TR/web-animations-1/#dom-animation-pending
pub func animations_animation_pending(this: AnimationsAnimation*) -> bool:
    return this.m_pending_play_task == AnimationsAnimationTaskState.scheduled or this.m_pending_pause_task == AnimationsAnimationTaskState.scheduled

## Ported from Libraries/LibWeb/Animations/Animation.h:83 Web::Animations::Animation::ready.
# https://www.w3.org/TR/web-animations-1/#dom-animation-ready
pub func animations_animation_ready(this: AnimationsAnimation*) -> JsPromiseCapability*:
    return animations_animation_current_ready_promise(this)

## Ported from Libraries/LibWeb/Animations/Animation.h:86 Web::Animations::Animation::finished.
# https://www.w3.org/TR/web-animations-1/#dom-animation-finished
pub func animations_animation_finished(this: AnimationsAnimation*) -> JsPromiseCapability*:
    return animations_animation_current_finished_promise(this)

## Ported from Libraries/LibWeb/Animations/Animation.h:87 Web::Animations::Animation::is_finished.
pub func animations_animation_is_finished(this: AnimationsAnimation*) -> bool:
    return this.m_is_finished

## Ported from Libraries/LibWeb/Animations/Animation.h:89 Web::Animations::Animation::is_idle.
pub func animations_animation_is_idle(this: AnimationsAnimation*) -> bool:
    return animations_animation_play_state(this) == BindAnimationPlayState.idle

## Ported from Libraries/LibWeb/Animations/Animation.h:123 Web::Animations::Animation::is_css_animation (virtual).
pub func animations_animation_is_css_animation_impl(this: AnimationsAnimation*) -> bool:
    return false

## Ported from Libraries/LibWeb/Animations/Animation.h:124 Web::Animations::Animation::is_css_transition (virtual).
pub func animations_animation_is_css_transition_impl(this: AnimationsAnimation*) -> bool:
    return false

## Ported from Libraries/LibWeb/Animations/Animation.h:126 Web::Animations::Animation::owning_element.
pub func animations_animation_owning_element(this: AnimationsAnimation*) -> DomAbstractElement?:
    return this.m_owning_element

## Ported from Libraries/LibWeb/Animations/Animation.h:127 Web::Animations::Animation::set_owning_element.
pub func animations_animation_set_owning_element(this: AnimationsAnimation*, value: DomAbstractElement?):
    this.m_owning_element = value

## Ported from Libraries/LibWeb/Animations/Animation.h:129 Web::Animations::Animation::animation_class (virtual).
pub func animations_animation_animation_class_impl(this: AnimationsAnimation*) -> AnimationsAnimationClass:
    return AnimationsAnimationClass.none_

## Ported from Libraries/LibWeb/Animations/Animation.h:130 Web::Animations::Animation::class_specific_composite_order (virtual).
pub func animations_animation_class_specific_composite_order_impl(this: AnimationsAnimation*, other: AnimationsAnimation*) -> i32:
    return 0

## Ported from Libraries/LibWeb/Animations/Animation.h:132 Web::Animations::Animation::global_animation_list_order.
pub func animations_animation_global_animation_list_order(this: AnimationsAnimation*) -> u32:
    return this.m_global_animation_list_order

## Ported from Libraries/LibWeb/Animations/Animation.h:134 Web::Animations::Animation::release_saved_cancel_time: the
## saved cancel time, moved out (the moved-from Optional is empty).
pub func animations_animation_release_saved_cancel_time(this: AnimationsAnimation*) -> AnimationsTimeValue?:
    let saved_cancel_time = this.m_saved_cancel_time
    this.m_saved_cancel_time = none
    return saved_cancel_time

## Ported from Libraries/LibWeb/Animations/Animation.h:138 Web::Animations::Animation::last_css_animation_play_state.
pub func animations_animation_last_css_animation_play_state(this: AnimationsAnimation*) -> css_data.AnimationPlayState?:
    return this.m_last_css_animation_play_state

## Ported from Libraries/LibWeb/Animations/Animation.h:139 Web::Animations::Animation::set_last_css_animation_play_state.
pub func animations_animation_set_last_css_animation_play_state(this: AnimationsAnimation*, state: css_data.AnimationPlayState):
    this.m_last_css_animation_play_state = state

# mark: Creation, effect and timeline ===========================================================

## Ported from Libraries/LibWeb/Animations/Animation.cpp:29 Web::Animations::Animation::create.
# https://www.w3.org/TR/web-animations-1/#dom-animation-animation
pub func animations_animation_create(realm: Realm*, effect: AnimationsAnimationEffect*?, timeline: (AnimationsAnimationTimeline*?)?) -> AnimationsAnimation*:
    # 1. Let animation be a new Animation object.
    let animation = realm_create_animations_animation(realm, realm)

    # 2. Run the procedure to set the timeline of an animation on animation passing timeline as the new timeline or, if
    #    a timeline argument is missing, passing the default document timeline of the Document associated with the
    #    Window that is the current global object.
    var new_timeline: AnimationsAnimationTimeline*? = none
    if let given = timeline:
        new_timeline = given
    else:
        let window = as_html_window((gc.Cell*)html_current_global_object())
        new_timeline = dom_document_timeline(html_window_associated_document(window)).animations_animation_timeline()
    animations_animation_set_timeline(animation, new_timeline)

    # 3. Run the procedure to set the associated effect of an animation on animation passing source as the new effect.
    animations_animation_set_effect(animation, effect)

    return animation

## Ported from Libraries/LibWeb/Animations/Animation.cpp:49 Web::Animations::Animation::construct_impl.
pub func animations_animation_construct_impl(realm: Realm*, effect: AnimationsAnimationEffect*?, timeline: (AnimationsAnimationTimeline*?)?) -> AnimationsAnimation*:
    # NB: If the timeline argument was not present, pass an OptionalNone so 'create' treats it as missing.
    # (realm.vm().argument_count() < 2: the JS constructor's arguments are phase 3.)
    _ = realm
    _ = effect
    _ = timeline
    trap("unported (P3): Web::Animations::Animation::construct_impl (the arguments of a JS call)")

## Ported from Libraries/LibWeb/Animations/Animation.cpp:59 Web::Animations::Animation::set_effect.
# https://www.w3.org/TR/web-animations-1/#animation-set-the-associated-effect-of-an-animation
pub func animations_animation_set_effect(this: AnimationsAnimation*, new_effect: AnimationsAnimationEffect*?):
    # Setting this attribute updates the object’s associated effect using the procedure to set the associated effect of
    # an animation.

    # 1. Let old effect be the current associated effect of animation, if any.
    let old_effect = this.m_effect

    # 2. If new effect is the same object as old effect, abort this procedure.
    if new_effect == old_effect:
        return

    # 3. If animation has a pending pause task, reschedule that task to run as soon as animation is ready.
    # 4. If animation has a pending play task, reschedule that task to run as soon as animation is ready to play ne
    #    effect.
    # Note: There is no real difference between "pending" and "as soon as possible", so this step is a no-op.

    # 5. If new effect is not null and if new effect is the associated effect of another animation, previous animation,
    #    run the procedure to set the associated effect of an animation (this procedure) on previous animation passing
    #    null as new effect.
    if let effect = new_effect:
        if animations_animation_effect_associated_animation(effect) != this:
            if let animation = animations_animation_effect_associated_animation(effect):
                animations_animation_set_effect(animation, none)

    # 6. Let the associated effect of animation be new effect.
    var old_target: DomElement*? = none
    if let effect = this.m_effect:
        old_target = animations_animation_effect_target(effect)
    var new_target: DomElement*? = none
    if let effect = new_effect:
        new_target = animations_animation_effect_target(effect)
    if old_target != new_target:
        if let target = old_target:
            animations_animatable_disassociate_with_animation(&target.animatable, this)
        if let target = new_target:
            animations_animatable_associate_with_animation(&target.animatable, this)
    if let effect = new_effect:
        animations_animation_effect_set_associated_animation(effect, this)
    if let effect = this.m_effect:
        animations_animation_effect_set_associated_animation(effect, none)
    this.m_effect = new_effect

    # Once animated properties of the old effect no longer apply, we need to ensure appropriate invalidations are scheduled
    if let effect = old_effect:
        var context = AnimationsAnimationUpdateContext()
        animations_animation_effect_update_computed_properties(effect, &context)
        animations_animation_update_context_destruct(&context)

    # 7. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
    #    and the synchronously notify flag set to false.
    animations_animation_update_finished_state(this, AnimationsAnimationDidSeek.no, AnimationsAnimationSynchronouslyNotify.no)

## Ported from Libraries/LibWeb/Animations/Animation.cpp:112 Web::Animations::Animation::set_timeline.
# https://www.w3.org/TR/web-animations-1/#animation-set-the-timeline-of-an-animation
# https://drafts.csswg.org/web-animations-2/#setting-the-timeline
pub func animations_animation_set_timeline(this: AnimationsAnimation*, new_timeline: AnimationsAnimationTimeline*?):
    # 1. Let old timeline be the current timeline of animation, if any.
    let old_timeline = this.m_timeline

    # 2. If new timeline is the same object as old timeline, abort this procedure.
    if new_timeline == old_timeline:
        return

    # 3. Let previous play state be animation’s play state.
    let previous_play_state = animations_animation_play_state(this)

    # 4. Let previous current time be the animation’s current time.
    let previous_current_time = animations_animation_current_time(this)

    # 5. Set previous progress based in the first condition that applies:
    var previous_progress: f64? = none
    if let current_time = previous_current_time:
        # If end time is zero:
        # Set previous progress to zero.
        var end_time_is_zero = false
        if let effect = this.m_effect:
            end_time_is_zero = animations_animation_effect_end_time(effect).value == 0.0
        if end_time_is_zero:
            previous_progress = 0.0
        else:
            # Otherwise
            # Set previous progress = previous current time / end time
            let effect = this.m_effect else trap("a null effect: m_effect->end_time()")
            previous_progress = animations_time_value_div_time_value(current_time, animations_animation_effect_end_time(effect))
    # (If previous current time is unresolved: set previous progress to unresolved.)

    # 6. Let from finite timeline be true if old timeline is not null and not monotonically increasing.
    var from_finite_timeline = false
    if let timeline = old_timeline:
        from_finite_timeline = not animations_animation_timeline_is_monotonically_increasing(timeline)

    # 7. Let to finite timeline be true if timeline is not null and not monotonically increasing.
    var to_finite_timeline = false
    if let timeline = new_timeline:
        to_finite_timeline = not animations_animation_timeline_is_monotonically_increasing(timeline)

    # 8. Let the timeline of animation be new timeline.
    if let timeline = this.m_timeline:
        animations_animation_timeline_disassociate_with_animation(timeline, this)
    this.m_timeline = new_timeline
    if let timeline = this.m_timeline:
        animations_animation_timeline_associate_with_animation(timeline, this)

    # AD-HOC: The normalization of the specified timing of the associated effect depends on the associated timeline.
    #         This must be done before calling set_current_time_for_bindings() to ensure consistent units
    if let effect = this.m_effect:
        animations_animation_effect_normalize_specified_timing(effect)

    # 9. Perform the steps corresponding to the first matching condition from the following, if any:
    # If to finite timeline,
    if to_finite_timeline:
        # 1. Apply any pending playback rate on animation
        animations_animation_apply_any_pending_playback_rate(this)

        # 2. set auto align start time to true.
        this.m_auto_align_start_time = true

        # 3. Set start time to unresolved.
        this.m_start_time = none

        # 4. Set hold time to unresolved.
        this.m_hold_time = none

        # 5. If previous play state is "finished" or "running"
        if previous_play_state == BindAnimationPlayState.finished or previous_play_state == BindAnimationPlayState.running:
            # 1. Schedule a pending play task
            this.m_pending_play_task = AnimationsAnimationTaskState.scheduled

        # 6. If previous play state is "paused" and previous progress is resolved:
        if previous_play_state == BindAnimationPlayState.paused and previous_progress != none:
            # 1. Set hold time to previous progress * end time.
            this.m_hold_time = animations_animation_previous_progress_multiplied_by_end_time(this, previous_progress)
        # NOTE: This step ensures that previous progress is preserved even in the case of a pause-pending animation with a resolved start time.
    # If from finite timeline and previous progress is resolved,
    elif from_finite_timeline and previous_progress != none:
        # Run the procedure to set the current time to previous progress * end time.
        # NB: We know that the time we are passing is valid so we can wrap this in a MUST() to avoid error handling
        let realm = bind_platform_object_realm(this.bind_platform_object())
        let time = animations_time_value_as_css_numberish(animations_animation_previous_progress_multiplied_by_end_time(this, previous_progress), realm)
        animations_animation_set_current_time_for_bindings(this, animations_nullable_css_numberish_from_css_numberish(time)) else trap("MUST: set_current_time_for_bindings")

    # 10. If the start time of animation is resolved, make animation’s hold time unresolved.
    if this.m_start_time != none:
        this.m_hold_time = none

    # 11. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false,
    #    and the synchronously notify flag set to false.
    animations_animation_update_finished_state(this, AnimationsAnimationDidSeek.no, AnimationsAnimationSynchronouslyNotify.no)

## set_timeline's `previous_progress_multiplied_by_end_time` lambda.
func animations_animation_previous_progress_multiplied_by_end_time(this: AnimationsAnimation*, previous_progress: f64?) -> AnimationsTimeValue:
    # AD-HOC: The spec doesn't say what to do if we have no effect so we just assume an end time of 0
    let effect = this.m_effect else return animations_time_value_create_zero(this.m_timeline)

    var type_ = AnimationsTimeValueType.milliseconds
    if let timeline = this.m_timeline:
        if animations_animation_timeline_is_progress_based(timeline):
            type_ = AnimationsTimeValueType.percentage
    let progress = previous_progress else trap("VERIFY: Optional::value()")
    return AnimationsTimeValue(type_ = type_, value = animations_animation_effect_end_time(effect).value * progress)