Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / animations / time_value.lucb

#==============================================================================================
#
#   time_value - Animations::TimeValue: a time in milliseconds or a percentage of a
#                progress-based timeline
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Animations/TimeValue.cpp (lines 1-79) with the inline members
#       of TimeValue.h, by region p4a: the arithmetic operators (named per DESIGN.md ยง2.8:
#       neg, mul, sub, add, div, div_time_value, cmp, eq) and AK's min/max over them, the
#       zero time of a timeline, and the conversions to and from CSSNumberish. A CSSNumberish
#       holding a CSSNumericValue (the typed OM) is phase 3.
#
#       Copyright (c) 2025, Callum Law <callumlaw1709@outlook.com>.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Conversions =============================================================================

## Ported from Libraries/LibWeb/Animations/TimeValue.cpp:14 Web::Animations::TimeValue::from_css_numberish.
pub func animations_time_value_from_css_numberish(time: CssCssNumberish, abstract_element: const DomAbstractElement*) -> AnimationsTimeValue:
    _ = abstract_element
    match time:
        .double_(value):
            return AnimationsTimeValue(type_ = AnimationsTimeValueType.milliseconds, value = value)
        .css_numeric_value(numeric_value):
            # The unit values and calculations of a CSSNumericValue (the typed OM) are phase 3.
            _ = numeric_value
            trap("unported (P3): Web::Animations::TimeValue::from_css_numberish (a CSSNumericValue)")

## Ported from Libraries/LibWeb/Animations/TimeValue.cpp:57 Web::Animations::TimeValue::create_zero.
pub func animations_time_value_create_zero(timeline: AnimationsAnimationTimeline*?) -> AnimationsTimeValue:
    if let given = timeline:
        if animations_animation_timeline_is_progress_based(given):
            return AnimationsTimeValue(type_ = AnimationsTimeValueType.percentage, value = 0.0)

    return AnimationsTimeValue(type_ = AnimationsTimeValueType.milliseconds, value = 0.0)

## Ported from Libraries/LibWeb/Animations/TimeValue.cpp:65 Web::Animations::TimeValue::as_css_numberish.
pub func animations_time_value_as_css_numberish(this: AnimationsTimeValue, realm: Realm*) -> CssCssNumberish:
    match this.type_:
        .milliseconds:
            return CssCssNumberish.double_(this.value)
        .percentage:
            let numeric_value = css_unit_value_create(realm, this.value, ak.fly_string_from_str("percent"))
            return CssCssNumberish.css_numeric_value((CssNumericValue*)numeric_value)
        _:
            trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/Animations/TimeValue.h:83 Web::Animations::NullableCSSNumberish::from_optional_css_numberish_time.
pub func animations_nullable_css_numberish_from_optional_css_numberish_time(realm: Realm*, value: AnimationsTimeValue?) -> AnimationsNullableCssNumberish:
    if let time = value:
        return animations_nullable_css_numberish_from_css_numberish(animations_time_value_as_css_numberish(time, realm))

    return AnimationsNullableCssNumberish.empty

## A CSSNumberish as a NullableCSSNumberish (FlattenVariant's converting constructor).
pub func animations_nullable_css_numberish_from_css_numberish(value: CssCssNumberish) -> AnimationsNullableCssNumberish:
    match value:
        .double_(number):
            return AnimationsNullableCssNumberish.double_(number)
        .css_numeric_value(numeric_value):
            return AnimationsNullableCssNumberish.css_numeric_value(numeric_value)

# mark: Operators ===============================================================================

## Ported from Libraries/LibWeb/Animations/TimeValue.h:25 Web::Animations::TimeValue::operator- (negation).
pub func animations_time_value_neg(this: AnimationsTimeValue) -> AnimationsTimeValue:
    return AnimationsTimeValue(type_ = this.type_, value = -this.value)

## Ported from Libraries/LibWeb/Animations/TimeValue.h:30 Web::Animations::TimeValue::operator*(double).
pub func animations_time_value_mul(this: AnimationsTimeValue, other: f64) -> AnimationsTimeValue:
    return AnimationsTimeValue(type_ = this.type_, value = this.value * other)

## Ported from Libraries/LibWeb/Animations/TimeValue.h:35 Web::Animations::TimeValue::operator-(TimeValue).
pub func animations_time_value_sub(this: AnimationsTimeValue, other: AnimationsTimeValue) -> AnimationsTimeValue:
    assert(this.type_ == other.type_)
    return AnimationsTimeValue(type_ = this.type_, value = this.value - other.value)

## Ported from Libraries/LibWeb/Animations/TimeValue.h:41 Web::Animations::TimeValue::operator+(TimeValue).
pub func animations_time_value_add(this: AnimationsTimeValue, other: AnimationsTimeValue) -> AnimationsTimeValue:
    assert(this.type_ == other.type_)
    return AnimationsTimeValue(type_ = this.type_, value = this.value + other.value)

## Ported from Libraries/LibWeb/Animations/TimeValue.h:47 Web::Animations::TimeValue::operator/(double).
pub func animations_time_value_div(this: AnimationsTimeValue, divisor: f64) -> AnimationsTimeValue:
    return AnimationsTimeValue(type_ = this.type_, value = this.value / divisor)

## Ported from Libraries/LibWeb/Animations/TimeValue.h:52 Web::Animations::TimeValue::operator/(TimeValue).
pub func animations_time_value_div_time_value(this: AnimationsTimeValue, other: AnimationsTimeValue) -> f64:
    assert(this.type_ == other.type_)
    return this.value / other.value

## Ported from Libraries/LibWeb/Animations/TimeValue.h:58 Web::Animations::TimeValue::operator<=>.
pub func animations_time_value_cmp(this: AnimationsTimeValue, other: AnimationsTimeValue) -> i32:
    assert(this.type_ == other.type_)

    if this.value < other.value:
        return -1
    if this.value > other.value:
        return 1
    return 0

## `a < b` through TimeValue::operator<=>.
pub func animations_time_value_lt(this: AnimationsTimeValue, other: AnimationsTimeValue) -> bool:
    return animations_time_value_cmp(this, other) < 0

## `a <= b` through TimeValue::operator<=>.
pub func animations_time_value_le(this: AnimationsTimeValue, other: AnimationsTimeValue) -> bool:
    return animations_time_value_cmp(this, other) <= 0

## `a > b` through TimeValue::operator<=>.
pub func animations_time_value_gt(this: AnimationsTimeValue, other: AnimationsTimeValue) -> bool:
    return animations_time_value_cmp(this, other) > 0

## `a >= b` through TimeValue::operator<=>.
pub func animations_time_value_ge(this: AnimationsTimeValue, other: AnimationsTimeValue) -> bool:
    return animations_time_value_cmp(this, other) >= 0

## Ported from Libraries/LibWeb/Animations/TimeValue.h:69 Web::Animations::TimeValue::operator==.
pub func animations_time_value_eq(this: AnimationsTimeValue, other: AnimationsTimeValue) -> bool:
    return this.type_ == other.type_ and this.value == other.value

## AK::min(a, b) over TimeValues: `b < a ? b : a`.
pub func animations_time_value_min(a: AnimationsTimeValue, b: AnimationsTimeValue) -> AnimationsTimeValue:
    return b if animations_time_value_lt(b, a) else a

## AK::max(a, b) over TimeValues: `a < b ? b : a`.
pub func animations_time_value_max(a: AnimationsTimeValue, b: AnimationsTimeValue) -> AnimationsTimeValue:
    return b if animations_time_value_lt(a, b) else a