summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-10-13 22:03:39 +0200
committerAndreas Kling <kling@serenityos.org>2021-10-13 23:56:26 +0200
commitfa7bbc602de6a61a55d3c82807fe389046437e1e (patch)
tree16be896aa87fbb305124b7a5abd22866f71cf55b
parent3375953918ac63767fd4495b757ab39ec670633b (diff)
downloadserenity-fa7bbc602de6a61a55d3c82807fe389046437e1e.zip
LibWeb: Move FFC layout algorithm step 4 to a separate function
-rw-r--r--Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp76
-rw-r--r--Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h2
2 files changed, 43 insertions, 35 deletions
diff --git a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp
index 3b5f7d52b6..3d156d0aa6 100644
--- a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp
+++ b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp
@@ -127,41 +127,7 @@ void FlexFormattingContext::run(Box& flex_container, LayoutMode)
}
// 4. Determine the main size of the flex container
- if ((!main_is_constrained && main_size_is_infinite) || main_available_size == 0) {
- // Uses https://www.w3.org/TR/css-flexbox-1/#intrinsic-main-sizes
- // 9.9.1
- // 1.
- float largest_max_content_flex_fraction = 0;
- for (auto& flex_item : flex_items) {
- // FIXME: This needs some serious work.
- 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);
- } else {
- max_content_flex_fraction /= max(flex_item.box.computed_values().flex_shrink_factor().value_or(1), 1.0f) * flex_item.flex_base_size;
- }
- flex_item.max_content_flex_fraction = max_content_flex_fraction;
-
- if (max_content_flex_fraction > largest_max_content_flex_fraction)
- largest_max_content_flex_fraction = max_content_flex_fraction;
- }
-
- // 2. Omitted
- // 3.
- float result = 0;
- for (auto& flex_item : 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);
- } 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;
- }
- result += flex_item.flex_base_size + product;
- }
- main_available_size = clamp(result, main_min_size, main_max_size);
- }
- set_main_size(flex_container, main_available_size);
+ determine_main_size_of_flex_container(flex_container, flex_items, main_is_constrained, main_size_is_infinite, main_available_size, main_min_size, main_max_size);
// 5. Collect flex items into flex lines:
// After this step no additional items are to be added to flex_lines or any of its items!
@@ -915,4 +881,44 @@ void FlexFormattingContext::determine_flex_base_size_and_hypothetical_main_size(
flex_item.hypothetical_main_size = clamp(flex_item.flex_base_size, clamp_min, clamp_max);
}
+// https://www.w3.org/TR/css-flexbox-1/#algo-main-container
+void FlexFormattingContext::determine_main_size_of_flex_container(Box& flex_container, Vector<FlexItem>& flex_items, bool const main_is_constrained, bool const main_size_is_infinite, float& main_available_size, float const main_min_size, float const main_max_size)
+{
+ if ((!main_is_constrained && main_size_is_infinite) || main_available_size == 0) {
+ // Uses https://www.w3.org/TR/css-flexbox-1/#intrinsic-main-sizes
+ // 9.9.1
+ // 1.
+ float largest_max_content_flex_fraction = 0;
+ for (auto& flex_item : flex_items) {
+ // FIXME: This needs some serious work.
+ 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);
+ } else {
+ max_content_flex_fraction /= max(flex_item.box.computed_values().flex_shrink_factor().value_or(1), 1.0f) * flex_item.flex_base_size;
+ }
+ flex_item.max_content_flex_fraction = max_content_flex_fraction;
+
+ if (max_content_flex_fraction > largest_max_content_flex_fraction)
+ largest_max_content_flex_fraction = max_content_flex_fraction;
+ }
+
+ // 2. Omitted
+ // 3.
+ float result = 0;
+ for (auto& flex_item : 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);
+ } 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;
+ }
+ result += flex_item.flex_base_size + product;
+ }
+ main_available_size = clamp(result, main_min_size, main_max_size);
+ }
+ set_main_size(flex_container, main_available_size);
+}
+
}
diff --git a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h
index ff6c1ac76e..c8095223fd 100644
--- a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h
+++ b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h
@@ -59,6 +59,8 @@ private:
float layout_for_maximum_main_size(Box&);
void determine_flex_base_size_and_hypothetical_main_size(Box const& flex_container, FlexItem&);
+ void determine_main_size_of_flex_container(Box& flex_container, Vector<FlexItem>&, bool main_is_constrained, bool main_size_is_infinite, float& main_available_size, float main_min_size, float main_max_size);
+
bool is_row_layout() const { return m_flex_direction == CSS::FlexDirection::Row || m_flex_direction == CSS::FlexDirection::RowReverse; }
CSS::FlexDirection m_flex_direction {};