1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:17:44 +00:00

LibWeb: Use the StackingContext tree for hit testing

This makes it possible to click links that are above other content due
to stacking context order (e.g via CSS z-index.)
This commit is contained in:
Andreas Kling 2020-07-01 19:02:28 +02:00
parent f7a900367f
commit 392b055806
8 changed files with 36 additions and 1 deletions

View file

@ -725,6 +725,8 @@ HitTestResult LayoutBlock::hit_test(const Gfx::IntPoint& position) const
HitTestResult last_good_candidate;
for (auto& line_box : m_line_boxes) {
for (auto& fragment : line_box.fragments()) {
if (is<LayoutBox>(fragment.layout_node()) && to<LayoutBox>(fragment.layout_node()).stacking_context())
continue;
if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
if (fragment.layout_node().is_block())
return to<LayoutBlock>(fragment.layout_node()).hit_test(position);

View file

@ -231,6 +231,8 @@ HitTestResult LayoutBox::hit_test(const Gfx::IntPoint& position) const
// m_rect.contains() since inline text rects can't be trusted..
HitTestResult result { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
for_each_child([&](auto& child) {
if (is<LayoutBox>(child) && to<LayoutBox>(child).stacking_context())
return;
auto child_result = child.hit_test(position);
if (child_result.layout_node)
result = child_result;

View file

@ -64,6 +64,7 @@ public:
bool establishes_stacking_context() const;
StackingContext* stacking_context() { return m_stacking_context; }
const StackingContext* stacking_context() const { return m_stacking_context; }
void set_stacking_context(NonnullOwnPtr<StackingContext> context) { m_stacking_context = move(context); }
StackingContext* enclosing_stacking_context();

View file

@ -113,4 +113,9 @@ void LayoutDocument::paint(PaintContext& context, PaintPhase phase)
stacking_context()->paint(context, phase);
}
HitTestResult LayoutDocument::hit_test(const Gfx::IntPoint& position) const
{
return stacking_context()->hit_test(position);
}
}

View file

@ -41,9 +41,10 @@ public:
virtual void layout(LayoutMode = LayoutMode::Default) override;
void paint_all_phases(PaintContext&);
virtual void paint(PaintContext&, PaintPhase) override;
virtual HitTestResult hit_test(const Gfx::IntPoint&) const override;
const LayoutRange& selection() const { return m_selection; }
LayoutRange& selection() { return m_selection; }

View file

@ -106,6 +106,10 @@ HitTestResult LayoutNode::hit_test(const Gfx::IntPoint& position) const
{
HitTestResult result;
for_each_child([&](auto& child) {
// Skip over children that establish their own stacking context.
// The outer loop who called us will take care of those.
if (is<LayoutBox>(child) && to<LayoutBox>(child).stacking_context())
return;
auto child_result = child.hit_test(position);
if (child_result.layout_node)
result = child_result;