Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / css_transition.lucb

#==============================================================================================
#
#   css_transition - CSS::CSSTransition: an animation started by a change of a transitioned
#                    property
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/CSS/CSSTransition.cpp (lines 1-177) with the inline members of
#       CSSTransition.h, by region p4a: starting a transition (a CSSTransition with a two-
#       keyframe KeyframeEffect on the element, registered with it and played), its property,
#       animation class and composite order, its start and end times and values, and the
#       output of its timing function at a time. CSSTransition's private constructor, which
#       does the work, is css_transition_construct.
#
#       Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
#       Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

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

## Ported from Libraries/LibWeb/CSS/CSSTransition.h:38 Web::CSS::CSSTransition::transition_start_time.
pub func css_transition_transition_start_time(this: CssTransition*) -> f64:
    return this.m_start_time

## Ported from Libraries/LibWeb/CSS/CSSTransition.h:39 Web::CSS::CSSTransition::transition_end_time.
pub func css_transition_transition_end_time(this: CssTransition*) -> f64:
    return this.m_end_time

## Ported from Libraries/LibWeb/CSS/CSSTransition.h:40 Web::CSS::CSSTransition::transition_start_value.
pub func css_transition_transition_start_value(this: CssTransition*) -> CssStyleValue*:
    return this.m_start_value

## Ported from Libraries/LibWeb/CSS/CSSTransition.h:41 Web::CSS::CSSTransition::transition_end_value.
pub func css_transition_transition_end_value(this: CssTransition*) -> CssStyleValue*:
    return this.m_end_value

## Ported from Libraries/LibWeb/CSS/CSSTransition.h:42 Web::CSS::CSSTransition::reversing_adjusted_start_value.
pub func css_transition_reversing_adjusted_start_value(this: CssTransition*) -> CssStyleValue*:
    return this.m_reversing_adjusted_start_value

## Ported from Libraries/LibWeb/CSS/CSSTransition.h:43 Web::CSS::CSSTransition::reversing_shortening_factor.
pub func css_transition_reversing_shortening_factor(this: CssTransition*) -> f64:
    return this.m_reversing_shortening_factor

## Ported from Libraries/LibWeb/CSS/CSSTransition.h:55 Web::CSS::CSSTransition::previous_phase.
pub func css_transition_previous_phase(this: CssTransition*) -> CssTransitionPhase:
    return this.m_previous_phase

## Ported from Libraries/LibWeb/CSS/CSSTransition.h:56 Web::CSS::CSSTransition::set_previous_phase.
pub func css_transition_set_previous_phase(this: CssTransition*, phase: CssTransitionPhase):
    this.m_previous_phase = phase

## Ported from Libraries/LibWeb/CSS/CSSTransition.h:76 Web::CSS::CSSTransition::is_css_transition (override).
func css_transition_is_css_transition_impl(base: AnimationsAnimation*) -> bool:
    return true

# mark: CSSTransition ===========================================================================

## Ported from Libraries/LibWeb/CSS/CSSTransition.cpp:23 Web::CSS::CSSTransition::start_a_transition.
pub func css_transition_start_a_transition(abstract_element: DomAbstractElement, property_id: css_data.PropertyId, transition_generation: usize, delay: f64, start_time: f64, end_time: f64, start_value: CssStyleValue*, end_value: CssStyleValue*, reversing_adjusted_start_value: CssStyleValue*, reversing_shortening_factor: f64) -> CssTransition*:
    var element_reference = abstract_element
    let realm = dom_element_realm(dom_abstract_element_element(&element_reference))
    return realm_create_css_transition(realm, realm, abstract_element, property_id, transition_generation, delay, start_time, end_time, start_value, end_value, reversing_adjusted_start_value, reversing_shortening_factor)

