diff options
author | Igor Pissolati <igo08an@hotmail.com> | 2022-04-09 12:44:42 -0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-09 18:27:24 +0200 |
commit | 1b94b4c593e5a0600d18f448c019a9a8b3533340 (patch) | |
tree | c985c14065668d97663db5f23f9a23e9b3dd32a9 /Userland | |
parent | a2bc97a9c232df35ad3412c8e6f64602ac812d79 (diff) | |
download | serenity-1b94b4c593e5a0600d18f448c019a9a8b3533340.zip |
LibWeb: Bring MouseEvent a bit closer to spec
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/HTMLElement.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Page/EventHandler.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp | 44 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/MouseEvent.h | 23 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl | 14 |
6 files changed, 75 insertions, 18 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index d91d6c1e44..c5804f59e3 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -980,7 +980,7 @@ NonnullRefPtr<Event> Document::create_event(String const& interface) } else if (interface_lowercase == "messageevent") { event = HTML::MessageEvent::create(""); } else if (interface_lowercase.is_one_of("mouseevent", "mouseevents")) { - event = UIEvents::MouseEvent::create("", 0, 0, 0, 0); + event = UIEvents::MouseEvent::create(""); } else if (interface_lowercase == "storageevent") { event = Event::create(""); // FIXME: Create StorageEvent } else if (interface_lowercase == "svgevents") { diff --git a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp index 748d85fc5e..73de1cd369 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp @@ -395,7 +395,7 @@ bool HTMLElement::fire_a_synthetic_pointer_event(FlyString const& type, DOM::Ele // 1. Let event be the result of creating an event using PointerEvent. // 2. Initialize event's type attribute to e. // FIXME: Actually create a PointerEvent! - auto event = UIEvents::MouseEvent::create(type, 0.0, 0.0, 0.0, 0.0); + auto event = UIEvents::MouseEvent::create(type); // 3. Initialize event's bubbles and cancelable attributes to true. event->set_bubbles(true); diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.cpp b/Userland/Libraries/LibWeb/Page/EventHandler.cpp index 90eed4faca..24166b3fb0 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EventHandler.cpp @@ -191,12 +191,12 @@ bool EventHandler::handle_mouseup(Gfx::IntPoint const& position, unsigned button return false; } auto offset = compute_mouse_event_offset(position, paintable->layout_node()); - node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y())); + node->dispatch_event(UIEvents::MouseEvent::create_from_platform_event(UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y(), button)); handled_event = true; bool run_activation_behavior = true; if (node.ptr() == m_mousedown_target && button == GUI::MouseButton::Primary) { - run_activation_behavior = node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::click, offset.x(), offset.y(), position.x(), position.y())); + run_activation_behavior = node->dispatch_event(UIEvents::MouseEvent::create_from_platform_event(UIEvents::EventNames::click, offset.x(), offset.y(), position.x(), position.y(), button)); } if (run_activation_behavior) { @@ -308,7 +308,7 @@ bool EventHandler::handle_mousedown(Gfx::IntPoint const& position, unsigned butt m_mousedown_target = node; auto offset = compute_mouse_event_offset(position, paintable->layout_node()); - node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y())); + node->dispatch_event(UIEvents::MouseEvent::create_from_platform_event(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y(), button)); } // NOTE: Dispatching an event may have disturbed the world. @@ -416,7 +416,7 @@ bool EventHandler::handle_mousemove(Gfx::IntPoint const& position, unsigned butt } auto offset = compute_mouse_event_offset(position, paintable->layout_node()); - node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y())); + node->dispatch_event(UIEvents::MouseEvent::create_from_platform_event(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y())); // NOTE: Dispatching an event may have disturbed the world. if (!paint_root() || paint_root() != node->document().paint_box()) return true; diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp index 96bad7de8f..89c4c5225f 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp @@ -4,23 +4,53 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <LibGUI/Event.h> #include <LibWeb/HTML/EventNames.h> #include <LibWeb/UIEvents/EventNames.h> #include <LibWeb/UIEvents/MouseEvent.h> namespace Web::UIEvents { -MouseEvent::MouseEvent(FlyString const& event_name, double offset_x, double offset_y, double client_x, double 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) +MouseEvent::MouseEvent(FlyString const& event_name, MouseEventInit const& event_init) + : UIEvent(event_name, event_init) + , m_offset_x(event_init.offset_x) + , m_offset_y(event_init.offset_y) + , m_client_x(event_init.client_x) + , m_client_y(event_init.client_y) + , m_button(event_init.button) { set_event_characteristics(); } -MouseEvent::~MouseEvent() = default; +// https://www.w3.org/TR/uievents/#dom-mouseevent-button +static i16 determine_button(unsigned mouse_button) +{ + switch (mouse_button) { + case GUI::MouseButton::Primary: + return 0; + case GUI::MouseButton::Middle: + return 1; + case GUI::MouseButton::Secondary: + return 2; + case GUI::MouseButton::Backward: + return 3; + case GUI::MouseButton::Forward: + return 4; + default: + VERIFY_NOT_REACHED(); + } +} + +NonnullRefPtr<MouseEvent> MouseEvent::create_from_platform_event(FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button) +{ + MouseEventInit event_init {}; + event_init.offset_x = offset_x; + event_init.offset_y = offset_y; + event_init.client_x = client_x; + event_init.client_y = client_y; + event_init.button = determine_button(mouse_button); + return MouseEvent::create(event_name, event_init); +} void MouseEvent::set_event_characteristics() { diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h index 99c2df94f2..5318667283 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h @@ -12,16 +12,27 @@ namespace Web::UIEvents { +struct MouseEventInit : public EventModifierInit { + double offset_x = 0; + double offset_y = 0; + double client_x = 0; + double client_y = 0; + + i16 button = 0; +}; + class MouseEvent final : public UIEvent { public: using WrapperType = Bindings::MouseEventWrapper; - static NonnullRefPtr<MouseEvent> create(FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y) + static NonnullRefPtr<MouseEvent> create(FlyString const& event_name, MouseEventInit const& event_init = {}) { - return adopt_ref(*new MouseEvent(event_name, offset_x, offset_y, client_x, client_y)); + return adopt_ref(*new MouseEvent(event_name, event_init)); } - virtual ~MouseEvent() override; + static NonnullRefPtr<MouseEvent> create_from_platform_event(FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button = 1); + + virtual ~MouseEvent() override = default; double offset_x() const { return m_offset_x; } double offset_y() const { return m_offset_y; } @@ -32,16 +43,18 @@ public: double x() const { return client_x(); } double y() const { return client_y(); } -protected: - MouseEvent(FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y); + i16 button() const { return m_button; } private: + MouseEvent(FlyString const& event_name, MouseEventInit const& event_init); + void set_event_characteristics(); double m_offset_x { 0 }; double m_offset_y { 0 }; double m_client_x { 0 }; double m_client_y { 0 }; + i16 m_button { 0 }; }; } diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl index 972c3a5476..330b65b898 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl @@ -7,4 +7,18 @@ interface MouseEvent : UIEvent { readonly attribute double x; readonly attribute double y; + readonly attribute short button; + }; + +dictionary MouseEventInit : EventModifierInit { + + // FIXME: offsetX and offsetY shouldn't be here. + double offsetX = 0; + double offsetY = 0; + double clientX = 0; + double clientY = 0; + + short button = 0; + +};
\ No newline at end of file |