1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:27:45 +00:00

LibWeb: Make hit testing return a { paintable, offset }

Everything related to hit testing is better off using the painting tree.
The thing being mousemoved over is a paintable, so let's hand that out
directly instead of the corresponding layout node.
This commit is contained in:
Andreas Kling 2022-03-10 22:58:19 +01:00
parent cb0c5390ff
commit f017c1c064
5 changed files with 46 additions and 49 deletions

View file

@ -42,16 +42,16 @@ HitTestResult BlockContainer::hit_test(const Gfx::IntPoint& position, HitTestTyp
if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) { if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
if (is<BlockContainer>(fragment.layout_node())) if (is<BlockContainer>(fragment.layout_node()))
return verify_cast<BlockContainer>(fragment.layout_node()).hit_test(position, type); return verify_cast<BlockContainer>(fragment.layout_node()).hit_test(position, type);
return { fragment.layout_node(), fragment.text_index_at(position.x()) }; return { fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
} }
if (fragment.absolute_rect().top() <= position.y()) if (fragment.absolute_rect().top() <= position.y())
last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) }; last_good_candidate = { fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
} }
} }
if (type == HitTestType::TextCursor && last_good_candidate.layout_node) if (type == HitTestType::TextCursor && last_good_candidate.paintable)
return last_good_candidate; return last_good_candidate;
return { paint_box()->absolute_border_box_rect().contains(position.x(), position.y()) ? this : nullptr }; return { paint_box()->absolute_border_box_rect().contains(position.x(), position.y()) ? paintable() : nullptr };
} }
bool BlockContainer::is_scrollable() const bool BlockContainer::is_scrollable() const

View file

@ -62,10 +62,10 @@ HitTestResult Box::hit_test(const Gfx::IntPoint& position, HitTestType type) con
// FIXME: It would be nice if we could confidently skip over hit testing // FIXME: It would be nice if we could confidently skip over hit testing
// parts of the layout tree, but currently we can't just check // parts of the layout tree, but currently we can't just check
// m_rect.contains() since inline text rects can't be trusted.. // m_rect.contains() since inline text rects can't be trusted..
HitTestResult result { paint_box()->absolute_border_box_rect().contains(position.x(), position.y()) ? this : nullptr }; HitTestResult result { paint_box()->absolute_border_box_rect().contains(position.x(), position.y()) ? paint_box() : nullptr };
for_each_child_in_paint_order([&](auto& child) { for_each_child_in_paint_order([&](auto& child) {
auto child_result = child.hit_test(position, type); auto child_result = child.hit_test(position, type);
if (child_result.layout_node) if (child_result.paintable)
result = child_result; result = child_result;
}); });
return result; return result;

View file

