mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
LibHTML: Make hit testing work for LayoutText
LayoutText can't simply rely on its LayoutNode::rect() for hit testing. Instead, we have to iterate over the individual runs and see if we're hitting any of them. Also, LayoutNode::hit_test() now always recurses into children, since we can't trust the rect() to tell the truth (inline rects are wrong.)
This commit is contained in:
parent
aac8b091a4
commit
13860e4dd8
Notes:
sideshowbarker
2024-07-19 11:54:00 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/13860e4dd84
3 changed files with 46 additions and 12 deletions
|
@ -1,7 +1,10 @@
|
|||
#include <LibGUI/GPainter.h>
|
||||
#include <LibHTML/DOM/Element.h>
|
||||
#include <LibHTML/Layout/LayoutBlock.h>
|
||||
#include <LibHTML/Layout/LayoutNode.h>
|
||||
|
||||
//#define DRAW_BOXES_AROUND_LAYOUT_NODES
|
||||
|
||||
LayoutNode::LayoutNode(const Node* node, StyleProperties&& style_properties)
|
||||
: m_node(node)
|
||||
, m_style_properties(style_properties)
|
||||
|
@ -30,6 +33,9 @@ const LayoutBlock* LayoutNode::containing_block() const
|
|||
|
||||
void LayoutNode::render(RenderingContext& context)
|
||||
{
|
||||
#ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
|
||||
context.painter().draw_rect(m_rect, Color::Blue);
|
||||
#endif
|
||||
// TODO: render our background and border
|
||||
for_each_child([&](auto& child) {
|
||||
child.render(context);
|
||||
|
@ -38,9 +44,10 @@ void LayoutNode::render(RenderingContext& context)
|
|||
|
||||
HitTestResult LayoutNode::hit_test(const Point& position) const
|
||||
{
|
||||
if (!m_rect.contains(position))
|
||||
return {};
|
||||
HitTestResult result { this };
|
||||
// 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
|
||||
// m_rect.contains() since inline text rects can't be trusted..
|
||||
HitTestResult result { m_rect.contains(position) ? this : nullptr };
|
||||
for_each_child([&](auto& child) {
|
||||
auto child_result = child.hit_test(position);
|
||||
if (child_result.layout_node)
|
||||
|
|
|
@ -222,6 +222,21 @@ void LayoutText::layout()
|
|||
rect().set_bottom(last_run.pos.y() + m_font->glyph_height());
|
||||
}
|
||||
|
||||
template<typename Callback>
|
||||
void LayoutText::for_each_run(Callback callback) const
|
||||
{
|
||||
for (auto& run : m_runs) {
|
||||
Rect rect {
|
||||
run.pos.x(),
|
||||
run.pos.y(),
|
||||
m_font->width(run.text),
|
||||
m_font->glyph_height()
|
||||
};
|
||||
if (callback(run, rect) == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void LayoutText::render(RenderingContext& context)
|
||||
{
|
||||
auto& painter = context.painter();
|
||||
|
@ -232,16 +247,23 @@ void LayoutText::render(RenderingContext& context)
|
|||
|
||||
bool is_underline = text_decoration == "underline";
|
||||
|
||||
for (auto& run : m_runs) {
|
||||
Rect rect {
|
||||
run.pos.x(),
|
||||
run.pos.y(),
|
||||
m_font->width(run.text),
|
||||
m_font->glyph_height()
|
||||
};
|
||||
for_each_run([&](auto& run, auto& rect) {
|
||||
painter.draw_text(rect, run.text, TextAlignment::TopLeft, color);
|
||||
|
||||
if (is_underline)
|
||||
painter.draw_line(rect.bottom_left().translated(0, 1), rect.bottom_right().translated(0, 1), color);
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
|
||||
HitTestResult LayoutText::hit_test(const Point& position) const
|
||||
{
|
||||
HitTestResult result;
|
||||
for_each_run([&](auto&, auto& rect) {
|
||||
if (rect.contains(position)) {
|
||||
result.layout_node = this;
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,12 @@ public:
|
|||
|
||||
const Vector<Run>& runs() const { return m_runs; }
|
||||
|
||||
virtual HitTestResult hit_test(const Point&) const override;
|
||||
|
||||
private:
|
||||
template<typename Callback>
|
||||
void for_each_run(Callback) const;
|
||||
|
||||
void load_font();
|
||||
void compute_runs();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue