Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / animations / keyframe_effect_key_frame_set.lucb

#==============================================================================================
#
#   keyframe_effect_key_frame_set - Animations::KeyframeEffect::generate_initial_and_final_frames
#
#   DESCRIPTION:
#       Port of LibWeb/Animations/KeyframeEffect.cpp (lines 575-626 and 976-987), ahead of the animations
#       (phase 4): StyleScope::make_rule_cache_for_cascade_origin builds a KeyFrameSet for
#       every @keyframes rule and completes it with its initial and final keyframes, and the
#       user-agent style sheet has three such rules, so every rule cache of phase 1 runs it;
#       a keyframe's animation-composition is mapped with
#       css_animation_composition_to_bindings_composite_operation_or_auto. Ported by region
#       r39; region p4a ported the rest of KeyframeEffect.cpp (keyframe_effect).
#
#       Copyright (c) 2023-2024, Matthew Olsson <mattco@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## generate_initial_and_final_frames' `expanded_properties` lambda: the longhands a keyframe's properties set.
func animations_keyframe_effect_expanded_properties(properties: const ak.HashMap[css_data.PropertyId, AnimationsKeyframeEffectKeyFrameSetUseInitialOrCssStyleValue, css_data.PropertyIdTraits]*) -> ak.HashTable[css_data.PropertyId, css_data.PropertyIdTraits]:
    var result: ak.HashTable[css_data.PropertyId, css_data.PropertyIdTraits]

    for property in ak.hash_map_begin_const(properties):
        if css_data.property_is_shorthand(property.key):
            let longhands = css_data.expanded_longhands_for_shorthand(property.key)
            for longhand in ak.vector_span_const(&longhands):
                _ = ak.hash_table_set(&result, longhand)
        else:
            _ = ak.hash_table_set(&result, property.key)

    return result

## The keyframe at `key` of `keyframe_set`, inserted with C++'s defaults if there is none.
func animations_keyframe_effect_find_or_create_keyframe(keyframe_set: AnimationsKeyframeEffectKeyFrameSet*, key: u64) -> AnimationsKeyframeEffectKeyFrameSetResolvedKeyFrame*:
    if let existing_keyframe = ak.red_black_tree_find(&keyframe_set.keyframes_by_key, key):
        return existing_keyframe
    ak.red_black_tree_insert(&keyframe_set.keyframes_by_key, key, animations_keyframe_effect_key_frame_set_resolved_key_frame_new())
    return ak.red_black_tree_find(&keyframe_set.keyframes_by_key, key) else trap("VERIFY: the keyframe was inserted")

## Ported from Libraries/LibWeb/Animations/KeyframeEffect.cpp:575 Web::Animations::KeyframeEffect::generate_initial_and_final_frames.
pub func animations_keyframe_effect_generate_initial_and_final_frames(keyframe_set: AnimationsKeyframeEffectKeyFrameSet*, animated_properties: const ak.HashTable[css_data.PropertyId, css_data.PropertyIdTraits]*):
    # 1. Find or create the initial keyframe, a keyframe with a keyframe offset of 0%, default timing function
    #    as its keyframe timing function, and default composite as its keyframe composite.
    let initial_keyframe = animations_keyframe_effect_find_or_create_keyframe(keyframe_set, 0)

    # 2. For any property in animated properties that is not otherwise present in a keyframe with an offset of
    #    0% or one that would be positioned earlier in the used keyframe order, add the computed value of that
    #    property on element to initial keyframe’s keyframe values.
    for property in ak.hash_table_begin_const(animated_properties):
        let expanded = animations_keyframe_effect_expanded_properties(&initial_keyframe.properties)
        if not ak.hash_table_contains_t(&expanded, *property):
            _ = ak.hash_map_set(&initial_keyframe.properties, *property, AnimationsKeyframeEffectKeyFrameSetUseInitialOrCssStyleValue.use_initial(AnimationsKeyframeEffectKeyFrameSetUseInitial(empty_ = 0)))

    # 3. If initial keyframe’s keyframe values is not empty, prepend initial keyframe to keyframes.

    # 4. Repeat for final keyframe, using an offset of 100%, considering keyframes positioned later in the used
    #    keyframe order, and appending to keyframes.
    let final_key = (u64)(100.0 * animations_keyframe_effect_animation_key_frame_key_scale_factor)
    let final_keyframe = animations_keyframe_effect_find_or_create_keyframe(keyframe_set, final_key)

    for property in ak.hash_table_begin_const(animated_properties):
        let expanded = animations_keyframe_effect_expanded_properties(&final_keyframe.properties)
        if not ak.hash_table_contains_t(&expanded, *property):
            _ = ak.hash_map_set(&final_keyframe.properties, *property, AnimationsKeyframeEffectKeyFrameSetUseInitialOrCssStyleValue.use_initial(AnimationsKeyframeEffectKeyFrameSetUseInitial(empty_ = 0)))

## Ported from Libraries/LibWeb/Animations/KeyframeEffect.cpp:976 Web::Animations::css_animation_composition_to_bindings_composite_operation_or_auto.
pub func animations_css_animation_composition_to_bindings_composite_operation_or_auto(composition: css_data.AnimationComposition) -> BindCompositeOperationOrAuto:
    match composition:
        .accumulate:
            return BindCompositeOperationOrAuto.accumulate
        .add:
            return BindCompositeOperationOrAuto.add
        .replace:
            return BindCompositeOperationOrAuto.replace
        _:
            trap("VERIFY_NOT_REACHED")