summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-09-28 23:02:22 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-09-28 23:04:59 +0200
commit3de4b99dc34ca8fc29ad323a36800347eaebaa65 (patch)
tree7ddbd59feb7fcb93f9e3f9f8848e44864793deca /Libraries
parent3ed41abba4c9d8640431ac638c3a8143458428fa (diff)
downloadserenity-3de4b99dc34ca8fc29ad323a36800347eaebaa65.zip
LibHTML: Implement naive hit testing
We don't have proper line boxes yet, so we can't easily hit test inline text.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibHTML/HtmlView.cpp15
-rw-r--r--Libraries/LibHTML/HtmlView.h1
-rw-r--r--Libraries/LibHTML/Layout/LayoutNode.cpp14
-rw-r--r--Libraries/LibHTML/Layout/LayoutNode.h8
4 files changed, 38 insertions, 0 deletions
diff --git a/Libraries/LibHTML/HtmlView.cpp b/Libraries/LibHTML/HtmlView.cpp
index 99bf292807..23c0a2f971 100644
--- a/Libraries/LibHTML/HtmlView.cpp
+++ b/Libraries/LibHTML/HtmlView.cpp
@@ -1,5 +1,6 @@
#include <LibGUI/GPainter.h>
#include <LibGUI/GScrollBar.h>
+#include <LibHTML/DOM/Element.h>
#include <LibHTML/Dump.h>
#include <LibHTML/HtmlView.h>
#include <LibHTML/Layout/LayoutNode.h>
@@ -78,3 +79,17 @@ void HtmlView::paint_event(GPaintEvent& event)
RenderingContext context { painter };
m_layout_root->render(context);
}
+
+void HtmlView::mousemove_event(GMouseEvent& event)
+{
+ if (!m_layout_root)
+ return GScrollableWidget::mousemove_event(event);
+
+ auto result = m_layout_root->hit_test(event.position());
+ if (result.layout_node) {
+ if (auto* node = result.layout_node->node()) {
+ dbg() << "HtmlView: mousemove: " << node->tag_name() << "{" << node << "}";
+ }
+ }
+ event.accept();
+}
diff --git a/Libraries/LibHTML/HtmlView.h b/Libraries/LibHTML/HtmlView.h
index 8d04f738fe..d7b9b6d668 100644
--- a/Libraries/LibHTML/HtmlView.h
+++ b/Libraries/LibHTML/HtmlView.h
@@ -17,6 +17,7 @@ protected:
virtual void resize_event(GResizeEvent&) override;
virtual void paint_event(GPaintEvent&) override;
+ virtual void mousemove_event(GMouseEvent&) override;
private:
void layout_and_sync_size();
diff --git a/Libraries/LibHTML/Layout/LayoutNode.cpp b/Libraries/LibHTML/Layout/LayoutNode.cpp
index e4500008f1..ba8d19722a 100644
--- a/Libraries/LibHTML/Layout/LayoutNode.cpp
+++ b/Libraries/LibHTML/Layout/LayoutNode.cpp
@@ -1,3 +1,4 @@
+#include <LibHTML/DOM/Element.h>
#include <LibHTML/Layout/LayoutBlock.h>
#include <LibHTML/Layout/LayoutNode.h>
@@ -34,3 +35,16 @@ void LayoutNode::render(RenderingContext& context)
child.render(context);
});
}
+
+HitTestResult LayoutNode::hit_test(const Point& position) const
+{
+ if (!m_rect.contains(position))
+ return {};
+ HitTestResult result { this };
+ for_each_child([&](auto& child) {
+ auto child_result = child.hit_test(position);
+ if (child_result.layout_node)
+ result = child_result;
+ });
+ return result;
+}
diff --git a/Libraries/LibHTML/Layout/LayoutNode.h b/Libraries/LibHTML/Layout/LayoutNode.h
index 7803b5fcc8..8663408df8 100644
--- a/Libraries/LibHTML/Layout/LayoutNode.h
+++ b/Libraries/LibHTML/Layout/LayoutNode.h
@@ -9,7 +9,13 @@
#include <LibHTML/TreeNode.h>
class Node;
+class Element;
class LayoutBlock;
+class LayoutNode;
+
+struct HitTestResult {
+ RefPtr<LayoutNode> layout_node;
+};
class LayoutNode : public TreeNode<LayoutNode> {
public:
@@ -22,6 +28,8 @@ public:
ComputedStyle& style() { return m_style; }
const ComputedStyle& style() const { return m_style; }
+ virtual HitTestResult hit_test(const Point&) const;
+
bool is_anonymous() const { return !m_node; }
const Node* node() const { return m_node; }