LibHTML: Build some foundation for text selection

Add LayoutPosition and LayoutRange classes. The layout tree root node
now has a selection() LayoutRange. It's essentially a start and end
LayoutPosition.

A LayoutPosition is a LayoutNode, and an optional index into that node.
The index is only relevant for text nodes, where it's the character
index into the rendered text.

HtmlView now updates the selection start/end of the LayoutDocument when
clicking and dragging with the left mouse button.

We don't paint the selection yet, and there's no way to copy what's
selected. It only exists as a LayoutRange.
This commit is contained in:
Andreas Kling 2019-11-05 22:13:26 +01:00
parent 2755184e11
commit f3f0b08d43
Notes: sideshowbarker 2024-07-19 11:21:36 +09:00
8 changed files with 119 additions and 2 deletions

View file

@ -64,7 +64,6 @@ void HtmlView::set_document(Document* new_document)
};
}
#ifdef HTML_DEBUG
if (document != nullptr) {
dbgprintf("\033[33;1mLayout tree before layout:\033[0m\n");
@ -159,6 +158,11 @@ void HtmlView::mousemove_event(GMouseEvent& event)
is_hovering_link = true;
}
}
if (m_in_mouse_selection) {
layout_root()->selection().set_end({ result.layout_node, result.index_in_node });
dump_selection("MouseMove");
update();
}
}
if (window())
window()->set_override_cursor(is_hovering_link ? GStandardCursor::Hand : GStandardCursor::None);
@ -196,6 +200,12 @@ void HtmlView::mousedown_event(GMouseEvent& event)
dbg() << "HtmlView: clicking on a link to " << link->href();
if (on_link_click)
on_link_click(link->href());
} else {
if (event.button() == GMouseButton::Left) {
layout_root()->selection().set({ result.layout_node, result.index_in_node }, {});
dump_selection("MouseDown");
m_in_mouse_selection = true;
}
}
}
}
@ -204,6 +214,17 @@ void HtmlView::mousedown_event(GMouseEvent& event)
event.accept();
}
void HtmlView::mouseup_event(GMouseEvent& event)
{
if (!layout_root())
return GScrollableWidget::mouseup_event(event);
if (event.button() == GMouseButton::Left) {
dump_selection("MouseUp");
m_in_mouse_selection = false;
}
}
void HtmlView::keydown_event(GKeyEvent& event)
{
if (event.modifiers() == 0) {
@ -352,3 +373,10 @@ const Document* HtmlView::document() const
{
return main_frame().document();
}
void HtmlView::dump_selection(const char* event_name)
{
dbg() << event_name << " selection start: "
<< layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: "
<< layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node;
}