Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / css_animation.lucb

#==============================================================================================
#
#   css_animation - CSS::CSSAnimation: an animation made from the animation-* properties
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/CSS/CSSAnimation.cpp (lines 1-153) with the inline members of
#       CSSAnimation.h, by region p4a: creating one, its composite order and animation class,
#       applying the computed animation-* properties to it and its keyframe effect (timeline,
#       duration, delay, iteration count, easing, fill mode, direction, composition, and
#       playing or pausing it), and setting its timeline from script.
#
#       Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

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

## Ported from Libraries/LibWeb/CSS/CSSAnimation.h:24 Web::CSS::CSSAnimation::animation_name.
pub func css_animation_animation_name(this: CssAnimation*) -> ak.FlyString:
    return this.m_animation_name

## Ported from Libraries/LibWeb/CSS/CSSAnimation.h:25 Web::CSS::CSSAnimation::set_animation_name.
pub func css_animation_set_animation_name(this: CssAnimation*, animation_name: ak.FlyString):
    this.m_animation_name = animation_name

## Ported from Libraries/LibWeb/CSS/CSSAnimation.h:32 Web::CSS::CSSAnimation::default_easing.
pub func css_animation_default_easing(this: CssAnimation*) -> CssEasingFunction:
    return this.m_default_easing

## Ported from Libraries/LibWeb/CSS/CSSAnimation.h:41 Web::CSS::CSSAnimation::is_css_animation (override).
func css_animation_is_css_animation_impl(base: AnimationsAnimation*) -> bool:
    return true

# mark: CSSAnimation ============================================================================

## Ported from Libraries/LibWeb/CSS/CSSAnimation.cpp:20 Web::CSS::CSSAnimation::create.
pub func css_animation_create(realm: Realm*) -> CssAnimation*:
    return realm_create_css_animation(realm, realm)

## Ported from Libraries/LibWeb/CSS/CSSAnimation.cpp:26 Web::CSS::CSSAnimation::class_specific_composite_order (override).
# https://www.w3.org/TR/css-animations-2/#animation-composite-order
func css_animation_class_specific_composite_order_impl(base: AnimationsAnimation*, other_animation: AnimationsAnimation*) -> i32:
    let other = as_css_animation(other_animation)

    # The existence of an owning element determines the animation class, so both animations should have their owning
    # element in the same state
    let owning_element = animations_animation_owning_element(base)
    let other_owning_element = animations_animation_owning_element(other.animations_animation())
    assert((owning_element != none) == (other_owning_element != none))

    # Within the set of CSS Animations with an owning element, two animations A and B are sorted in composite order
    # (first to last) as follows:
    if owning_element != none:
        # 1. If the owning element of A and B differs, sort A and B by tree order of their corresponding owning elements.
        #    With regard to pseudo-elements, the sort order is as follows:
        #    - element
        #    - ::marker
        #    - ::before
        #    - any other pseudo-elements not mentioned specifically in this list, sorted in ascending order by the Unicode
        #      codepoints that make up each selector
        #    - ::after
        #    - element children
        if owning_element != other_owning_element:
            # FIXME: Sort by tree order
            return 0

        # 2. Otherwise, sort A and B based on their position in the computed value of the animation-name property of the
        #    (common) owning element.
        # FIXME: Do this when animation-name supports multiple values
        return 0

    # The composite order of CSS Animations without an owning element is based on their position in the global animation list.
    return (i32)(animations_animation_global_animation_list_order(base) -% animations_animation_global_animation_list_order(other.animations_animation()))

## Ported from Libraries/LibWeb/CSS/CSSAnimation.cpp:61 Web::CSS::CSSAnimation::animation_class (override).
func css_animation_animation_class_impl(base: AnimationsAnimation*) -> AnimationsAnimationClass:
    if animations_animation_owning_element(base) != none:
        return AnimationsAnimationClass.css_animation_with_owning_element
    return AnimationsAnimationClass.css_animation_without_owning_element

## Ported from Libraries/LibWeb/CSS/CSSAnimation.cpp:70 Web::CSS::should_update_timeline.
# NB: Unrelated style changes shouldn't cause us to recreate anonymous timelines, to achieve this we drop updates
#     between two equivalent anonymous timelines.
func css_should_update_timeline(old_timeline: AnimationsAnimationTimeline*?, new_timeline: AnimationsAnimationTimeline*?) -> bool:
    let old_value = old_timeline else return true
    let new_value = new_timeline else return true

    let old_scroll_timeline = as_if_animations_scroll_timeline(old_value) else return true
    let new_scroll_timeline = as_if_animations_scroll_timeline(new_value) else return true

    match old_scroll_timeline.m_source:
        .anonymous_source(old_source):
            match new_scroll_timeline.m_source:
                .anonymous_source(new_source):
                    return old_source != new_source
                _:
                    return true
        _:
            return true

## Run play() under a TemporaryExecutionContext (apply_css_properties' play state change).
func css_animation_play_in_execution_context(this: CssAnimation*):
    var context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&context, bind_platform_object_realm(this.bind_platform_object()))
    defer html_temporary_execution_context_destruct(&context)
    animations_animation_play(this.animations_animation()) else trap("FIXME: should propagate errors")

## Run pause() under a TemporaryExecutionContext (apply_css_properties' play state change).
func css_animation_pause_in_execution_context(this: CssAnimation*):
    var context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&context, bind_platform_object_realm(this.bind_platform_object()))
    defer html_temporary_execution_context_destruct(&context)
    animations_animation_pause(this.animations_animation()) else trap("FIXME: should propagate errors")

## Run set_timeline() under a TemporaryExecutionContext (apply_css_properties' timeline change).
func css_animation_set_timeline_in_execution_context(this: CssAnimation*, timeline: AnimationsAnimationTimeline*?):
    var context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&context, bind_platform_object_realm(this.bind_platform_object()))
    defer html_temporary_execution_context_destruct(&context)
    animations_animation_set_timeline(this.animations_animation(), timeline)

## Ported from Libraries/LibWeb/CSS/CSSAnimation.cpp:88 Web::CSS::CSSAnimation::apply_css_properties.
pub func css_animation_apply_css_properties(this: CssAnimation*, animation_properties: const CssComputedPropertiesAnimationProperties*):
    let animation = this.animations_animation()

    # FIXME: Don't apply overridden properties as defined here: https://drafts.csswg.org/css-animations-2/#animations

    let effect_base = animations_animation_effect(animation) else trap("VERIFY: effect()")

    let effect = as_animations_keyframe_effect(effect_base)

    if not ak.hash_table_contains_t(&this.m_ignored_css_properties, css_data.PropertyId.animation_timeline) and css_should_update_timeline(animations_animation_timeline(animation), animation_properties.timeline):
        css_animation_set_timeline_in_execution_context(this, animation_properties.timeline)

    animations_animation_effect_set_specified_iteration_duration(effect_base, animation_properties.duration)
    animations_animation_effect_set_specified_start_delay(effect_base, animation_properties.delay)
    animations_animation_effect_set_iteration_count(effect_base, animation_properties.iteration_count)
    # https://drafts.csswg.org/web-animations-2/#updating-animationeffect-timing
    # Timing properties may also be updated due to a style change. Any change to a CSS animation property that affects
    # timing requires rerunning the procedure to normalize specified timing.
    animations_animation_effect_normalize_specified_timing(effect_base)
    # NB: animation-timing-function is applied per-keyframe, not as the effect-level timing function.
    #     The effect-level timing function remains linear.
    this.m_default_easing = animation_properties.timing_function
    animations_animation_effect_set_fill_mode(effect_base, animations_css_fill_mode_to_bindings_fill_mode(animation_properties.fill_mode))
    animations_animation_effect_set_playback_direction(effect_base, animations_css_animation_direction_to_bindings_playback_direction(animation_properties.direction))
    animations_keyframe_effect_set_composite(effect, animations_css_animation_composition_to_bindings_composite_operation(animation_properties.composition))

    if animation_properties.play_state != animations_animation_last_css_animation_play_state(animation):
        if animation_properties.play_state == css_data.AnimationPlayState.running and animations_animation_play_state(animation) != BindAnimationPlayState.running:
            css_animation_play_in_execution_context(this)
        elif animation_properties.play_state == css_data.AnimationPlayState.paused and animations_animation_play_state(animation) != BindAnimationPlayState.paused:
            css_animation_pause_in_execution_context(this)

        animations_animation_set_last_css_animation_play_state(animation, animation_properties.play_state)

## Ported from Libraries/LibWeb/CSS/CSSAnimation.cpp:128 Web::CSS::CSSAnimation::set_timeline_for_bindings (override).
func css_animation_set_timeline_for_bindings_impl(base: AnimationsAnimation*, timeline: AnimationsAnimationTimeline*?):
    let this = as_css_animation(base)
    # AD-HOC: When the timeline of a CSS animation is modified by the author from JS we should no longer apply changes
    #         to the `animation-timeline` property. See https://github.com/w3c/csswg-drafts/issues/13472
    _ = ak.hash_table_set(&this.m_ignored_css_properties, css_data.PropertyId.animation_timeline)
    animations_animation_set_timeline(base, timeline)

## Ported from Libraries/LibWeb/CSS/CSSAnimation.cpp:136 Web::CSS::CSSAnimation::CSSAnimation (private).
func css_animation_construct(this: CssAnimation*, realm: Realm*):
    animations_animation_construct(this.animations_animation(), realm)
    # The C++ default member initializers.
    this.m_default_easing = css_easing_function_ease()

    # FIXME:
    # CSS Animations generated using the markup defined in this specification are not added to the global animation
    # list when they are created. Instead, these animations are appended to the global animation list at the first
    # moment when they transition out of the idle play state after being disassociated from their owning element. CSS
    # Animations that have been disassociated from their owning element but are still idle do not have a defined
    # composite order.

## Ported from Libraries/LibWeb/CSS/CSSAnimation.cpp:147 Web::CSS::CSSAnimation::initialize (override).
func css_animation_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.css_animation)
    animations_animation_initialize_impl(base, realm)