ソースを参照

LibHTML: Have Document track its hovered Node

This gets set from HtmlView::mousemove_event() at the moment.
Andreas Kling 5 年 前
コミット
88de955073

+ 5 - 0
Libraries/LibHTML/DOM/Document.h

@@ -25,7 +25,12 @@ public:
 
     virtual String tag_name() const override { return "#document"; }
 
+    void set_hovered_node(Node* node) { m_hovered_node = node; }
+    Node* hovered_node() { return m_hovered_node; }
+    const Node* hovered_node() const { return m_hovered_node; }
+
 private:
     OwnPtr<StyleResolver> m_style_resolver;
     NonnullRefPtrVector<StyleSheet> m_sheets;
+    RefPtr<Node> m_hovered_node;
 };

+ 7 - 1
Libraries/LibHTML/HtmlView.cpp

@@ -85,11 +85,17 @@ void HtmlView::mousemove_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) {
-        if (auto* node = result.layout_node->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: mousemove: " << node->tag_name() << "{" << node << "}";
         }
     }
+    if (hovered_node_changed)
+        update();
     event.accept();
 }

+ 6 - 0
Libraries/LibHTML/Layout/LayoutNode.cpp

@@ -1,9 +1,11 @@
 #include <LibGUI/GPainter.h>
+#include <LibHTML/DOM/Document.h>
 #include <LibHTML/DOM/Element.h>
 #include <LibHTML/Layout/LayoutBlock.h>
 #include <LibHTML/Layout/LayoutNode.h>
 
 //#define DRAW_BOXES_AROUND_LAYOUT_NODES
+//#define DRAW_BOXES_AROUND_HOVERED_NODES
 
 LayoutNode::LayoutNode(const Node* node, StyleProperties&& style_properties)
     : m_node(node)
@@ -35,6 +37,10 @@ void LayoutNode::render(RenderingContext& context)
 {
 #ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
     context.painter().draw_rect(m_rect, Color::Blue);
+#endif
+#ifdef DRAW_BOXES_AROUND_HOVERED_NODES
+    if (!is_anonymous() && node() == document().hovered_node())
+        context.painter().draw_rect(m_rect, Color::Red);
 #endif
     // TODO: render our background and border
     for_each_child([&](auto& child) {