1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:57:44 +00:00

LibWeb: Make the paint tree a proper standalone tree

Until now, paint trees have been piggybacking on the layout tree for
traversal, and paintables didn't actually have their own parent/child
pointers.

This patch changes that by making Paintable inherit from TreeNode, and
adding a new pass to LayoutState::commit() where we recursively build
the new paint tree.
This commit is contained in:
Andreas Kling 2023-08-18 12:30:27 +02:00
parent 4d4dbacfc3
commit 216bd513fa
5 changed files with 33 additions and 46 deletions

View file

@ -196,7 +196,24 @@ void LayoutState::resolve_relative_positions(Vector<Painting::PaintableWithLines
}
}
void LayoutState::commit()
static void build_paint_tree(Node& node, Painting::Paintable* parent_paintable = nullptr)
{
if (!node.paintable())
return;
auto& paintable = const_cast<Painting::Paintable&>(*node.paintable());
if (parent_paintable) {
// In case this was a relayout of an existing tree, we need to remove the paintable from its old parent first.
if (auto* old_parent = paintable.parent()) {
old_parent->remove_child(paintable);
}
parent_paintable->append_child(paintable);
}
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
build_paint_tree(*child, &paintable);
}
}
void LayoutState::commit(Box& root)
{
// Only the top-level LayoutState should ever be committed.
VERIFY(!m_parent);
@ -215,12 +232,14 @@ void LayoutState::commit()
node.box_model().border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
node.box_model().margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
node.set_paintable(node.create_paintable());
auto paintable = node.create_paintable();
node.set_paintable(paintable);
// For boxes, transfer all the state needed for painting.
if (is<Layout::Box>(node)) {
auto& box = static_cast<Layout::Box const&>(node);
auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
paintable_box.set_offset(used_values.offset);
paintable_box.set_content_size(used_values.content_width(), used_values.content_height());
if (used_values.override_borders_data().has_value()) {
@ -255,6 +274,11 @@ void LayoutState::commit()
}
}
for (auto* text_node : text_nodes)
text_node->set_paintable(text_node->create_paintable());
build_paint_tree(root);
// Measure overflow in scroll containers.
for (auto& it : used_values_per_layout_node) {
auto& used_values = *it.value;
@ -263,9 +287,6 @@ void LayoutState::commit()
auto const& box = static_cast<Layout::Box const&>(used_values.node());
measure_scrollable_overflow(box);
}
for (auto* text_node : text_nodes)
text_node->set_paintable(text_node->create_paintable());
}
void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)