diff options
author | Andreas Kling <kling@serenityos.org> | 2023-03-19 09:57:31 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-20 17:57:58 +0100 |
commit | 00999a245cfa3d4c3c9c8cdba851f37214e8200e (patch) | |
tree | b27116ff1097d93ec24b88ac352a0a247c5edca0 | |
parent | e945994877cc8a62b086d77be3b779e564857738 (diff) | |
download | serenity-00999a245cfa3d4c3c9c8cdba851f37214e8200e.zip |
LibWeb: Let FormattingContext decide the automatic width of its root
Instead of special-casing FlexFormattingContext in the intrinsic sizing
layout helpers, add FormattingContext::automatic_content_width() and let
each context subclass decide what that means.
No behavior change here, just moving this responsibility.
12 files changed, 42 insertions, 18 deletions
diff --git a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp index 80d370902a..1ae86a278f 100644 --- a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp @@ -38,6 +38,11 @@ BlockFormattingContext::~BlockFormattingContext() } } +CSSPixels BlockFormattingContext::automatic_content_width() const +{ + return greatest_child_width(root()); +} + CSSPixels BlockFormattingContext::automatic_content_height() const { return compute_auto_height_for_block_formatting_context_root(root()); @@ -925,7 +930,7 @@ BlockFormattingContext::SpaceUsedByFloats BlockFormattingContext::space_used_by_ return space_used_by_floats; } -CSSPixels BlockFormattingContext::greatest_child_width(Box const& box) +CSSPixels BlockFormattingContext::greatest_child_width(Box const& box) const { // Similar to FormattingContext::greatest_child_width() // but this one takes floats into account! diff --git a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.h b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.h index d47dde1868..5d9d9aad6c 100644 --- a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.h @@ -22,6 +22,7 @@ public: ~BlockFormattingContext(); virtual void run(Box const&, LayoutMode, AvailableSpace const&) override; + virtual CSSPixels automatic_content_width() const override; virtual CSSPixels automatic_content_height() const override; auto const& left_side_floats() const { return m_left_floats; } @@ -40,7 +41,7 @@ public: SpaceUsedByFloats space_used_by_floats(CSSPixels y) const; - virtual CSSPixels greatest_child_width(Box const&) override; + virtual CSSPixels greatest_child_width(Box const&) const override; void layout_floating_box(Box const& child, BlockContainer const& containing_block, LayoutMode, AvailableSpace const&, CSSPixels y, LineBuilder* = nullptr); diff --git a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp index e04527a688..3ccbd1213f 100644 --- a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp @@ -75,9 +75,14 @@ FlexFormattingContext::FlexFormattingContext(LayoutState& state, Box const& flex FlexFormattingContext::~FlexFormattingContext() = default; +CSSPixels FlexFormattingContext::automatic_content_width() const +{ + return m_flex_container_state.content_width(); +} + CSSPixels FlexFormattingContext::automatic_content_height() const { - return m_state.get(flex_container()).content_height(); + return m_flex_container_state.content_height(); } void FlexFormattingContext::run(Box const& run_box, LayoutMode, AvailableSpace const& available_content_space) diff --git a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h index 07716c18d5..806ff96998 100644 --- a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.h @@ -19,6 +19,7 @@ public: virtual bool inhibits_floating() const override { return true; } virtual void run(Box const&, LayoutMode, AvailableSpace const&) override; + virtual CSSPixels automatic_content_width() const override; virtual CSSPixels automatic_content_height() const override; Box const& flex_container() const { return context_box(); } diff --git a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp index aad5d9c39f..727973afd2 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp @@ -127,7 +127,8 @@ OwnPtr<FormattingContext> FormattingContext::create_independent_formatting_conte : FormattingContext(Type::Block, state, box) { } - virtual CSSPixels automatic_content_height() const override { return 0; }; + virtual CSSPixels automatic_content_width() const override { return 0; } + virtual CSSPixels automatic_content_height() const override { return 0; } virtual void run(Box const&, LayoutMode, AvailableSpace const&) override { } }; return make<ReplacedFormattingContext>(state, child_box); @@ -170,7 +171,8 @@ OwnPtr<FormattingContext> FormattingContext::create_independent_formatting_conte : FormattingContext(Type::Block, state, box) { } - virtual CSSPixels automatic_content_height() const override { return 0; }; + virtual CSSPixels automatic_content_width() const override { return 0; } + virtual CSSPixels automatic_content_height() const override { return 0; } virtual void run(Box const&, LayoutMode, AvailableSpace const&) override { } }; return make<DummyFormattingContext>(state, child_box); @@ -208,7 +210,7 @@ OwnPtr<FormattingContext> FormattingContext::layout_inside(Box const& child_box, return independent_formatting_context; } -CSSPixels FormattingContext::greatest_child_width(Box const& box) +CSSPixels FormattingContext::greatest_child_width(Box const& box) const { CSSPixels max_width = 0; if (box.children_are_inline()) { @@ -1093,11 +1095,7 @@ CSSPixels FormattingContext::calculate_min_content_width(Layout::Box const& box) auto available_height = AvailableSize::make_indefinite(); context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, available_height)); - if (context->type() == FormattingContext::Type::Flex) { - cache.min_content_width = box_state.content_width(); - } else { - cache.min_content_width = context->greatest_child_width(box).value(); - } + cache.min_content_width = context->automatic_content_width(); if (!isfinite(cache.min_content_width->value())) { // HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine. @@ -1131,11 +1129,7 @@ CSSPixels FormattingContext::calculate_max_content_width(Layout::Box const& box) auto available_height = AvailableSize::make_indefinite(); context->run(box, LayoutMode::IntrinsicSizing, AvailableSpace(available_width, available_height)); - if (context->type() == FormattingContext::Type::Flex) { - cache.max_content_width = box_state.content_width(); - } else { - cache.max_content_width = context->greatest_child_width(box).value(); - } + cache.max_content_width = context->automatic_content_width(); if (!isfinite(cache.max_content_width->value())) { // HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine. diff --git a/Userland/Libraries/LibWeb/Layout/FormattingContext.h b/Userland/Libraries/LibWeb/Layout/FormattingContext.h index 3150f0b9b2..378d99f72e 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/FormattingContext.h @@ -29,7 +29,7 @@ public: virtual void run(Box const&, LayoutMode, AvailableSpace const&) = 0; // This function returns the automatic content height of the context's root box. - virtual CSSPixels automatic_content_width() const { return 0; } + virtual CSSPixels automatic_content_width() const = 0; // This function returns the automatic content height of the context's root box. virtual CSSPixels automatic_content_height() const = 0; @@ -64,7 +64,7 @@ public: CSS::Length calculate_inner_width(Layout::Box const&, AvailableSize const&, CSS::Size const& width) const; CSS::Length calculate_inner_height(Layout::Box const&, AvailableSize const&, CSS::Size const& height) const; - virtual CSSPixels greatest_child_width(Box const&); + virtual CSSPixels greatest_child_width(Box const&) const; CSSPixels containing_block_width_for(Box const& box) const { return containing_block_width_for(box, m_state); } CSSPixels containing_block_height_for(Box const& box) const { return containing_block_height_for(box, m_state); } diff --git a/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp index f67e69be96..f6cf4a5c40 100644 --- a/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp @@ -1901,6 +1901,11 @@ void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const m_automatic_content_height = total_y; } +CSSPixels GridFormattingContext::automatic_content_width() const +{ + return greatest_child_width(context_box()); +} + CSSPixels GridFormattingContext::automatic_content_height() const { return m_automatic_content_height; diff --git a/Userland/Libraries/LibWeb/Layout/GridFormattingContext.h b/Userland/Libraries/LibWeb/Layout/GridFormattingContext.h index 61e0881d29..e4697fc43e 100644 --- a/Userland/Libraries/LibWeb/Layout/GridFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/GridFormattingContext.h @@ -62,6 +62,7 @@ public: ~GridFormattingContext(); virtual void run(Box const&, LayoutMode, AvailableSpace const& available_space) override; + virtual CSSPixels automatic_content_width() const override; virtual CSSPixels automatic_content_height() const override; private: diff --git a/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp index 95a3276bd7..4219f449dc 100644 --- a/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp @@ -21,6 +21,11 @@ SVGFormattingContext::SVGFormattingContext(LayoutState& state, Box const& box, F SVGFormattingContext::~SVGFormattingContext() = default; +CSSPixels SVGFormattingContext::automatic_content_width() const +{ + return 0; +} + CSSPixels SVGFormattingContext::automatic_content_height() const { return 0; diff --git a/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.h b/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.h index 95bfc02798..654591c6d5 100644 --- a/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.h @@ -17,6 +17,7 @@ public: ~SVGFormattingContext(); virtual void run(Box const&, LayoutMode, AvailableSpace const&) override; + virtual CSSPixels automatic_content_width() const override; virtual CSSPixels automatic_content_height() const override; }; diff --git a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp index 7001d2fa2c..1f75270c54 100644 --- a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp @@ -542,6 +542,11 @@ void TableFormattingContext::run(Box const& box, LayoutMode layout_mode, Availab m_automatic_content_height = total_content_height; } +CSSPixels TableFormattingContext::automatic_content_width() const +{ + return greatest_child_width(context_box()); +} + CSSPixels TableFormattingContext::automatic_content_height() const { return m_automatic_content_height; diff --git a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h index feb5bbb3f6..6de167e692 100644 --- a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.h @@ -19,6 +19,7 @@ public: ~TableFormattingContext(); virtual void run(Box const&, LayoutMode, AvailableSpace const&) override; + virtual CSSPixels automatic_content_width() const override; virtual CSSPixels automatic_content_height() const override; TableBox const& table_box() const { return static_cast<TableBox const&>(context_box()); } |