mirror of
https://github.com/RGBCube/serenity
synced 2025-05-25 22:25:08 +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
3 changed files with 46 additions and 12 deletions
|
@ -1,7 +1,10 @@
|
||||||
|
#include <LibGUI/GPainter.h>
|
||||||
#include <LibHTML/DOM/Element.h>
|
#include <LibHTML/DOM/Element.h>
|
||||||
#include <LibHTML/Layout/LayoutBlock.h>
|
#include <LibHTML/Layout/LayoutBlock.h>
|
||||||
#include <LibHTML/Layout/LayoutNode.h>
|
#include <LibHTML/Layout/LayoutNode.h>
|
||||||
|
|
||||||
|
//#define DRAW_BOXES_AROUND_LAYOUT_NODES
|
||||||
|
|
||||||
LayoutNode::LayoutNode(const Node* node, StyleProperties&& style_properties)
|
LayoutNode::LayoutNode(const Node* node, StyleProperties&& style_properties)
|
||||||
: m_node(node)
|
: m_node(node)
|
||||||
, m_style_properties(style_properties)
|
, m_style_properties(style_properties)
|
||||||
|
@ -30,6 +33,9 @@ const LayoutBlock* LayoutNode::containing_block() const
|
||||||
|
|
||||||
void LayoutNode::render(RenderingContext& context)
|
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
|
// TODO: render our background and border
|
||||||
for_each_child([&](auto& child) {
|
for_each_child([&](auto& child) {
|
||||||
child.render(context);
|
child.render(context);
|
||||||
|
@ -38,9 +44,10 @@ void LayoutNode::render(RenderingContext& context)
|
||||||
|
|
||||||
HitTestResult LayoutNode::hit_test(const Point& position) const
|
HitTestResult LayoutNode::hit_test(const Point& position) const
|
||||||
{
|
{
|
||||||
if (!m_rect.contains(position))
|
// FIXME: It would be nice if we could confidently skip over hit testing
|
||||||
return {};
|
// parts of the layout tree, but currently we can't just check
|
||||||
HitTestResult result { this };
|
// m_rect.contains() since inline text rects can't be trusted..
|
||||||
|
HitTestResult result { m_rect.contains(position) ? this : nullptr };
|
||||||
for_each_child([&](auto& child) {
|
for_each_child([&](auto& child) {
|
||||||
auto child_result = child.hit_test(position);
|
auto child_result = child.hit_test(position);
|
||||||
if (child_result.layout_node)
|
if (child_result.layout_node)
|
||||||
|
|
|
@ -222,6 +222,21 @@ void LayoutText::layout()
|
||||||
rect().set_bottom(last_run.pos.y() + m_font->glyph_height());
|
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)
|
void LayoutText::render(RenderingContext& context)
|
||||||
{
|
{
|
||||||
auto& painter = context.painter();
|
auto& painter = context.painter();
|
||||||
|
@ -232,16 +247,23 @@ void LayoutText::render(RenderingContext& context)
|
||||||
|
|
||||||
bool is_underline = text_decoration == "underline";
|
bool is_underline = text_decoration == "underline";
|
||||||
|
|
||||||
for (auto& run : m_runs) {
|
for_each_run([&](auto& run, auto& rect) {
|
||||||
Rect rect {
|
|
||||||
run.pos.x(),
|
|
||||||
run.pos.y(),
|
|
||||||
m_font->width(run.text),
|
|
||||||
m_font->glyph_height()
|
|
||||||
};
|
|
||||||
painter.draw_text(rect, run.text, TextAlignment::TopLeft, color);
|
painter.draw_text(rect, run.text, TextAlignment::TopLeft, color);
|
||||||
|
|
||||||
if (is_underline)
|
if (is_underline)
|
||||||
painter.draw_line(rect.bottom_left().translated(0, 1), rect.bottom_right().translated(0, 1), color);
|
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; }
|
const Vector<Run>& runs() const { return m_runs; }
|
||||||
|
|
||||||
|
virtual HitTestResult hit_test(const Point&) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
template<typename Callback>
|
||||||
|
void for_each_run(Callback) const;
|
||||||
|
|
||||||
void load_font();
|
void load_font();
|
||||||
void compute_runs();
|
void compute_runs();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue