From c8b9c137a1e5519213457324f7aab8830a049424 Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Fri, 23 Feb 2024 14:31:00 -0700 Subject: [PATCH] LibWeb: Handle discrete properties This also changes transform's animation-type to by-computed-value. It is far easier to handle since we switch on StyleValue::type(), and it might be the case that this applies to all custom animated properties and we don't need "custom" at all, but let's wait until we get to those properties to make that decision. --- Userland/Libraries/LibWeb/CSS/Properties.json | 2 +- .../Libraries/LibWeb/CSS/StyleComputer.cpp | 42 +++++++++++++------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Properties.json b/Userland/Libraries/LibWeb/CSS/Properties.json index f2c91d0c84..11d46f9a1b 100644 --- a/Userland/Libraries/LibWeb/CSS/Properties.json +++ b/Userland/Libraries/LibWeb/CSS/Properties.json @@ -2219,7 +2219,7 @@ ] }, "transform": { - "animation-type": "custom", + "animation-type": "by-computed-value", "inherited": false, "initial": "none", "affects-layout": false, diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 48650acfde..ed87b96e09 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -758,14 +758,8 @@ static ErrorOr cascade_custom_properties(DOM::Element& element, Optional> interpolate_property(StyleValue const& from, StyleValue const& to, float delta) +static ErrorOr> interpolate_value(StyleValue const& from, StyleValue const& to, float delta) { - if (from.type() != to.type()) { - if (delta > 0.999f) - return to; - return from; - } - auto interpolate_raw = [delta = static_cast(delta)](auto from, auto to) { return static_cast>(static_cast(from) + static_cast(to - from) * delta); }; @@ -804,8 +798,8 @@ static ErrorOr> interpolate_property(StyleValue const& auto& from_position = from.as_position(); auto& to_position = to.as_position(); return PositionStyleValue::create( - TRY(interpolate_property(from_position.edge_x(), to_position.edge_x(), delta))->as_edge(), - TRY(interpolate_property(from_position.edge_y(), to_position.edge_y(), delta))->as_edge()); + TRY(interpolate_value(from_position.edge_x(), to_position.edge_x(), delta))->as_edge(), + TRY(interpolate_value(from_position.edge_y(), to_position.edge_y(), delta))->as_edge()); } case StyleValue::Type::Rect: { auto from_rect = from.as_rect().rect(); @@ -831,7 +825,7 @@ static ErrorOr> interpolate_property(StyleValue const& StyleValueVector interpolated_values; interpolated_values.ensure_capacity(from_input_values.size()); for (size_t i = 0; i < from_input_values.size(); ++i) - interpolated_values.append(TRY(interpolate_property(*from_input_values[i], *to_input_values[i], delta))); + interpolated_values.append(TRY(interpolate_value(*from_input_values[i], *to_input_values[i], delta))); return TransformationStyleValue::create(from_transform.transform_function(), move(interpolated_values)); } @@ -844,7 +838,7 @@ static ErrorOr> interpolate_property(StyleValue const& StyleValueVector interpolated_values; interpolated_values.ensure_capacity(from_list.size()); for (size_t i = 0; i < from_list.size(); ++i) - interpolated_values.append(TRY(interpolate_property(from_list.values()[i], to_list.values()[i], delta))); + interpolated_values.append(TRY(interpolate_value(from_list.values()[i], to_list.values()[i], delta))); return StyleValueList::create(move(interpolated_values), from_list.separator()); } @@ -853,6 +847,30 @@ static ErrorOr> interpolate_property(StyleValue const& } } +static ErrorOr> interpolate_property(PropertyID property_id, StyleValue const& from, StyleValue const& to, float delta) +{ + if (from.type() != to.type()) { + if (delta > 0.999f) + return to; + return from; + } + + auto animation_type = animation_type_from_longhand_property(property_id); + switch (animation_type) { + case AnimationType::ByComputedValue: + return interpolate_value(from, to, delta); + case AnimationType::None: + return to; + // FIXME: Handle all custom animatable properties + case AnimationType::Custom: + // FIXME: Handle repeatable-list animatable properties + case AnimationType::RepeatableList: + case AnimationType::Discrete: + default: + return delta >= 0.5f ? to : from; + } +} + ErrorOr StyleComputer::collect_animation_into(JS::NonnullGCPtr effect, StyleProperties& style_properties) const { auto animation = effect->associated_animation(); @@ -933,7 +951,7 @@ ErrorOr StyleComputer::collect_animation_into(JS::NonnullGCPtr {} = {}", string_from_property_id(it.key), progress_in_keyframe, start->to_string(), end->to_string(), next_value->to_string()); style_properties.set_property(it.key, next_value); }