## Ported from Libraries/LibWeb/CSS/CSSTransition.cpp:39 Web::CSS::CSSTransition::transition_property. (C++ answers the
## StringView of the property's name; the name is a FlyString here.)
pub func css_transition_transition_property(this: CssTransition*) -> ak.FlyString:
    return css_data.string_from_property_id(this.m_transition_property)

## Ported from Libraries/LibWeb/CSS/CSSTransition.cpp:44 Web::CSS::CSSTransition::animation_class (override).
func css_transition_animation_class_impl(base: AnimationsAnimation*) -> AnimationsAnimationClass:
    return AnimationsAnimationClass.css_transition

## Ported from Libraries/LibWeb/CSS/CSSTransition.cpp:49 Web::CSS::CSSTransition::class_specific_composite_order (override).
func css_transition_class_specific_composite_order_impl(base: AnimationsAnimation*, other_animation: AnimationsAnimation*) -> i32:
    let this = as_css_transition(base)
    let other = as_css_transition(other_animation)

    let owning_element = animations_animation_owning_element(base)
    let other_owning_element = animations_animation_owning_element(other_animation)

    # Within the set of CSS Transitions, two animations A and B are sorted in composite order (first to last) as
    # follows:

    # 1. If neither A nor B has an owning element, sort based on their relative position in the global animation list.
    if owning_element == none and other_owning_element == none:
        return (i32)(animations_animation_global_animation_list_order(base) -% animations_animation_global_animation_list_order(other_animation))

    # 2. Otherwise, if only one of A or B has an owning element, let the animation with an owning element sort first.
    if owning_element != none and other_owning_element == none:
        return -1
    if owning_element == none and other_owning_element != none:
        return 1

    # 3. Otherwise, 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: Actually sort by tree order
        return 0

    # 4. Otherwise, if A and B have different transition generation values, sort by their corresponding transition
    #    generation in ascending order.
    if this.m_transition_generation != other.m_transition_generation:
        # (size_t subtraction converted to int, as the C++ return does)
        return (i32)(this.m_transition_generation -% other.m_transition_generation)

    # 5. Otherwise, sort A and B in ascending order by the Unicode codepoints that make up the expanded transition
    #    property name of each transition (i.e. without attempting case conversion and such that ‘-moz-column-width’
    #    sorts before ‘column-width’).
    # FIXME: This should operate on Unicode strings, not StringViews.
    let property = css_transition_transition_property(this)
    let other_property = css_transition_transition_property(other)
    let view = ak.fly_string_bytes_as_string_view(&property)
    return ak.string_view_compare(&view, ak.fly_string_bytes_as_string_view(&other_property))

