summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-21 18:27:06 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-21 18:27:06 +0100
commit55c845713a260d249936f91bc265ceb6fd084876 (patch)
tree45c025dcf745e01a7c5183e8925984ce221df5d2 /Libraries
parent4dde36844b26de285abdd289a2b0443bfc54188c (diff)
downloadserenity-55c845713a260d249936f91bc265ceb6fd084876.zip
LibWeb: Give MouseEvents the correct offsetX and offsetY values
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibWeb/HtmlView.cpp23
-rw-r--r--Libraries/LibWeb/HtmlView.h1
2 files changed, 20 insertions, 4 deletions
diff --git a/Libraries/LibWeb/HtmlView.cpp b/Libraries/LibWeb/HtmlView.cpp
index b46f0d9c48..240a6cc3cf 100644
--- a/Libraries/LibWeb/HtmlView.cpp
+++ b/Libraries/LibWeb/HtmlView.cpp
@@ -185,7 +185,8 @@ void HtmlView::mousemove_event(GUI::MouseEvent& event)
#endif
is_hovering_link = true;
}
- const_cast<Node*>(node)->dispatch_event(MouseEvent::create("mousemove", 2, 3));
+ auto offset = compute_mouse_event_offset(event.position(), *result.layout_node);
+ const_cast<Node*>(node)->dispatch_event(MouseEvent::create("mousemove", offset.x(), offset.y()));
}
if (m_in_mouse_selection) {
layout_root()->selection().set_end({ result.layout_node, result.index_in_node });
@@ -236,7 +237,8 @@ void HtmlView::mousedown_event(GUI::MouseEvent& event)
m_in_mouse_selection = true;
}
}
- const_cast<Node*>(node)->dispatch_event(MouseEvent::create("mousedown", 2, 3));
+ auto offset = compute_mouse_event_offset(event.position(), *result.layout_node);
+ const_cast<Node*>(node)->dispatch_event(MouseEvent::create("mousedown", offset.x(), offset.y()));
}
}
if (hovered_node_changed)
@@ -251,8 +253,10 @@ void HtmlView::mouseup_event(GUI::MouseEvent& event)
auto result = layout_root()->hit_test(to_content_position(event.position()));
if (result.layout_node) {
- if (auto* node = result.layout_node->node())
- const_cast<Node*>(node)->dispatch_event(MouseEvent::create("mouseup", 2, 3));
+ if (auto* node = result.layout_node->node()) {
+ auto offset = compute_mouse_event_offset(event.position(), *result.layout_node);
+ const_cast<Node*>(node)->dispatch_event(MouseEvent::create("mouseup", offset.x(), offset.y()));
+ }
}
if (event.button() == GUI::MouseButton::Left) {
@@ -423,4 +427,15 @@ void HtmlView::did_scroll()
main_frame().set_viewport_rect(visible_content_rect());
}
+Gfx::Point HtmlView::compute_mouse_event_offset(const Gfx::Point& event_position, const LayoutNode& layout_node) const
+{
+ auto content_event_position = to_content_position(event_position);
+ auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
+
+ return {
+ content_event_position.x() - static_cast<int>(top_left_of_layout_node.x()),
+ content_event_position.y() - static_cast<int>(top_left_of_layout_node.y())
+ };
+}
+
}
diff --git a/Libraries/LibWeb/HtmlView.h b/Libraries/LibWeb/HtmlView.h
index 7eed509711..c287b4a3dd 100644
--- a/Libraries/LibWeb/HtmlView.h
+++ b/Libraries/LibWeb/HtmlView.h
@@ -78,6 +78,7 @@ private:
void layout_and_sync_size();
void dump_selection(const char* event_name);
+ Gfx::Point compute_mouse_event_offset(const Gfx::Point&, const LayoutNode&) const;
RefPtr<Web::Frame> m_main_frame;