From 0fd25fcbbc0131b12ec05a85d0cdda8f7e152eb6 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 13 Oct 2021 22:07:55 +0200 Subject: LibWeb: Move FFC layout algorithm step 5 to a separate function --- .../LibWeb/Layout/FlexFormattingContext.cpp | 53 +++++++++++++--------- .../LibWeb/Layout/FlexFormattingContext.h | 3 ++ 2 files changed, 34 insertions(+), 22 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp index 3d156d0aa6..27ed4c3193 100644 --- a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp @@ -131,28 +131,7 @@ void FlexFormattingContext::run(Box& flex_container, LayoutMode) // 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! - Vector flex_lines; - // FIXME: Also support wrap-reverse - if (flex_container.computed_values().flex_wrap() == CSS::FlexWrap::Nowrap) { - FlexLine line; - for (auto& flex_item : flex_items) { - line.items.append(&flex_item); - } - flex_lines.append(line); - } else { - FlexLine line; - float line_main_size = 0; - for (auto& flex_item : flex_items) { - if ((line_main_size + flex_item.hypothetical_main_size) > main_available_size) { - flex_lines.append(line); - line = {}; - line_main_size = 0; - } - line.items.append(&flex_item); - line_main_size += flex_item.hypothetical_main_size; - } - flex_lines.append(line); - } + auto flex_lines = collect_flex_items_into_flex_lines(flex_container, flex_items, main_available_size); // 6. Resolve the flexible lengths https://www.w3.org/TR/css-flexbox-1/#resolve-flexible-lengths enum FlexFactor { @@ -921,4 +900,34 @@ void FlexFormattingContext::determine_main_size_of_flex_container(Box& flex_cont set_main_size(flex_container, main_available_size); } +// https://www.w3.org/TR/css-flexbox-1/#algo-line-break +Vector FlexFormattingContext::collect_flex_items_into_flex_lines(Box const& flex_container, Vector& flex_items, float main_available_size) +{ + Vector flex_lines; + + // FIXME: Also support wrap-reverse + if (flex_container.computed_values().flex_wrap() == CSS::FlexWrap::Nowrap) { + FlexLine line; + for (auto& flex_item : flex_items) { + line.items.append(&flex_item); + } + flex_lines.append(line); + } else { + FlexLine line; + float line_main_size = 0; + for (auto& flex_item : flex_items) { + if ((line_main_size + flex_item.hypothetical_main_size) > main_available_size) { + flex_lines.append(line); + line = {}; + line_main_size = 0; + } + line.items.append(&flex_item); + line_main_size += flex_item.hypothetical_main_size; + } + flex_lines.append(line); + } + + return flex_lines; +} + } diff --git a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h index c8095223fd..d8e70d42df 100644 --- a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h @@ -11,6 +11,7 @@ namespace Web::Layout { struct FlexItem; +struct FlexLine; class FlexFormattingContext final : public FormattingContext { public: @@ -61,6 +62,8 @@ private: void determine_main_size_of_flex_container(Box& flex_container, Vector&, bool main_is_constrained, bool main_size_is_infinite, float& main_available_size, float main_min_size, float main_max_size); + Vector collect_flex_items_into_flex_lines(Box const& flex_container, Vector&, float main_available_size); + bool is_row_layout() const { return m_flex_direction == CSS::FlexDirection::Row || m_flex_direction == CSS::FlexDirection::RowReverse; } CSS::FlexDirection m_flex_direction {}; -- cgit v1.2.3