1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 05:25:00 +00:00

LibHTML: Detect link clicks

Clicking on a link in an HtmlView will now call on_link_click(String)
if present, allowing you to implement basic hypertext navigation. :^)
This commit is contained in:
Andreas Kling 2019-09-29 12:04:02 +02:00
parent b477aff843
commit 92aae72025
2 changed files with 28 additions and 0 deletions

View file

@ -103,3 +103,28 @@ void HtmlView::mousemove_event(GMouseEvent& event)
update();
event.accept();
}
void HtmlView::mousedown_event(GMouseEvent& event)
{
if (!m_layout_root)
return GScrollableWidget::mousemove_event(event);
bool hovered_node_changed = false;
auto result = m_layout_root->hit_test(event.position());
if (result.layout_node) {
auto* node = result.layout_node->node();
m_document->set_hovered_node(const_cast<Node*>(node));
hovered_node_changed = node == m_document->hovered_node();
if (node) {
dbg() << "HtmlView: mousedown: " << node->tag_name() << "{" << node << "}";
if (auto* link = node->enclosing_link_element()) {
dbg() << "HtmlView: clicking on a link to " << link->href();
if (on_link_click)
on_link_click(link->href());
}
}
}
if (hovered_node_changed)
update();
event.accept();
}