From ad8e2f481d72b70a9a68b09f95a9e306d8c6a7c3 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 15 Apr 2021 20:50:02 +0300 Subject: [PATCH] LibWeb: Expose the MouseEvent::{clientX, clientY} attributes These provide the cursor coordinate within the viewport at which the event occurred (as opposed to the page relative coordinates exposed via offsetX, offsetY). --- Userland/Libraries/LibWeb/DOM/Document.cpp | 2 +- Userland/Libraries/LibWeb/Page/EventHandler.cpp | 6 +++--- Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp | 4 +++- Userland/Libraries/LibWeb/UIEvents/MouseEvent.h | 10 +++++++--- Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl | 2 ++ 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index a4f77872a4e..59c2407904a 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -660,7 +660,7 @@ NonnullRefPtr Document::create_event(const String& interface) } else if (interface_lowercase == "messageevent") { event = Event::create(""); // FIXME: Create MessageEvent } else if (interface_lowercase.is_one_of("mouseevent", "mouseevents")) { - event = UIEvents::MouseEvent::create("", 0, 0); + event = UIEvents::MouseEvent::create("", 0, 0, 0, 0); } else if (interface_lowercase == "storageevent") { event = Event::create(""); // FIXME: Create StorageEvent } else if (interface_lowercase == "svgevents") { diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.cpp b/Userland/Libraries/LibWeb/Page/EventHandler.cpp index e751a468ea3..65936c285f8 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EventHandler.cpp @@ -182,7 +182,7 @@ bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button return false; } auto offset = compute_mouse_event_offset(position, *result.layout_node); - node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y())); + node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y())); handled_event = true; } @@ -230,7 +230,7 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt page->set_focused_frame({}, m_frame); auto offset = compute_mouse_event_offset(position, *result.layout_node); - node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y())); + node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y())); } // NOTE: Dispatching an event may have disturbed the world. @@ -337,7 +337,7 @@ bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned butt hovered_node_cursor = cursor_css_to_gfx(cursor); auto offset = compute_mouse_event_offset(position, *result.layout_node); - node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y())); + node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y())); // NOTE: Dispatching an event may have disturbed the world. if (!layout_root() || layout_root() != node->document().layout_node()) return true; diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp index e102a4e1380..8b3aab45b4a 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp @@ -30,10 +30,12 @@ namespace Web::UIEvents { -MouseEvent::MouseEvent(const FlyString& event_name, i32 offset_x, i32 offset_y) +MouseEvent::MouseEvent(const FlyString& event_name, i32 offset_x, i32 offset_y, i32 client_x, i32 client_y) : UIEvent(event_name) , m_offset_x(offset_x) , m_offset_y(offset_y) + , m_client_x(client_x) + , m_client_y(client_y) { set_event_characteristics(); } diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h index 13fc8d3322c..4c2d216fc77 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h @@ -35,24 +35,28 @@ class MouseEvent final : public UIEvents::UIEvent { public: using WrapperType = Bindings::MouseEventWrapper; - static NonnullRefPtr create(const FlyString& event_name, i32 offset_x, i32 offset_y) + static NonnullRefPtr create(const FlyString& event_name, i32 offset_x, i32 offset_y, i32 client_x, i32 client_y) { - return adopt(*new MouseEvent(event_name, offset_x, offset_y)); + return adopt(*new MouseEvent(event_name, offset_x, offset_y, client_x, client_y)); } virtual ~MouseEvent() override; i32 offset_x() const { return m_offset_x; } i32 offset_y() const { return m_offset_y; } + i32 client_x() const { return m_client_x; } + i32 client_y() const { return m_client_y; } protected: - MouseEvent(const FlyString& event_name, i32 offset_x, i32 offset_y); + MouseEvent(const FlyString& event_name, i32 offset_x, i32 offset_y, i32 client_x, i32 client_y); private: void set_event_characteristics(); i32 m_offset_x { 0 }; i32 m_offset_y { 0 }; + i32 m_client_x { 0 }; + i32 m_client_y { 0 }; }; } diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl index 2751f2353a6..452ef261d4b 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl @@ -2,5 +2,7 @@ interface MouseEvent : Event { readonly attribute double offsetX; readonly attribute double offsetY; + readonly attribute double clientX; + readonly attribute double clientY; };