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

LibWeb: Make hit testing functions return Optional<HitTestResult>

Using "HitTestResult with null paintable" as a way to signal misses was
unnecessarily confusing. Let's use Optional instead. :^)
This commit is contained in:
Andreas Kling 2022-03-21 11:11:05 +01:00
parent 8c88ee1165
commit 0ba785894c
7 changed files with 50 additions and 44 deletions

View file

@ -273,7 +273,7 @@ void StackingContext::paint(PaintContext& context) const
}
}
HitTestResult StackingContext::hit_test(Gfx::FloatPoint const& position, HitTestType type) const
Optional<HitTestResult> StackingContext::hit_test(Gfx::FloatPoint const& position, HitTestType type) const
{
// FIXME: Use the transform origin specified in CSS or SVG
auto transform_origin = m_box.paint_box()->absolute_position();
@ -289,27 +289,27 @@ HitTestResult StackingContext::hit_test(Gfx::FloatPoint const& position, HitTest
if (child.m_box.computed_values().z_index().value_or(0) < 0)
break;
auto result = child.hit_test(transformed_position, type);
if (result.paintable)
if (result.has_value())
return result;
}
HitTestResult result;
Optional<HitTestResult> result;
// 6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
if (box.is_positioned() && !box.paint_box()->stacking_context()) {
result = box.paint_box()->hit_test(transformed_position, type);
if (result.paintable)
if (result.has_value())
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
if (result.paintable)
if (result.has_value())
return result;
// 5. the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
if (m_box.children_are_inline() && is<Layout::BlockContainer>(m_box)) {
auto result = m_box.paint_box()->hit_test(transformed_position, type);
if (result.paintable)
if (result.has_value())
return result;
}
@ -317,23 +317,25 @@ HitTestResult StackingContext::hit_test(Gfx::FloatPoint const& position, HitTest
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
if (box.is_floating()) {
result = box.paint_box()->hit_test(transformed_position, type);
if (result.paintable)
if (result.has_value())
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
if (result.has_value())
return result;
// 3. the in-flow, non-inline-level, non-positioned descendants.
if (!m_box.children_are_inline()) {
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
if (!box.is_absolutely_positioned() && !box.is_floating()) {
result = box.paint_box()->hit_test(transformed_position, type);
if (result.paintable)
if (result.has_value())
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
if (result.paintable)
if (result.has_value())
return result;
}
@ -343,14 +345,14 @@ HitTestResult StackingContext::hit_test(Gfx::FloatPoint const& position, HitTest
if (child.m_box.computed_values().z_index().value_or(0) < 0)
break;
auto result = child.hit_test(transformed_position, type);
if (result.paintable)
if (result.has_value())
return result;
}
// 1. the background and borders of the element forming the stacking context.
if (m_box.paint_box()->absolute_border_box_rect().contains(transformed_position)) {
return HitTestResult {
.paintable = m_box.paintable(),
.paintable = *m_box.paintable(),
};
}