LibWeb: Remember the selection state of each LayoutNode

Instead of computing it on the fly while painting each layout node,
they now remember their selection state. This avoids a whole bunch
of tree traversal while painting with anything selected.
This commit is contained in:
Andreas Kling 2020-08-21 17:50:41 +02:00
parent cf4870c93e
commit d47f77169f
Notes: sideshowbarker 2024-07-19 03:21:04 +09:00
6 changed files with 60 additions and 15 deletions

View file

@ -120,4 +120,30 @@ HitTestResult LayoutDocument::hit_test(const Gfx::IntPoint& position, HitTestTyp
return stacking_context()->hit_test(position, type);
}
void LayoutDocument::recompute_selection_states()
{
SelectionState state = SelectionState::None;
auto selection = this->selection().normalized();
for_each_in_subtree([&](auto& layout_node) {
if (!selection.is_valid()) {
// Everything gets SelectionState::None.
} else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
state = SelectionState::StartAndEnd;
} else if (&layout_node == selection.start().layout_node) {
state = SelectionState::Start;
} else if (&layout_node == selection.end().layout_node) {
state = SelectionState::End;
} else {
if (state == SelectionState::Start)
state = SelectionState::Full;
else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
state = SelectionState::None;
}
layout_node.set_selection_state(state);
return IterationDecision::Continue;
});
}
}