LibWeb/CSS: Store linear-gradient() angle as a StyleValue

This means we now support calc() there too.
This commit is contained in:
Sam Atkins 2025-11-28 17:23:13 +00:00
parent 73fbaaba77
commit d327f677c5
Notes: github-actions[bot] 2025-12-01 11:10:08 +00:00
5 changed files with 24 additions and 22 deletions

View file

@ -8,6 +8,8 @@
*/
#include "LinearGradientStyleValue.h"
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/DisplayListRecorder.h>
@ -53,8 +55,8 @@ String LinearGradientStyleValue::to_string(SerializationMode mode) const
[&](SideOrCorner side_or_corner) {
builder.appendff("{}{}", m_properties.gradient_type == GradientType::Standard ? "to "sv : ""sv, side_or_corner_to_string(side_or_corner));
},
[&](Angle const& angle) {
builder.append(angle.to_string());
[&](NonnullRefPtr<StyleValue const> const& angle) {
builder.append(angle->to_string(mode));
});
if (has_color_space)
@ -114,8 +116,14 @@ float LinearGradientStyleValue::angle_degrees(CSSPixelSize gradient_size) const
return angle + 180.0;
return angle;
},
[&](Angle const& angle) {
return angle.to_degrees();
[&](NonnullRefPtr<StyleValue const> const& style_value) {
if (style_value->is_angle())
return style_value->as_angle().angle().to_degrees();
if (style_value->is_calculated()) {
if (auto maybe_angle = style_value->as_calculated().resolve_angle({}); maybe_angle.has_value())
return maybe_angle->to_degrees();
}
return 0.0;
});
}

View file

@ -10,8 +10,6 @@
#pragma once
#include <AK/Vector.h>
#include <LibWeb/CSS/Angle.h>
#include <LibWeb/CSS/Percentage.h>
#include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
#include <LibWeb/Painting/GradientPainting.h>
@ -32,7 +30,7 @@ enum class SideOrCorner {
class LinearGradientStyleValue final : public AbstractImageStyleValue {
public:
using GradientDirection = Variant<Angle, SideOrCorner>;
using GradientDirection = Variant<NonnullRefPtr<StyleValue const>, SideOrCorner>;
enum class GradientType {
Standard,
@ -43,7 +41,7 @@ public:
{
VERIFY(!color_stop_list.is_empty());
bool any_non_legacy = color_stop_list.find_first_index_if([](auto const& stop) { return !stop.color_stop.color->is_keyword() && stop.color_stop.color->as_color().color_syntax() == ColorSyntax::Modern; }).has_value();
return adopt_ref(*new (nothrow) LinearGradientStyleValue(direction, move(color_stop_list), type, repeating, interpolation_method, any_non_legacy ? ColorSyntax::Modern : ColorSyntax::Legacy));
return adopt_ref(*new (nothrow) LinearGradientStyleValue(move(direction), move(color_stop_list), type, repeating, interpolation_method, any_non_legacy ? ColorSyntax::Modern : ColorSyntax::Legacy));
}
virtual String to_string(SerializationMode) const override;
@ -78,7 +76,7 @@ public:
private:
LinearGradientStyleValue(GradientDirection direction, Vector<ColorStopListElement> color_stop_list, GradientType type, GradientRepeating repeating, Optional<InterpolationMethod> interpolation_method, ColorSyntax color_syntax)
: AbstractImageStyleValue(Type::LinearGradient)
, m_properties { .direction = direction, .color_stop_list = move(color_stop_list), .gradient_type = type, .repeating = repeating, .interpolation_method = interpolation_method, .color_syntax = color_syntax }
, m_properties { .direction = move(direction), .color_stop_list = move(color_stop_list), .gradient_type = type, .repeating = repeating, .interpolation_method = interpolation_method, .color_syntax = color_syntax }
{
}