## Ported from Libraries/LibWeb/CSS/CSSTransition.cpp:91 Web::CSS::CSSTransition::CSSTransition (private).
func css_transition_construct(this: CssTransition*, realm: Realm*, abstract_element: DomAbstractElement, property_id: css_data.PropertyId, transition_generation: usize, delay: f64, start_time: f64, end_time: f64, start_value: CssStyleValue*, end_value: CssStyleValue*, reversing_adjusted_start_value: CssStyleValue*, reversing_shortening_factor: f64):
    animations_animation_construct(this.animations_animation(), realm)
    this.m_transition_property = property_id
    this.m_transition_generation = transition_generation
    this.m_start_time = start_time + delay
    this.m_end_time = end_time + delay
    this.m_start_value = start_value
    this.m_end_value = end_value
    this.m_reversing_adjusted_start_value = reversing_adjusted_start_value
    this.m_reversing_shortening_factor = reversing_shortening_factor
    this.m_keyframe_effect = animations_keyframe_effect_create(realm)
    this.m_previous_phase = CssTransitionPhase.idle

    # FIXME:
    # Transitions 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. Transitions
    # that have been disassociated from their owning element but are still idle do not have a defined composite order.

    # Construct a KeyframesEffect for our animation
    var element_reference = abstract_element
    let element = dom_abstract_element_element(&element_reference)
    let pseudo_element = dom_abstract_element_pseudo_element(&element_reference)
    let effect = this.m_keyframe_effect.animations_animation_effect()
    animations_keyframe_effect_set_target_dom_abstract_element(this.m_keyframe_effect, abstract_element)
    animations_animation_effect_set_specified_start_delay(effect, delay)
    animations_animation_effect_set_specified_iteration_duration(effect, F64OrString.f64_(end_time - start_time))
    # 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)
    let attributes = animations_animatable_property_transition_attributes(&element.animatable, pseudo_element, property_id) else trap("VERIFY: Optional::value()")
    animations_animation_effect_set_timing_function(effect, attributes.timing_function)

    let key_frame_set = (new AnimationsKeyframeEffectKeyFrameSet) else trap("out of memory")
    var initial_keyframe = animations_keyframe_effect_key_frame_set_resolved_key_frame_new()
    _ = ak.hash_map_set(&initial_keyframe.properties, property_id, AnimationsKeyframeEffectKeyFrameSetUseInitialOrCssStyleValue.css_style_value(this.m_start_value))

    var final_keyframe = animations_keyframe_effect_key_frame_set_resolved_key_frame_new()
    _ = ak.hash_map_set(&final_keyframe.properties, property_id, AnimationsKeyframeEffectKeyFrameSetUseInitialOrCssStyleValue.css_style_value(this.m_end_value))

    ak.red_black_tree_insert(&key_frame_set.keyframes_by_key, 0, initial_keyframe)
    ak.red_black_tree_insert(&key_frame_set.keyframes_by_key, (u64)(100.0 * animations_keyframe_effect_animation_key_frame_key_scale_factor), final_keyframe)

    animations_keyframe_effect_set_key_frame_set(this.m_keyframe_effect, key_frame_set)
    animations_animation_set_timeline(this.animations_animation(), dom_document_timeline(dom_abstract_element_document(&element_reference)).animations_animation_timeline())
    animations_animation_set_owning_element(this.animations_animation(), abstract_element)
    animations_animation_set_effect(this.animations_animation(), effect)
    animations_animatable_set_transition(&element.animatable, pseudo_element, this.m_transition_property, this)

    var context: HtmlTemporaryExecutionContext = ---
    html_temporary_execution_context_construct_realm(&context, realm)
    defer html_temporary_execution_context_destruct(&context)
    animations_animation_play(this.animations_animation()) else trap("FIXME: should propagate errors")

## Ported from Libraries/LibWeb/CSS/CSSTransition.cpp:151 Web::CSS::CSSTransition::initialize (override).
func css_transition_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.css_transition)
    animations_animation_initialize_impl(base, realm)

## Ported from Libraries/LibWeb/CSS/CSSTransition.cpp:157 Web::CSS::CSSTransition::visit_edges (override).
func css_transition_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (CssTransition*)base
    animations_animation_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_cached_declaration)
    gc.cell_visitor_visit_t_2(visitor, this.m_keyframe_effect)

## Ported from Libraries/LibWeb/CSS/CSSTransition.cpp:164 Web::CSS::CSSTransition::timing_function_output_at_time.
pub func css_transition_timing_function_output_at_time(this: CssTransition*, t: f64) -> f64:
    var progress = (t - css_transition_transition_start_time(this)) / (css_transition_transition_end_time(this) - css_transition_transition_start_time(this))
    # AD-HOC: If the transition has an empty duration then we get NaN here,
    # setting progress to 1 because an instant transition may be considered "finished".
    # donor bug: the condition is inverted (an empty duration has start == end); kept.
    if css_transition_transition_start_time(this) < css_transition_transition_end_time(this):
        progress = 1.0

    # FIXME: Is this before_flag value correct?
    let before_flag = t < css_transition_transition_start_time(this)
    return css_easing_function_evaluate_at(animations_animation_effect_timing_function(this.m_keyframe_effect.animations_animation_effect()), progress, before_flag)