@ -27,7 +27,7 @@ enum class LayoutMode {
}; };
struct HitTestResult { struct HitTestResult {
RefPtr<Node> layout_node; RefPtr<Painting::Paintable> paintable;
int index_in_node { 0 }; int index_in_node { 0 };
enum InternalPosition { enum InternalPosition {

View file

@ -124,11 +124,8 @@ bool EventHandler::handle_mousewheel(const Gfx::IntPoint& position, unsigned int
// FIXME: Support wheel events in nested browsing contexts. // FIXME: Support wheel events in nested browsing contexts.
auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact); auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
if (result.layout_node if (result.paintable && result.paintable->handle_mousewheel({}, position, buttons, modifiers, wheel_delta_x, wheel_delta_y))
&& result.layout_node->paintable()
&& result.layout_node->paintable()->handle_mousewheel({}, position, buttons, modifiers, wheel_delta_x, wheel_delta_y)) {
return true; return true;
}
if (auto* page = m_browsing_context.page()) { if (auto* page = m_browsing_context.page()) {
page->client().page_did_request_scroll(wheel_delta_x * 20, wheel_delta_y * 20); page->client().page_did_request_scroll(wheel_delta_x * 20, wheel_delta_y * 20);
@ -152,8 +149,8 @@ bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button
auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact); auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
if (result.layout_node && result.layout_node->paintable() && result.layout_node->paintable()->wants_mouse_events()) { if (result.paintable && result.paintable->wants_mouse_events()) {
result.layout_node->paintable()->handle_mouseup({}, position, button, modifiers); result.paintable->handle_mouseup({}, position, button, modifiers);
// Things may have changed as a consequence of Layout::Node::handle_mouseup(). Hit test again. // Things may have changed as a consequence of Layout::Node::handle_mouseup(). Hit test again.
if (!layout_root()) if (!layout_root())
@ -161,14 +158,14 @@ bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button
result = layout_root()->hit_test(position, Layout::HitTestType::Exact); result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
} }
if (result.layout_node && result.layout_node->dom_node()) { if (result.paintable && result.paintable->layout_node().dom_node()) {
RefPtr<DOM::Node> node = result.layout_node->dom_node(); RefPtr<DOM::Node> node = result.paintable->layout_node().dom_node();
if (is<HTML::HTMLIFrameElement>(*node)) { if (is<HTML::HTMLIFrameElement>(*node)) {
if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context()) if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
return nested_browsing_context->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers); return nested_browsing_context->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, result.paintable->layout_node())), button, modifiers);
return false; return false;
} }
auto offset = compute_mouse_event_offset(position, *result.layout_node); auto offset = compute_mouse_event_offset(position, result.paintable->layout_node());
node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y())); node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y()));
handled_event = true; handled_event = true;
@ -198,19 +195,19 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
{ {
// TODO: Allow selecting element behind if one on top has pointer-events set to none. // TODO: Allow selecting element behind if one on top has pointer-events set to none.
auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact); auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
if (!result.layout_node) if (!result.paintable)
return false; return false;
auto pointer_events = result.layout_node->computed_values().pointer_events(); auto pointer_events = result.paintable->computed_values().pointer_events();
// FIXME: Handle other values for pointer-events. // FIXME: Handle other values for pointer-events.
if (pointer_events == CSS::PointerEvents::None) if (pointer_events == CSS::PointerEvents::None)
return false; return false;
node = result.layout_node->dom_node(); node = result.paintable->layout_node().dom_node();
document->set_hovered_node(node); document->set_hovered_node(node);
if (result.layout_node->paintable() && result.layout_node->paintable()->wants_mouse_events()) { if (result.paintable->wants_mouse_events()) {
result.layout_node->paintable()->handle_mousedown({}, position, button, modifiers); result.paintable->handle_mousedown({}, position, button, modifiers);
return true; return true;
} }
@ -219,14 +216,14 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
if (is<HTML::HTMLIFrameElement>(*node)) { if (is<HTML::HTMLIFrameElement>(*node)) {
if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context()) if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
return nested_browsing_context->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers); return nested_browsing_context->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, result.paintable->layout_node())), button, modifiers);
return false; return false;
} }
if (auto* page = m_browsing_context.page()) if (auto* page = m_browsing_context.page())
page->set_focused_browsing_context({}, m_browsing_context); page->set_focused_browsing_context({}, m_browsing_context);
auto offset = compute_mouse_event_offset(position, *result.layout_node); auto offset = compute_mouse_event_offset(position, result.paintable->layout_node());
m_mousedown_target = node; m_mousedown_target = node;
node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y())); node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y()));
} }
@ -272,7 +269,7 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
} else { } else {
if (button == GUI::MouseButton::Primary) { if (button == GUI::MouseButton::Primary) {
auto result = layout_root()->hit_test(position, Layout::HitTestType::TextCursor); auto result = layout_root()->hit_test(position, Layout::HitTestType::TextCursor);
if (result.layout_node && result.layout_node->dom_node()) { if (result.paintable && result.paintable->layout_node().dom_node()) {
// See if we want to focus something. // See if we want to focus something.
bool did_focus_something = false; bool did_focus_something = false;
@ -287,8 +284,8 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
// If we didn't focus anything, place the document text cursor at the mouse position. // If we didn't focus anything, place the document text cursor at the mouse position.
// FIXME: This is all rather strange. Find a better solution. // FIXME: This is all rather strange. Find a better solution.
if (!did_focus_something) { if (!did_focus_something) {
m_browsing_context.set_cursor_position(DOM::Position(*result.layout_node->dom_node(), result.index_in_node)); m_browsing_context.set_cursor_position(DOM::Position(*result.paintable->layout_node().dom_node(), result.index_in_node));
layout_root()->set_selection({ { result.layout_node, result.index_in_node }, {} }); layout_root()->set_selection({ { result.paintable->layout_node(), result.index_in_node }, {} });
m_in_mouse_selection = true; m_in_mouse_selection = true;
} }
} }
@ -317,25 +314,25 @@ bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned butt
Gfx::StandardCursor hovered_node_cursor = Gfx::StandardCursor::None; Gfx::StandardCursor hovered_node_cursor = Gfx::StandardCursor::None;
auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact); auto result = layout_root()->hit_test(position, Layout::HitTestType::Exact);
const HTML::HTMLAnchorElement* hovered_link_element = nullptr; const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
if (result.layout_node) { if (result.paintable) {
if (result.layout_node->paintable() && result.layout_node->paintable()->wants_mouse_events()) { if (result.paintable->wants_mouse_events()) {
document.set_hovered_node(result.layout_node->dom_node()); document.set_hovered_node(result.paintable->layout_node().dom_node());
result.layout_node->paintable()->handle_mousemove({}, position, buttons, modifiers); result.paintable->handle_mousemove({}, position, buttons, modifiers);
// FIXME: It feels a bit aggressive to always update the cursor like this. // FIXME: It feels a bit aggressive to always update the cursor like this.
if (auto* page = m_browsing_context.page()) if (auto* page = m_browsing_context.page())
page->client().page_did_request_cursor_change(Gfx::StandardCursor::None); page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
return true; return true;
} }
RefPtr<DOM::Node> node = result.layout_node->dom_node(); RefPtr<DOM::Node> node = result.paintable->layout_node().dom_node();
if (node && is<HTML::HTMLIFrameElement>(*node)) { if (node && is<HTML::HTMLIFrameElement>(*node)) {
if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context()) if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
return nested_browsing_context->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers); return nested_browsing_context->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, result.paintable->layout_node())), buttons, modifiers);
return false; return false;
} }
auto pointer_events = result.layout_node->computed_values().pointer_events(); auto pointer_events = result.paintable->computed_values().pointer_events();
// FIXME: Handle other values for pointer-events. // FIXME: Handle other values for pointer-events.
if (pointer_events == CSS::PointerEvents::None) if (pointer_events == CSS::PointerEvents::None)
return false; return false;
@ -348,20 +345,20 @@ bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned butt
is_hovering_link = true; is_hovering_link = true;
if (node->is_text()) { if (node->is_text()) {
auto cursor = result.layout_node->computed_values().cursor(); auto cursor = result.paintable->computed_values().cursor();
if (cursor == CSS::Cursor::Auto) if (cursor == CSS::Cursor::Auto)
hovered_node_cursor = Gfx::StandardCursor::IBeam; hovered_node_cursor = Gfx::StandardCursor::IBeam;
else else
hovered_node_cursor = cursor_css_to_gfx(cursor); hovered_node_cursor = cursor_css_to_gfx(cursor);
} else if (node->is_element()) { } else if (node->is_element()) {
auto cursor = result.layout_node->computed_values().cursor(); auto cursor = result.paintable->computed_values().cursor();
if (cursor == CSS::Cursor::Auto) if (cursor == CSS::Cursor::Auto)
hovered_node_cursor = Gfx::StandardCursor::Arrow; hovered_node_cursor = Gfx::StandardCursor::Arrow;
else else
hovered_node_cursor = cursor_css_to_gfx(cursor); hovered_node_cursor = cursor_css_to_gfx(cursor);
} }
auto offset = compute_mouse_event_offset(position, *result.layout_node); auto offset = compute_mouse_event_offset(position, result.paintable->layout_node());
node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y())); node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y()));
// NOTE: Dispatching an event may have disturbed the world. // NOTE: Dispatching an event may have disturbed the world.
if (!layout_root() || layout_root() != node->document().layout_node()) if (!layout_root() || layout_root() != node->document().layout_node())
@ -369,9 +366,9 @@ bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned butt
} }
if (m_in_mouse_selection) { if (m_in_mouse_selection) {
auto hit = layout_root()->hit_test(position, Layout::HitTestType::TextCursor); auto hit = layout_root()->hit_test(position, Layout::HitTestType::TextCursor);
if (hit.layout_node && hit.layout_node->dom_node()) { if (hit.paintable && hit.paintable->layout_node().dom_node()) {
m_browsing_context.set_cursor_position(DOM::Position(*hit.layout_node->dom_node(), result.index_in_node)); m_browsing_context.set_cursor_position(DOM::Position(*hit.paintable->layout_node().dom_node(), result.index_in_node));
layout_root()->set_selection_end({ hit.layout_node, hit.index_in_node }); layout_root()->set_selection_end({ hit.paintable->layout_node(), hit.index_in_node });
} }
if (auto* page = m_browsing_context.page()) if (auto* page = m_browsing_context.page())
page->client().page_did_change_selection(); page->client().page_did_change_selection();

View file

@ -168,7 +168,7 @@ Layout::HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, L
if (child.m_box.computed_values().z_index().value_or(0) < 0) if (child.m_box.computed_values().z_index().value_or(0) < 0)
break; break;
auto result = child.hit_test(position, type); auto result = child.hit_test(position, type);
if (result.layout_node) if (result.paintable)
return result; return result;
} }
@ -177,18 +177,18 @@ Layout::HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, L
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) { m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
if (box.is_positioned() && !box.paint_box()->stacking_context()) { if (box.is_positioned() && !box.paint_box()->stacking_context()) {
result = box.hit_test(position, type); result = box.hit_test(position, type);
if (result.layout_node) if (result.paintable)
return IterationDecision::Break; return IterationDecision::Break;
} }
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
if (result.layout_node) if (result.paintable)
return result; return result;
// 5. the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks. // 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)) { if (m_box.children_are_inline() && is<Layout::BlockContainer>(m_box)) {
auto result = m_box.hit_test(position, type); auto result = m_box.hit_test(position, type);
if (result.layout_node) if (result.paintable)
return result; return result;
} }
@ -196,7 +196,7 @@ Layout::HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, L
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) { m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
if (box.is_floating()) { if (box.is_floating()) {
result = box.hit_test(position, type); result = box.hit_test(position, type);
if (result.layout_node) if (result.paintable)
return IterationDecision::Break; return IterationDecision::Break;
} }
return IterationDecision::Continue; return IterationDecision::Continue;
@ -207,12 +207,12 @@ Layout::HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, L
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) { m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
if (!box.is_absolutely_positioned() && !box.is_floating()) { if (!box.is_absolutely_positioned() && !box.is_floating()) {
result = box.hit_test(position, type); result = box.hit_test(position, type);
if (result.layout_node) if (result.paintable)
return IterationDecision::Break; return IterationDecision::Break;
} }
return IterationDecision::Continue; return IterationDecision::Continue;
}); });
if (result.layout_node) if (result.paintable)
return result; return result;
} }
@ -222,14 +222,14 @@ Layout::HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, L
if (child.m_box.computed_values().z_index().value_or(0) < 0) if (child.m_box.computed_values().z_index().value_or(0) < 0)
break; break;
auto result = child.hit_test(position, type); auto result = child.hit_test(position, type);
if (result.layout_node) if (result.paintable)
return result; return result;
} }
// 1. the background and borders of the element forming the stacking context. // 1. the background and borders of the element forming the stacking context.
if (m_box.paint_box()->absolute_border_box_rect().contains(position.to_type<float>())) { if (m_box.paint_box()->absolute_border_box_rect().contains(position.to_type<float>())) {
return Layout::HitTestResult { return Layout::HitTestResult {
.layout_node = m_box, .paintable = m_box.paintable(),
}; };
} }