ladybird/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.h
Andreas Kling 5af02a914c LibWeb: Let parent formatting context determine size of flex containers
Until now, we had implemented flex container sizing by awkwardly doing
exactly what the spec said (basically having FFC size the container)
despite that not really making sense in the big picture. (Parent
formatting contexts should be responsible for sizing and placing their
children)

This patch moves us away from the Flexbox spec text a little bit, by
removing the logic for sizing the flex container in FFC, and instead
making sure that all formatting contexts can set both width and height
of flex container children.

This required changes in BFC and IFC, but it's actually quite simple!
Width was already not a problem, and it turns out height isn't either,
since the automatic height of a flex container is max-content.
With this in mind, we can simply determine the height of flex containers
before transferring control to FFC, and everything flows nicely.

With this change, we can remove all the virtuals and FFC logic for
negotiating container size with the parent formatting context.
We also don't need the "available space for flex container" stuff
anymore either, so that's gone as well.

There are some minor diffs in layout test results from this, but the
new results actually match other browsers more closely, so that's fine.

This should make flex layout, and indeed layout in general, easier to
understand, since this was the main weird special case outside of
BFC/IFC where a formatting context delegates work to its parent instead
of the other way around. :^)
2024-01-10 16:28:12 +01:00

55 lines
1.7 KiB
C++

/*
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Layout/BlockContainer.h>
#include <LibWeb/Layout/FormattingContext.h>
namespace Web::Layout {
class InlineFormattingContext final : public FormattingContext {
public:
InlineFormattingContext(LayoutState&, BlockContainer const& containing_block, BlockFormattingContext& parent);
~InlineFormattingContext();
BlockFormattingContext& parent();
BlockFormattingContext const& parent() const;
BlockContainer const& containing_block() const { return static_cast<BlockContainer const&>(context_box()); }
virtual void run(Box const&, LayoutMode, AvailableSpace const&) override;
virtual CSSPixels automatic_content_height() const override;
virtual CSSPixels automatic_content_width() const override;
void dimension_box_on_line(Box const&, LayoutMode);
CSSPixels leftmost_x_offset_at(CSSPixels y) const;
AvailableSize available_space_for_line(CSSPixels y) const;
bool any_floats_intrude_at_y(CSSPixels y) const;
bool can_fit_new_line_at_y(CSSPixels y) const;
CSSPixels vertical_float_clearance() const;
void set_vertical_float_clearance(CSSPixels);
private:
void generate_line_boxes(LayoutMode);
void apply_justification_to_fragments(CSS::TextJustify, LineBox&, bool is_last_line);
LayoutState::UsedValues const& m_containing_block_state;
Optional<AvailableSpace> m_available_space;
CSSPixels m_automatic_content_width { 0 };
CSSPixels m_automatic_content_height { 0 };
CSSPixels m_vertical_float_clearance { 0 };
};
}