Procházet zdrojové kódy

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).
Idan Horowitz před 4 roky
rodič
revize
ad8e2f481d

+ 1 - 1
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -660,7 +660,7 @@ NonnullRefPtr<Event> 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") {

+ 3 - 3
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;

+ 3 - 1
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();
 }

+ 7 - 3
Userland/Libraries/LibWeb/UIEvents/MouseEvent.h

@@ -35,24 +35,28 @@ class MouseEvent final : public UIEvents::UIEvent {
 public:
     using WrapperType = Bindings::MouseEventWrapper;
 
-    static NonnullRefPtr<MouseEvent> create(const FlyString& event_name, i32 offset_x, i32 offset_y)
+    static NonnullRefPtr<MouseEvent> 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 };
 };
 
 }

+ 2 - 0
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;
 
 };