dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / animations / animation_effect_2.lucb
#==============================================================================================
#
# animation_effect_2 - Animations::AnimationEffect: phases, progress and the update context
#
# DESCRIPTION:
# Port of Libraries/LibWeb/Animations/AnimationEffect.cpp (lines 461-865), by region p4a:
# whether an effect is in play, current or in effect; the boundary times and the before,
# active, after and idle phases; the overall, directed, simple iteration and transformed
# progress, the current direction and iteration; parsing an easing string; the
# constructor, initialize and visit_edges; and AnimationUpdateContext's destructor, which
# applies the invalidations the animated properties need. Part 1 has the timing
# properties and the times.
#
# Copyright (c) 2023-2024, Matthew Olsson <mattco@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: In play, current, in effect =============================================================
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:462 Web::Animations::AnimationEffect::is_in_play.
# https://www.w3.org/TR/web-animations-1/#in-play
pub func animations_animation_effect_is_in_play(this: AnimationsAnimationEffect*) -> bool:
# An animation effect is in play if all of the following conditions are met:
# - the animation effect is in the active phase, and
# - the animation effect is associated with an animation that is not finished.
if not animations_animation_effect_is_in_the_active_phase(this):
return false
let animation = this.m_associated_animation else return false
return not animations_animation_is_finished(animation)
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:471 Web::Animations::AnimationEffect::is_current.
# https://www.w3.org/TR/web-animations-1/#current
pub func animations_animation_effect_is_current(this: AnimationsAnimationEffect*) -> bool:
# An animation effect is current if any of the following conditions are true:
# - the animation effect is in play, or
if animations_animation_effect_is_in_play(this):
return true
if let animation = this.m_associated_animation:
let playback_rate = animations_animation_playback_rate(animation)
# - the animation effect is associated with an animation with a playback rate > 0 and the animation effect is
# in the before phase, or
if playback_rate > 0.0 and animations_animation_effect_is_in_the_before_phase(this):
return true
# - the animation effect is associated with an animation with a playback rate < 0 and the animation effect is
# in the after phase, or
if playback_rate < 0.0 and animations_animation_effect_is_in_the_after_phase(this):
return true
# - the animation effect is associated with an animation not in the idle play state with a non-null associated
# timeline that is not monotonically increasing.
if animations_animation_play_state(animation) != BindAnimationPlayState.idle:
if let timeline = animations_animation_timeline(animation):
if not animations_animation_timeline_is_monotonically_increasing(timeline):
return true
return false
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:502 Web::Animations::AnimationEffect::is_in_effect.
# https://www.w3.org/TR/web-animations-1/#in-effect
pub func animations_animation_effect_is_in_effect(this: AnimationsAnimationEffect*) -> bool:
# An animation effect is in effect if its active time, as calculated according to the procedure in
# §4.8.3.1 Calculating the active time, is not unresolved.
return animations_animation_effect_active_time(this) != none
# mark: Phases ==================================================================================
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:510 Web::Animations::AnimationEffect::before_active_boundary_time.
# https://www.w3.org/TR/web-animations-1/#before-active-boundary-time
pub func animations_animation_effect_before_active_boundary_time(this: AnimationsAnimationEffect*) -> AnimationsTimeValue:
# max(min(start delay, end time), 0)
let clamped = animations_time_value_min(this.m_start_delay, animations_animation_effect_end_time(this))
return animations_time_value_max(clamped, animations_time_value_create_zero(animations_animation_effect_associated_timeline(this)))
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:517 Web::Animations::AnimationEffect::after_active_boundary_time.
# https://www.w3.org/TR/web-animations-1/#active-after-boundary-time
pub func animations_animation_effect_after_active_boundary_time(this: AnimationsAnimationEffect*) -> AnimationsTimeValue:
# max(min(start delay + active duration, end time), 0)
let clamped = animations_time_value_min(animations_time_value_add(this.m_start_delay, animations_animation_effect_active_duration(this)), animations_animation_effect_end_time(this))
return animations_time_value_max(clamped, animations_time_value_create_zero(animations_animation_effect_associated_timeline(this)))
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:524 Web::Animations::AnimationEffect::is_in_the_before_phase.
# https://www.w3.org/TR/web-animations-1/#animation-effect-before-phase
pub func animations_animation_effect_is_in_the_before_phase(this: AnimationsAnimationEffect*) -> bool:
# An animation effect is in the before phase if the animation effect’s local time is not unresolved and either of
# the following conditions are met:
let local_time = animations_animation_effect_local_time(this) else return false
# - the local time is less than the before-active boundary time, or
let before_active_boundary_time = animations_animation_effect_before_active_boundary_time(this)
if animations_time_value_lt(local_time, before_active_boundary_time):
return true
# - the animation direction is "backwards" and the local time is equal to the before-active boundary time.
return animations_animation_effect_animation_direction(this) == AnimationsAnimationDirection.backwards and animations_time_value_eq(local_time, before_active_boundary_time)
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:542 Web::Animations::AnimationEffect::is_in_the_after_phase.
# https://www.w3.org/TR/web-animations-1/#animation-effect-after-phase
pub func animations_animation_effect_is_in_the_after_phase(this: AnimationsAnimationEffect*) -> bool:
# An animation effect is in the after phase if the animation effect’s local time is not unresolved and either of
# the following conditions are met:
let local_time = animations_animation_effect_local_time(this) else return false
# - the local time is greater than the active-after boundary time, or
let after_active_boundary_time = animations_animation_effect_after_active_boundary_time(this)
if animations_time_value_gt(local_time, after_active_boundary_time):
return true
# - the animation direction is "forwards" and the local time is equal to the active-after boundary time.
return animations_animation_effect_animation_direction(this) == AnimationsAnimationDirection.forwards and animations_time_value_eq(local_time, after_active_boundary_time)
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:560 Web::Animations::AnimationEffect::is_in_the_active_phase.
# https://www.w3.org/TR/web-animations-1/#animation-effect-active-phase
pub func animations_animation_effect_is_in_the_active_phase(this: AnimationsAnimationEffect*) -> bool:
# An animation effect is in the active phase if the animation effect’s local time is not unresolved and it is not
# in either the before phase nor the after phase.
return animations_animation_effect_local_time(this) != none and not animations_animation_effect_is_in_the_before_phase(this) and not animations_animation_effect_is_in_the_after_phase(this)
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:568 Web::Animations::AnimationEffect::is_in_the_idle_phase.
# https://www.w3.org/TR/web-animations-1/#animation-effect-idle-phase
pub func animations_animation_effect_is_in_the_idle_phase(this: AnimationsAnimationEffect*) -> bool:
# It is often convenient to refer to the case when an animation effect is in none of the above phases as being in
# the idle phase
return not animations_animation_effect_is_in_the_before_phase(this) and not animations_animation_effect_is_in_the_active_phase(this) and not animations_animation_effect_is_in_the_after_phase(this)
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:575 Web::Animations::AnimationEffect::phase.
pub func animations_animation_effect_phase(this: AnimationsAnimationEffect*) -> AnimationsAnimationEffectPhase:
# This is a convenience method that returns the phase of the animation effect, to avoid having to call all of the
# phase functions separately.
let local_time = animations_animation_effect_local_time(this) else return AnimationsAnimationEffectPhase.idle
let before_active_boundary_time = animations_animation_effect_before_active_boundary_time(this)
# - the local time is less than the before-active boundary time, or
# - the animation direction is "backwards" and the local time is equal to the before-active boundary time.
if animations_time_value_lt(local_time, before_active_boundary_time) or (animations_animation_effect_animation_direction(this) == AnimationsAnimationDirection.backwards and animations_time_value_eq(local_time, before_active_boundary_time)):
return AnimationsAnimationEffectPhase.before
let after_active_boundary_time = animations_animation_effect_after_active_boundary_time(this)
# - the local time is greater than the active-after boundary time, or
# - the animation direction is "forwards" and the local time is equal to the active-after boundary time.
if animations_time_value_gt(local_time, after_active_boundary_time) or (animations_animation_effect_animation_direction(this) == AnimationsAnimationDirection.forwards and animations_time_value_eq(local_time, after_active_boundary_time)):
return AnimationsAnimationEffectPhase.after
# - An animation effect is in the active phase if the animation effect’s local time is not unresolved and it is not
# - in either the before phase nor the after phase.
return AnimationsAnimationEffectPhase.active
# mark: Progress ================================================================================
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:601 Web::Animations::AnimationEffect::overall_progress.
# https://www.w3.org/TR/web-animations-1/#overall-progress
pub func animations_animation_effect_overall_progress(this: AnimationsAnimationEffect*) -> f64?:
# 1. If the active time is unresolved, return unresolved.
let active_time = animations_animation_effect_active_time(this) else return none
# 2. Calculate an initial value for overall progress based on the first matching condition from below,
var overall_progress = 0.0
# -> If the iteration duration is zero,
if this.m_iteration_duration.value == 0.0:
# If the animation effect is in the before phase, let overall progress be zero, otherwise, let it be equal to
# the iteration count.
if animations_animation_effect_is_in_the_before_phase(this):
overall_progress = 0.0
else:
overall_progress = this.m_iteration_count
# Otherwise,
else:
# Let overall progress be the result of calculating active time / iteration duration.
overall_progress = animations_time_value_div_time_value(active_time, this.m_iteration_duration)
# 3. Return the result of calculating overall progress + iteration start.
return overall_progress + this.m_iteration_start
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:631 Web::Animations::AnimationEffect::directed_progress.
# https://www.w3.org/TR/web-animations-1/#directed-progress
pub func animations_animation_effect_directed_progress(this: AnimationsAnimationEffect*) -> f64?:
# 1. If the simple iteration progress is unresolved, return unresolved.
let simple_iteration_progress = animations_animation_effect_simple_iteration_progress(this) else return none
# 2. Calculate the current direction using the first matching condition from the following list:
let current_direction = animations_animation_effect_current_direction(this)
# 3. If the current direction is forwards then return the simple iteration progress.
if current_direction == AnimationsAnimationDirection.forwards:
return simple_iteration_progress
# Otherwise, return 1.0 - simple iteration progress.
return 1.0 - simple_iteration_progress
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:650 Web::Animations::AnimationEffect::current_direction.
# https://www.w3.org/TR/web-animations-1/#directed-progress
pub func animations_animation_effect_current_direction(this: AnimationsAnimationEffect*) -> AnimationsAnimationDirection:
# 2. Calculate the current direction using the first matching condition from the following list:
# -> If playback direction is normal,
if this.m_playback_direction == BindPlaybackDirection.normal:
# Let the current direction be forwards.
return AnimationsAnimationDirection.forwards
# -> If playback direction is reverse,
if this.m_playback_direction == BindPlaybackDirection.reverse:
# Let the current direction be reverse.
return AnimationsAnimationDirection.backwards
# -> Otherwise,
# 1. Let d be the current iteration.
var d = animations_animation_effect_current_iteration(this) else trap("VERIFY: a resolved current iteration")
# 2. If playback direction is alternate-reverse increment d by 1.
if this.m_playback_direction == BindPlaybackDirection.alternate_reverse:
d += 1.0
# 3. If d % 2 == 0, let the current direction be forwards, otherwise let the current direction be reverse. If d
# is infinity, let the current direction be forwards.
if math.is_infinite(d):
return AnimationsAnimationDirection.forwards
if math.mod(d, 2.0) == 0.0:
return AnimationsAnimationDirection.forwards
return AnimationsAnimationDirection.backwards
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:682 Web::Animations::AnimationEffect::simple_iteration_progress.
# https://www.w3.org/TR/web-animations-1/#simple-iteration-progress
pub func animations_animation_effect_simple_iteration_progress(this: AnimationsAnimationEffect*) -> f64?:
# 1. If the overall progress is unresolved, return unresolved.
let overall_progress = animations_animation_effect_overall_progress(this) else return none
# 2. If overall progress is infinity, let the simple iteration progress be iteration start % 1.0, otherwise, let
# the simple iteration progress be overall progress % 1.0.
var simple_iteration_progress = math.mod(this.m_iteration_start, 1.0) if math.is_infinite(overall_progress) else math.mod(overall_progress, 1.0)
# 3. If all of the following conditions are true,
# - the simple iteration progress calculated above is zero, and
# - the animation effect is in the active phase or the after phase, and
# - the active time is equal to the active duration, and
# - the iteration count is not equal to zero.
let active_time = animations_animation_effect_active_time(this)
if simple_iteration_progress == 0.0 and (animations_animation_effect_is_in_the_active_phase(this) or animations_animation_effect_is_in_the_after_phase(this)):
if let time = active_time:
if animations_time_value_eq(time, animations_animation_effect_active_duration(this)) and this.m_iteration_count != 0.0:
# let the simple iteration progress be 1.0.
simple_iteration_progress = 1.0
# 4. Return simple iteration progress.
return simple_iteration_progress
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:709 Web::Animations::AnimationEffect::current_iteration.
# https://www.w3.org/TR/web-animations-1/#current-iteration
pub func animations_animation_effect_current_iteration(this: AnimationsAnimationEffect*) -> f64?:
# 1. If the active time is unresolved, return unresolved.
if animations_animation_effect_active_time(this) == none:
return none
# 2. If the animation effect is in the after phase and the iteration count is infinity, return infinity.
if animations_animation_effect_is_in_the_after_phase(this) and math.is_infinite(this.m_iteration_count):
return this.m_iteration_count
# 3. If the simple iteration progress is 1.0, return floor(overall progress) - 1.
if let simple_iteration_progress = animations_animation_effect_simple_iteration_progress(this):
if simple_iteration_progress == 1.0:
return math.floor(animations_animation_effect_overall_progress(this) else trap("VERIFY: a resolved overall progress")) - 1.0
# 4. Otherwise, return floor(overall progress).
return math.floor(animations_animation_effect_overall_progress(this) else trap("VERIFY: a resolved overall progress"))
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:730 Web::Animations::AnimationEffect::transformed_progress.
# https://www.w3.org/TR/web-animations-1/#transformed-progress
pub func animations_animation_effect_transformed_progress(this: AnimationsAnimationEffect*) -> f64?:
# 1. If the directed progress is unresolved, return unresolved.
let directed_progress = animations_animation_effect_directed_progress(this) else return none
# 2. Calculate the value of the before flag as follows:
# 1. Determine the current direction using the procedure defined in §4.9.1 Calculating the directed progress.
let current_direction = animations_animation_effect_current_direction(this)
# 2. If the current direction is forwards, let going forwards be true, otherwise it is false.
let going_forwards = current_direction == AnimationsAnimationDirection.forwards
# 3. The before flag is set if the animation effect is in the before phase and going forwards is true; or if the animation effect
# is in the after phase and going forwards is false.
let before_flag = (animations_animation_effect_is_in_the_before_phase(this) and going_forwards) or (animations_animation_effect_is_in_the_after_phase(this) and not going_forwards)
# 3. Return the result of evaluating the animation effect’s timing function passing directed progress as the input progress value and
# before flag as the before flag.
return css_easing_function_evaluate_at(&this.m_timing_function, directed_progress, before_flag)
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:754 Web::Animations::AnimationEffect::parse_easing_string.
pub func animations_animation_effect_parse_easing_string(value: ak.StringView) -> CssEasingFunction?:
var parsing_params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(&parsing_params)
if let style_value = parse_css_value(parsing_params, value, css_data.PropertyId.animation_timing_function):
if css_style_value_is_unresolved(style_value) or css_style_value_is_css_wide_keyword(style_value):
return none
let easing_values = css_style_value_list_values(css_style_value_as_value_list(style_value))
if ak.vector_size(&easing_values) != 1:
return none
# FIXME: We should absolutize the style value to resolve relative lengths within calcs
return css_easing_function_from_style_value(*ak.vector_at_const(&easing_values, 0))
return none
# mark: Construction and edges ==================================================================
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:772 Web::Animations::AnimationEffect::AnimationEffect (protected).
pub func animations_animation_effect_construct(this: AnimationsAnimationEffect*, realm: Realm*):
# The C++ default member initializers.
this.m_start_delay = AnimationsTimeValue(type_ = AnimationsTimeValueType.milliseconds, value = 0.0)
this.m_end_delay = AnimationsTimeValue(type_ = AnimationsTimeValueType.milliseconds, value = 0.0)
this.m_fill_mode = BindFillMode.auto
this.m_iteration_count = 1.0
this.m_specified_iteration_duration = F64OrString.string(ak.string_from_str("auto"))
this.m_iteration_duration = AnimationsTimeValue(type_ = AnimationsTimeValueType.milliseconds, value = 0.0)
this.m_playback_direction = BindPlaybackDirection.normal
this.m_timing_function = css_easing_function_linear()
this.m_previous_phase = AnimationsAnimationEffectPhase.idle
bind_platform_object_construct_realm(this.bind_platform_object(), realm)
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:777 Web::Animations::AnimationEffect::initialize (override).
pub func animations_animation_effect_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.animation_effect)
js_cell_initialize_impl(base, realm)
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:783 Web::Animations::AnimationEffect::visit_edges (override).
pub func animations_animation_effect_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (AnimationsAnimationEffect*)base
bind_platform_object_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t(visitor, this.m_associated_animation)
# mark: AnimationUpdateContext ==================================================================
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:789 Web::Animations::compute_required_invalidation_for_animated_properties.
func animations_compute_required_invalidation_for_animated_properties(old_properties: const AnimationsAnimationUpdateContextElementDataPropertyMap*, new_properties: const AnimationsAnimationUpdateContextElementDataPropertyMap*) -> CssRequiredInvalidationAfterStyleChange:
var invalidation = CssRequiredInvalidationAfterStyleChange()
let first = (usize)css_data.first_longhand_property_id
let last = (usize)css_data.last_longhand_property_id
var old_and_new_properties = ak.bitmap_create(css_data.number_of_longhand_properties, false) else trap("MUST: Bitmap::create")
for entry in ak.hash_map_begin_const(old_properties):
ak.bitmap_set(&old_and_new_properties, (usize)entry.key - first, true)
for entry in ak.hash_map_begin_const(new_properties):
ak.bitmap_set(&old_and_new_properties, (usize)entry.key - first, true)
let bits = ak.bitmap_view(&old_and_new_properties)
for i in first..=last:
if not ak.bitmap_view_get(&bits, i - first):
continue
let property_id = (css_data.PropertyId)(u16)i
var old_value: CssStyleValue*? = none
if let value = ak.hash_map_get_k_const(old_properties, property_id):
old_value = *value
var new_value: CssStyleValue*? = none
if let value = ak.hash_map_get_k_const(new_properties, property_id):
new_value = *value
if old_value == none and new_value == none:
continue
css_required_invalidation_after_style_change_or_assign(&invalidation, css_compute_property_invalidation(property_id, old_value, new_value))
return invalidation
## Ported from Libraries/LibWeb/Animations/AnimationEffect.cpp:810 Web::Animations::AnimationUpdateContext::~AnimationUpdateContext.
pub func animations_animation_update_context_destruct(this: AnimationsAnimationUpdateContext*):
for it in ak.hash_map_begin(&this.elements):
let style = it.value.target_style else continue
let element = &it.key
let target = dom_abstract_element_element(element)
let new_properties = css_computed_properties_animated_property_values(style)
var invalidation = animations_compute_required_invalidation_for_animated_properties(&it.value.animated_properties_before_update, &new_properties)
if css_required_invalidation_after_style_change_is_none(&invalidation):
continue
# Traversal of the subtree is necessary to update the animated properties inherited from the target element.
var iterator = dom_node_subtree(target.dom_node()).iterator()
while let node = iterator.next():
let subtree_element = as_if_dom_element(node) else continue
if dom_element_cascaded_properties(subtree_element, none) == none:
iterator.skip_children()
continue
let element_invalidation = dom_element_recompute_inherited_style(subtree_element)
if css_required_invalidation_after_style_change_is_none(&element_invalidation):
iterator.skip_children()
continue
css_required_invalidation_after_style_change_or_assign(&invalidation, element_invalidation)
# NB: Called from animation update context destructor during style recalculation.
if let pseudo_element = dom_abstract_element_pseudo_element(element):
if let pseudo_element_node = dom_element_get_pseudo_element_node(target, pseudo_element):
layout_node_with_style_apply_style(pseudo_element_node, style)
else:
if let layout_node = dom_element_unsafe_layout_node(target):
layout_node_with_style_apply_style(layout_node, style)
if invalidation.relayout:
dom_node_set_needs_layout_update(target.dom_node(), DomSetNeedsLayoutReason.keyframe_effect)
if invalidation.rebuild_layout_tree:
# We mark layout tree for rebuild starting from parent element to correctly invalidate
# "display" property change to/from "contents" value.
if let parent_element = dom_node_parent_element(target.dom_node()):
dom_node_set_needs_layout_tree_update(parent_element.dom_node(), true, DomSetNeedsLayoutTreeUpdateReason.keyframe_effect)
else:
dom_node_set_needs_layout_tree_update(target.dom_node(), true, DomSetNeedsLayoutTreeUpdateReason.keyframe_effect)
if invalidation.repaint:
if invalidation.rebuild_accumulated_visual_contexts:
dom_document_set_needs_accumulated_visual_contexts_update(dom_abstract_element_document(element), true)
dom_node_set_needs_repaint(target.dom_node())
if invalidation.rebuild_stacking_context_tree:
dom_document_invalidate_stacking_context_tree(dom_abstract_element_document(element))