Jelajahi Sumber

LibWeb: Add to_px() helpers to CSS::Size and CSS::PercentageOr<T>

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.
Andreas Kling 2 tahun lalu
induk
melakukan
cdf0d3e905

+ 5 - 0
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(

+ 5 - 0
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);

+ 2 - 0
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