Skip to content

Commit 0ea438e

Browse files
committed
LibWeb: Put BFC floating object state into a struct
This patch adds a BFC::FloatSideData struct so we can contain left and right floating object layout state in a struct. This is preparation for adding more per-side state.
1 parent 70a56d2 commit 0ea438e

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -515,22 +515,22 @@ void BlockFormattingContext::place_block_level_non_replaced_element_in_normal_fl
515515
}
516516
}
517517

518-
auto clear_floating_boxes = [&](auto& floating_boxes) {
519-
if (!floating_boxes.is_empty()) {
518+
auto clear_floating_boxes = [&](auto& float_side) {
519+
if (!float_side.boxes.is_empty()) {
520520
float clearance_y = 0;
521-
for (auto* floating_box : floating_boxes) {
522-
clearance_y = max(clearance_y, floating_box->effective_offset().y() + floating_box->border_box_height());
521+
for (auto& floating_box : float_side.boxes) {
522+
clearance_y = max(clearance_y, floating_box.effective_offset().y() + floating_box.border_box_height());
523523
}
524524
y = max(y, clearance_y);
525-
floating_boxes.clear();
525+
float_side.boxes.clear();
526526
}
527527
};
528528

529529
// Flex-items don't float and also don't clear.
530530
if ((computed_values.clear() == CSS::Clear::Left || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
531-
clear_floating_boxes(m_left_floating_boxes);
531+
clear_floating_boxes(m_left_floats);
532532
if ((computed_values.clear() == CSS::Clear::Right || computed_values.clear() == CSS::Clear::Both) && !child_box.is_flex_item())
533-
clear_floating_boxes(m_right_floating_boxes);
533+
clear_floating_boxes(m_right_floats);
534534

535535
child_box.set_offset(x, y);
536536
}
@@ -601,8 +601,8 @@ void BlockFormattingContext::layout_floating_child(Box& box, BlockContainer cons
601601

602602
// Next, float to the left and/or right
603603
if (box.computed_values().float_() == CSS::Float::Left) {
604-
if (!m_left_floating_boxes.is_empty()) {
605-
auto& previous_floating_box = *m_left_floating_boxes.last();
604+
if (!m_left_floats.boxes.is_empty()) {
605+
auto& previous_floating_box = m_left_floats.boxes.last();
606606
auto previous_rect = rect_in_coordinate_space(previous_floating_box, root());
607607
if (previous_rect.contains_vertically(y_in_root)) {
608608
// This box touches another already floating box. Stack to the right.
@@ -611,16 +611,16 @@ void BlockFormattingContext::layout_floating_child(Box& box, BlockContainer cons
611611
// This box does not touch another floating box, go all the way to the left.
612612
x = box.box_model().margin_box().left;
613613
// Also, forget all previous left-floating boxes while we're here since they're no longer relevant.
614-
m_left_floating_boxes.clear();
614+
m_left_floats.boxes.clear();
615615
}
616616
} else {
617617
// This is the first left-floating box. Go all the way to the left.
618618
x = box.box_model().margin_box().left;
619619
}
620-
m_left_floating_boxes.append(&box);
620+
m_left_floats.boxes.append(box);
621621
} else if (box.computed_values().float_() == CSS::Float::Right) {
622-
if (!m_right_floating_boxes.is_empty()) {
623-
auto& previous_floating_box = *m_right_floating_boxes.last();
622+
if (!m_right_floats.boxes.is_empty()) {
623+
auto& previous_floating_box = m_right_floats.boxes.last();
624624
auto previous_rect = rect_in_coordinate_space(previous_floating_box, root());
625625
if (previous_rect.contains_vertically(y_in_root)) {
626626
// This box touches another already floating box. Stack to the left.
@@ -629,13 +629,13 @@ void BlockFormattingContext::layout_floating_child(Box& box, BlockContainer cons
629629
// This box does not touch another floating box, go all the way to the right.
630630
x = containing_block.width() - box.box_model().margin_box().right - box.width();
631631
// Also, forget all previous right-floating boxes while we're here since they're no longer relevant.
632-
m_right_floating_boxes.clear();
632+
m_right_floats.boxes.clear();
633633
}
634634
} else {
635635
// This is the first right-floating box. Go all the way to the right.
636636
x = containing_block.width() - box.box_model().margin_box().right - box.width();
637637
}
638-
m_right_floating_boxes.append(&box);
638+
m_right_floats.boxes.append(box);
639639
}
640640

641641
box.set_offset(x, box.effective_offset().y());

Userland/Libraries/LibWeb/Layout/BlockFormattingContext.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class BlockFormattingContext : public FormattingContext {
2323

2424
bool is_initial() const;
2525

26-
const Vector<Box*>& left_floating_boxes() const { return m_left_floating_boxes; }
27-
const Vector<Box*>& right_floating_boxes() const { return m_right_floating_boxes; }
26+
auto const& left_side_floats() const { return m_left_floats; }
27+
auto const& right_side_floats() const { return m_right_floats; }
2828

2929
static float compute_theoretical_height(Box const&);
3030
void compute_width(Box&);
@@ -50,14 +50,18 @@ class BlockFormattingContext : public FormattingContext {
5050
void layout_inline_children(BlockContainer&, LayoutMode);
5151

5252
void place_block_level_replaced_element_in_normal_flow(Box& child, BlockContainer const&);
53-
void place_block_level_non_replaced_element_in_normal_flow(Box& child, BlockContainer const&);
53+
void place_block_level_non_replaced_element_in_normal_flow(Box& float_side, BlockContainer const&);
5454

5555
void layout_floating_child(Box& child, BlockContainer const& containing_block);
5656

5757
void apply_transformations_to_children(Box&);
5858

59-
Vector<Box*> m_left_floating_boxes;
60-
Vector<Box*> m_right_floating_boxes;
59+
struct FloatSideData {
60+
Vector<Box&> boxes;
61+
};
62+
63+
FloatSideData m_left_floats;
64+
FloatSideData m_right_floats;
6165
};
6266

6367
}

Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ InlineFormattingContext::AvailableSpaceForLineInfo InlineFormattingContext::avai
3535

3636
auto const& bfc = static_cast<BlockFormattingContext const&>(*parent());
3737

38-
for (ssize_t i = bfc.left_floating_boxes().size() - 1; i >= 0; --i) {
39-
auto& floating_box = *bfc.left_floating_boxes().at(i);
38+
for (ssize_t i = bfc.left_side_floats().boxes.size() - 1; i >= 0; --i) {
39+
auto const& floating_box = bfc.left_side_floats().boxes.at(i);
4040
auto rect = floating_box.margin_box_as_relative_rect();
4141
if (rect.contains_vertically(y)) {
4242
info.left = rect.right() + 1;
@@ -46,8 +46,8 @@ InlineFormattingContext::AvailableSpaceForLineInfo InlineFormattingContext::avai
4646

4747
info.right = containing_block().width();
4848

49-
for (ssize_t i = bfc.right_floating_boxes().size() - 1; i >= 0; --i) {
50-
auto& floating_box = *bfc.right_floating_boxes().at(i);
49+
for (ssize_t i = bfc.right_side_floats().boxes.size() - 1; i >= 0; --i) {
50+
auto const& floating_box = bfc.right_side_floats().boxes.at(i);
5151
auto rect = floating_box.margin_box_as_relative_rect();
5252
if (rect.contains_vertically(y)) {
5353
info.right = rect.left() - 1;

0 commit comments

Comments
 (0)