瀏覽代碼

LibWeb: Make computed flex-grow and flex-shrink always available

These values are not allowed to be absent (auto/none/etc) so we don't
need to use Optional<float> for them. This simplifies some things.
Andreas Kling 3 年之前
父節點
當前提交
07f15aa550

+ 9 - 6
Userland/Libraries/LibWeb/CSS/ComputedValues.h

@@ -34,6 +34,8 @@ public:
     static CSS::Overflow overflow() { return CSS::Overflow::Visible; }
     static CSS::BoxSizing box_sizing() { return CSS::BoxSizing::ContentBox; }
     static CSS::PointerEvents pointer_events() { return CSS::PointerEvents::Auto; }
+    static float flex_grow() { return 0.0f; }
+    static float flex_shrink() { return 1.0f; }
 };
 
 struct BorderData {
@@ -78,8 +80,8 @@ public:
     CSS::FlexDirection flex_direction() const { return m_noninherited.flex_direction; }
     CSS::FlexWrap flex_wrap() const { return m_noninherited.flex_wrap; }
     FlexBasisData const& flex_basis() const { return m_noninherited.flex_basis; }
-    Optional<float> const& flex_grow_factor() const { return m_noninherited.flex_grow_factor; }
-    Optional<float> const& flex_shrink_factor() const { return m_noninherited.flex_shrink_factor; }
+    float flex_grow() const { return m_noninherited.flex_grow; }
+    float flex_shrink() const { return m_noninherited.flex_shrink; }
     CSS::AlignItems align_items() const { return m_noninherited.align_items; }
     Optional<float> const& opacity() const { return m_noninherited.opacity; }
     CSS::JustifyContent justify_content() const { return m_noninherited.justify_content; }
@@ -174,8 +176,9 @@ protected:
         CSS::FlexDirection flex_direction { InitialValues::flex_direction() };
         CSS::FlexWrap flex_wrap { InitialValues::flex_wrap() };
         CSS::FlexBasisData flex_basis {};
-        Optional<float> flex_grow_factor;
-        Optional<float> flex_shrink_factor;
+        float flex_grow { InitialValues::flex_grow() };
+        float flex_shrink { InitialValues::flex_shrink() };
+        ;
         CSS::AlignItems align_items { InitialValues::align_items() };
         CSS::JustifyContent justify_content { InitialValues::justify_content() };
         CSS::Overflow overflow_x { InitialValues::overflow() };
@@ -230,8 +233,8 @@ public:
     void set_flex_direction(CSS::FlexDirection value) { m_noninherited.flex_direction = value; }
     void set_flex_wrap(CSS::FlexWrap value) { m_noninherited.flex_wrap = value; }
     void set_flex_basis(FlexBasisData value) { m_noninherited.flex_basis = value; }
-    void set_flex_grow_factor(Optional<float> value) { m_noninherited.flex_grow_factor = value; }
-    void set_flex_shrink_factor(Optional<float> value) { m_noninherited.flex_shrink_factor = value; }
+    void set_flex_grow(float value) { m_noninherited.flex_grow = value; }
+    void set_flex_shrink(float value) { m_noninherited.flex_shrink = value; }
     void set_align_items(CSS::AlignItems value) { m_noninherited.align_items = value; }
     void set_opacity(Optional<float> value) { m_noninherited.opacity = value; }
     void set_justify_content(CSS::JustifyContent value) { m_noninherited.justify_content = value; }

+ 4 - 12
Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp

@@ -497,18 +497,10 @@ RefPtr<StyleValue> ResolvedCSSStyleDeclaration::style_value_for_property(Layout:
             VERIFY_NOT_REACHED();
         }
         break;
-    case CSS::PropertyID::FlexGrow: {
-        auto maybe_grow_factor = layout_node.computed_values().flex_grow_factor();
-        if (!maybe_grow_factor.has_value())
-            return {};
-        return NumericStyleValue::create_float(maybe_grow_factor.release_value());
-    }
-    case CSS::PropertyID::FlexShrink: {
-        auto maybe_shrink_factor = layout_node.computed_values().flex_shrink_factor();
-        if (!maybe_shrink_factor.has_value())
-            return {};
-        return NumericStyleValue::create_float(maybe_shrink_factor.release_value());
-    }
+    case CSS::PropertyID::FlexGrow:
+        return NumericStyleValue::create_float(layout_node.computed_values().flex_grow());
+    case CSS::PropertyID::FlexShrink:
+        return NumericStyleValue::create_float(layout_node.computed_values().flex_shrink());
     case CSS::PropertyID::Opacity: {
         auto maybe_opacity = layout_node.computed_values().opacity();
         if (!maybe_opacity.has_value())

+ 9 - 12
Userland/Libraries/LibWeb/CSS/StyleProperties.cpp

@@ -194,25 +194,22 @@ Optional<CSS::FlexBasisData> StyleProperties::flex_basis() const
     return {};
 }
 
-Optional<float> StyleProperties::flex_grow_factor() const
+float StyleProperties::flex_grow() const
 {
     auto value = property(CSS::PropertyID::FlexGrow);
-    if (!value.has_value())
-        return {};
-    if (value.value()->has_number())
-        return value.value()->to_number();
-    return {};
+    if (!value.has_value() || !value.value()->has_number())
+        return 0;
+    return value.value()->to_number();
 }
 
-Optional<float> StyleProperties::flex_shrink_factor() const
+float StyleProperties::flex_shrink() const
 {
     auto value = property(CSS::PropertyID::FlexShrink);
-    if (!value.has_value())
-        return {};
-    if (value.value()->has_number())
-        return value.value()->to_number();
-    return {};
+    if (!value.has_value() || !value.value()->has_number())
+        return 1;
+    return value.value()->to_number();
 }
+
 Optional<CSS::JustifyContent> StyleProperties::justify_content() const
 {
     auto value = property(CSS::PropertyID::JustifyContent);

+ 2 - 2
Userland/Libraries/LibWeb/CSS/StyleProperties.h

@@ -56,8 +56,8 @@ public:
     Optional<CSS::FlexDirection> flex_direction() const;
     Optional<CSS::FlexWrap> flex_wrap() const;
     Optional<CSS::FlexBasisData> flex_basis() const;
-    Optional<float> flex_grow_factor() const;
-    Optional<float> flex_shrink_factor() const;
+    float flex_grow() const;
+    float flex_shrink() const;
     Optional<CSS::AlignItems> align_items() const;
     Optional<float> opacity() const;
     Optional<CSS::JustifyContent> justify_content() const;

+ 6 - 6
Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp

@@ -531,9 +531,9 @@ void FlexFormattingContext::determine_main_size_of_flex_container(bool const mai
             float max_content_contribution = calculated_main_size(flex_item.box);
             float max_content_flex_fraction = max_content_contribution - flex_item.flex_base_size;
             if (max_content_flex_fraction > 0) {
-                max_content_flex_fraction /= max(flex_item.box.computed_values().flex_grow_factor().value_or(1), 1.0f);
+                max_content_flex_fraction /= max(flex_item.box.computed_values().flex_grow(), 1.0f);
             } else {
-                max_content_flex_fraction /= max(flex_item.box.computed_values().flex_shrink_factor().value_or(1), 1.0f) * flex_item.flex_base_size;
+                max_content_flex_fraction /= max(flex_item.box.computed_values().flex_shrink(), 1.0f) * flex_item.flex_base_size;
             }
             flex_item.max_content_flex_fraction = max_content_flex_fraction;
 
@@ -547,9 +547,9 @@ void FlexFormattingContext::determine_main_size_of_flex_container(bool const mai
         for (auto& flex_item : m_flex_items) {
             auto product = 0;
             if (flex_item.max_content_flex_fraction > 0) {
-                product = largest_max_content_flex_fraction * flex_item.box.computed_values().flex_grow_factor().value_or(1);
+                product = largest_max_content_flex_fraction * flex_item.box.computed_values().flex_grow();
             } else {
-                product = largest_max_content_flex_fraction * max(flex_item.box.computed_values().flex_shrink_factor().value_or(1), 1.0f) * flex_item.flex_base_size;
+                product = largest_max_content_flex_fraction * max(flex_item.box.computed_values().flex_shrink(), 1.0f) * flex_item.flex_base_size;
             }
             result += flex_item.flex_base_size + product;
         }
@@ -620,9 +620,9 @@ void FlexFormattingContext::resolve_flexible_lengths(float const main_available_
 
         for (auto& flex_item : flex_line.items) {
             if (used_flex_factor == FlexFactor::FlexGrowFactor)
-                flex_item->flex_factor = flex_item->box.computed_values().flex_grow_factor();
+                flex_item->flex_factor = flex_item->box.computed_values().flex_grow();
             else if (used_flex_factor == FlexFactor::FlexShrinkFactor)
-                flex_item->flex_factor = flex_item->box.computed_values().flex_shrink_factor();
+                flex_item->flex_factor = flex_item->box.computed_values().flex_shrink();
         }
 
         // 6.2. Size inflexible items

+ 2 - 2
Userland/Libraries/LibWeb/Layout/Node.cpp

@@ -264,8 +264,8 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
     if (flex_basis.has_value())
         computed_values.set_flex_basis(flex_basis.value());
 
-    computed_values.set_flex_grow_factor(specified_style.flex_grow_factor());
-    computed_values.set_flex_shrink_factor(specified_style.flex_shrink_factor());
+    computed_values.set_flex_grow(specified_style.flex_grow());
+    computed_values.set_flex_shrink(specified_style.flex_shrink());
 
     auto justify_content = specified_style.justify_content();
     if (justify_content.has_value())