From cdf0d3e905fea06ba6d2fbd00e6f3f1141b02099 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 6 May 2023 16:33:06 +0200 Subject: [PATCH] LibWeb: Add to_px() helpers to CSS::Size and CSS::PercentageOr Old pattern: foo.resolved(node, reference_value).to_px(node) New pattern: foo.to_px(node, reference_value) Also, the reference value for to_px() is a CSSPixels, which means we don't have to synthesize a CSS::Length just for this call. --- Userland/Libraries/LibWeb/CSS/PercentageOr.h | 5 +++++ Userland/Libraries/LibWeb/CSS/Size.cpp | 5 +++++ Userland/Libraries/LibWeb/CSS/Size.h | 2 ++ 3 files changed, 12 insertions(+) diff --git a/Userland/Libraries/LibWeb/CSS/PercentageOr.h b/Userland/Libraries/LibWeb/CSS/PercentageOr.h index a790cb7b10..ed575b28df 100644 --- a/Userland/Libraries/LibWeb/CSS/PercentageOr.h +++ b/Userland/Libraries/LibWeb/CSS/PercentageOr.h @@ -88,6 +88,11 @@ public: VERIFY_NOT_REACHED(); } + CSSPixels to_px(Layout::Node const& layout_node, CSSPixels reference_value) const + { + return resolved(layout_node, Length::make_px(reference_value)).to_px(layout_node); + } + T resolved(Layout::Node const& layout_node, T const& reference_value) const { return m_value.visit( diff --git a/Userland/Libraries/LibWeb/CSS/Size.cpp b/Userland/Libraries/LibWeb/CSS/Size.cpp index 0513d254ff..5df737b3be 100644 --- a/Userland/Libraries/LibWeb/CSS/Size.cpp +++ b/Userland/Libraries/LibWeb/CSS/Size.cpp @@ -14,6 +14,11 @@ Size::Size(Type type, LengthPercentage length_percentage) { } +CSSPixels Size::to_px(Layout::Node const& node, CSSPixels reference_value) const +{ + return m_length_percentage.resolved(node, CSS::Length::make_px(reference_value)).to_px(node); +} + CSS::Length Size::resolved(Layout::Node const& node, Length const& reference_value) const { return m_length_percentage.resolved(node, reference_value); diff --git a/Userland/Libraries/LibWeb/CSS/Size.h b/Userland/Libraries/LibWeb/CSS/Size.h index 77aaf2da59..ff12a824b1 100644 --- a/Userland/Libraries/LibWeb/CSS/Size.h +++ b/Userland/Libraries/LibWeb/CSS/Size.h @@ -47,6 +47,8 @@ public: // FIXME: This is a stopgap API that will go away once all layout code is aware of CSS::Size. CSS::Length resolved(Layout::Node const&, Length const& reference_value) const; + [[nodiscard]] CSSPixels to_px(Layout::Node const&, CSSPixels reference_value) const; + bool contains_percentage() const; CalculatedStyleValue const& calculated() const