Ver código fonte

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. :^)
Andreas Kling 5 anos atrás
pai
commit
92aae72025
2 arquivos alterados com 28 adições e 0 exclusões
  1. 25 0
      Libraries/LibHTML/HtmlView.cpp
  2. 3 0
      Libraries/LibHTML/HtmlView.h

+ 25 - 0
Libraries/LibHTML/HtmlView.cpp

@@ -103,3 +103,28 @@ void HtmlView::mousemove_event(GMouseEvent& event)
         update();
         update();
     event.accept();
     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();
+}

+ 3 - 0
Libraries/LibHTML/HtmlView.h

@@ -12,12 +12,15 @@ public:
     const Document* document() const { return m_document; }
     const Document* document() const { return m_document; }
     void set_document(Document*);
     void set_document(Document*);
 
 
+    Function<void(const String&)> on_link_click;
+
 protected:
 protected:
     HtmlView(GWidget* parent = nullptr);
     HtmlView(GWidget* parent = nullptr);
 
 
     virtual void resize_event(GResizeEvent&) override;
     virtual void resize_event(GResizeEvent&) override;
     virtual void paint_event(GPaintEvent&) override;
     virtual void paint_event(GPaintEvent&) override;
     virtual void mousemove_event(GMouseEvent&) override;
     virtual void mousemove_event(GMouseEvent&) override;
+    virtual void mousedown_event(GMouseEvent&) override;
 
 
 private:
 private:
     void layout_and_sync_size();
     void layout_and_sync_size();