From 9cbf031b6d8a36ffadaaee78f44bb9ded6b9c7e9 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Thu, 20 Oct 2022 20:56:38 +0300 Subject: [PATCH] LibWeb: Add button property in MouseEvent --- .../Libraries/LibWeb/Page/EventHandler.cpp | 24 +++++++++---------- Userland/Libraries/LibWeb/Page/EventHandler.h | 8 +++---- Userland/Libraries/LibWeb/Page/Page.cpp | 16 ++++++------- Userland/Libraries/LibWeb/Page/Page.h | 8 +++---- .../Libraries/LibWeb/UIEvents/MouseEvent.cpp | 4 +++- .../Libraries/LibWeb/UIEvents/MouseEvent.h | 5 +++- .../Libraries/LibWeb/UIEvents/MouseEvent.idl | 2 +- .../Libraries/LibWeb/UIEvents/WheelEvent.cpp | 4 +++- .../Libraries/LibWeb/UIEvents/WheelEvent.h | 2 +- .../WebContent/ConnectionFromClient.cpp | 16 ++++++------- 10 files changed, 48 insertions(+), 41 deletions(-) diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.cpp b/Userland/Libraries/LibWeb/Page/EventHandler.cpp index 6b7ac55f15..874543769e 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EventHandler.cpp @@ -123,7 +123,7 @@ Painting::PaintableBox const* EventHandler::paint_root() const return const_cast(m_browsing_context.active_document()->paint_box()); } -bool EventHandler::handle_mousewheel(Gfx::IntPoint const& position, unsigned int buttons, unsigned int modifiers, int wheel_delta_x, int wheel_delta_y) +bool EventHandler::handle_mousewheel(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned int modifiers, int wheel_delta_x, int wheel_delta_y) { if (m_browsing_context.active_document()) m_browsing_context.active_document()->update_layout(); @@ -168,7 +168,7 @@ bool EventHandler::handle_mousewheel(Gfx::IntPoint const& position, unsigned int } auto offset = compute_mouse_event_offset(position, *layout_node); - if (node->dispatch_event(*UIEvents::WheelEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::wheel, offset.x(), offset.y(), position.x(), position.y(), wheel_delta_x, wheel_delta_y))) { + if (node->dispatch_event(*UIEvents::WheelEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::wheel, offset.x(), offset.y(), position.x(), position.y(), wheel_delta_x, wheel_delta_y, buttons, button))) { if (auto* page = m_browsing_context.page()) { page->client().page_did_request_scroll(wheel_delta_x * 20, wheel_delta_y * 20); } @@ -181,7 +181,7 @@ bool EventHandler::handle_mousewheel(Gfx::IntPoint const& position, unsigned int return handled_event; } -bool EventHandler::handle_mouseup(Gfx::IntPoint const& position, unsigned button, unsigned modifiers) +bool EventHandler::handle_mouseup(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers) { if (m_browsing_context.active_document()) m_browsing_context.active_document()->update_layout(); @@ -218,7 +218,7 @@ bool EventHandler::handle_mouseup(Gfx::IntPoint const& position, unsigned button if (node) { if (is(*node)) { if (auto* nested_browsing_context = static_cast(*node).nested_browsing_context()) - return nested_browsing_context->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, modifiers); + return nested_browsing_context->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, buttons, modifiers); return false; } @@ -236,7 +236,7 @@ bool EventHandler::handle_mouseup(Gfx::IntPoint const& position, unsigned button } auto offset = compute_mouse_event_offset(position, *layout_node); - node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y(), button)); + node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y(), buttons, button)); handled_event = true; bool run_activation_behavior = true; @@ -302,7 +302,7 @@ after_node_use: return handled_event; } -bool EventHandler::handle_mousedown(Gfx::IntPoint const& position, unsigned button, unsigned modifiers) +bool EventHandler::handle_mousedown(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers) { if (m_browsing_context.active_document()) m_browsing_context.active_document()->update_layout(); @@ -343,7 +343,7 @@ bool EventHandler::handle_mousedown(Gfx::IntPoint const& position, unsigned butt if (is(*node)) { if (auto* nested_browsing_context = static_cast(*node).nested_browsing_context()) - return nested_browsing_context->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, modifiers); + return nested_browsing_context->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, buttons, modifiers); return false; } @@ -363,7 +363,7 @@ bool EventHandler::handle_mousedown(Gfx::IntPoint const& position, unsigned butt m_mousedown_target = node.ptr(); auto offset = compute_mouse_event_offset(position, *layout_node); - node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y(), button)); + node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y(), buttons, button)); } // NOTE: Dispatching an event may have disturbed the world. @@ -483,7 +483,7 @@ bool EventHandler::handle_mousemove(Gfx::IntPoint const& position, unsigned butt } auto offset = compute_mouse_event_offset(position, *layout_node); - node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y())); + node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y(), buttons)); // NOTE: Dispatching an event may have disturbed the world. if (!paint_root() || paint_root() != node->document().paint_box()) return true; @@ -520,7 +520,7 @@ bool EventHandler::handle_mousemove(Gfx::IntPoint const& position, unsigned butt return true; } -bool EventHandler::handle_doubleclick(Gfx::IntPoint const& position, unsigned button, unsigned modifiers) +bool EventHandler::handle_doubleclick(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers) { if (m_browsing_context.active_document()) m_browsing_context.active_document()->update_layout(); @@ -556,7 +556,7 @@ bool EventHandler::handle_doubleclick(Gfx::IntPoint const& position, unsigned bu if (is(*node)) { if (auto* nested_browsing_context = static_cast(*node).nested_browsing_context()) - return nested_browsing_context->event_handler().handle_doubleclick(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, modifiers); + return nested_browsing_context->event_handler().handle_doubleclick(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, buttons, modifiers); return false; } @@ -571,7 +571,7 @@ bool EventHandler::handle_doubleclick(Gfx::IntPoint const& position, unsigned bu return false; auto offset = compute_mouse_event_offset(position, *layout_node); - node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::dblclick, offset.x(), offset.y(), position.x(), position.y(), button)); + node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::dblclick, offset.x(), offset.y(), position.x(), position.y(), buttons, button)); // NOTE: Dispatching an event may have disturbed the world. if (!paint_root() || paint_root() != node->document().paint_box()) diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.h b/Userland/Libraries/LibWeb/Page/EventHandler.h index b65d425d6e..fa302c9695 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.h +++ b/Userland/Libraries/LibWeb/Page/EventHandler.h @@ -22,11 +22,11 @@ public: explicit EventHandler(Badge, HTML::BrowsingContext&); ~EventHandler(); - bool handle_mouseup(Gfx::IntPoint const&, unsigned button, unsigned modifiers); - bool handle_mousedown(Gfx::IntPoint const&, unsigned button, unsigned modifiers); + bool handle_mouseup(Gfx::IntPoint const&, unsigned button, unsigned buttons, unsigned modifiers); + bool handle_mousedown(Gfx::IntPoint const&, unsigned button, unsigned buttons, unsigned modifiers); bool handle_mousemove(Gfx::IntPoint const&, unsigned buttons, unsigned modifiers); - bool handle_mousewheel(Gfx::IntPoint const&, unsigned buttons, unsigned modifiers, int wheel_delta_x, int wheel_delta_y); - bool handle_doubleclick(Gfx::IntPoint const&, unsigned buttons, unsigned modifiers); + bool handle_mousewheel(Gfx::IntPoint const&, unsigned button, unsigned buttons, unsigned modifiers, int wheel_delta_x, int wheel_delta_y); + bool handle_doubleclick(Gfx::IntPoint const&, unsigned button, unsigned buttons, unsigned modifiers); bool handle_keydown(KeyCode, unsigned modifiers, u32 code_point); bool handle_keyup(KeyCode, unsigned modifiers, u32 code_point); diff --git a/Userland/Libraries/LibWeb/Page/Page.cpp b/Userland/Libraries/LibWeb/Page/Page.cpp index 864ed57b71..07a57f9043 100644 --- a/Userland/Libraries/LibWeb/Page/Page.cpp +++ b/Userland/Libraries/LibWeb/Page/Page.cpp @@ -60,19 +60,19 @@ CSS::PreferredColorScheme Page::preferred_color_scheme() const return m_client.preferred_color_scheme(); } -bool Page::handle_mousewheel(Gfx::IntPoint const& position, unsigned button, unsigned modifiers, int wheel_delta_x, int wheel_delta_y) +bool Page::handle_mousewheel(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers, int wheel_delta_x, int wheel_delta_y) { - return top_level_browsing_context().event_handler().handle_mousewheel(position, button, modifiers, wheel_delta_x, wheel_delta_y); + return top_level_browsing_context().event_handler().handle_mousewheel(position, button, buttons, modifiers, wheel_delta_x, wheel_delta_y); } -bool Page::handle_mouseup(Gfx::IntPoint const& position, unsigned button, unsigned modifiers) +bool Page::handle_mouseup(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers) { - return top_level_browsing_context().event_handler().handle_mouseup(position, button, modifiers); + return top_level_browsing_context().event_handler().handle_mouseup(position, button, buttons, modifiers); } -bool Page::handle_mousedown(Gfx::IntPoint const& position, unsigned button, unsigned modifiers) +bool Page::handle_mousedown(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers) { - return top_level_browsing_context().event_handler().handle_mousedown(position, button, modifiers); + return top_level_browsing_context().event_handler().handle_mousedown(position, button, buttons, modifiers); } bool Page::handle_mousemove(Gfx::IntPoint const& position, unsigned buttons, unsigned modifiers) @@ -80,9 +80,9 @@ bool Page::handle_mousemove(Gfx::IntPoint const& position, unsigned buttons, uns return top_level_browsing_context().event_handler().handle_mousemove(position, buttons, modifiers); } -bool Page::handle_doubleclick(Gfx::IntPoint const& position, unsigned button, unsigned modifiers) +bool Page::handle_doubleclick(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers) { - return top_level_browsing_context().event_handler().handle_doubleclick(position, button, modifiers); + return top_level_browsing_context().event_handler().handle_doubleclick(position, button, buttons, modifiers); } bool Page::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point) diff --git a/Userland/Libraries/LibWeb/Page/Page.h b/Userland/Libraries/LibWeb/Page/Page.h index e1a1376ea6..392282fc73 100644 --- a/Userland/Libraries/LibWeb/Page/Page.h +++ b/Userland/Libraries/LibWeb/Page/Page.h @@ -50,11 +50,11 @@ public: void load_html(StringView, const AK::URL&); - bool handle_mouseup(Gfx::IntPoint const&, unsigned button, unsigned modifiers); - bool handle_mousedown(Gfx::IntPoint const&, unsigned button, unsigned modifiers); + bool handle_mouseup(Gfx::IntPoint const&, unsigned button, unsigned buttons, unsigned modifiers); + bool handle_mousedown(Gfx::IntPoint const&, unsigned button, unsigned buttons, unsigned modifiers); bool handle_mousemove(Gfx::IntPoint const&, unsigned buttons, unsigned modifiers); - bool handle_mousewheel(Gfx::IntPoint const&, unsigned button, unsigned modifiers, int wheel_delta_x, int wheel_delta_y); - bool handle_doubleclick(Gfx::IntPoint const&, unsigned buttons, unsigned modifiers); + bool handle_mousewheel(Gfx::IntPoint const&, unsigned button, unsigned buttons, unsigned modifiers, int wheel_delta_x, int wheel_delta_y); + bool handle_doubleclick(Gfx::IntPoint const&, unsigned button, unsigned buttons, unsigned modifiers); bool handle_keydown(KeyCode, unsigned modifiers, u32 code_point); bool handle_keyup(KeyCode, unsigned modifiers, u32 code_point); diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp index d033f7263d..8ccfe84a9e 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.cpp @@ -20,6 +20,7 @@ MouseEvent::MouseEvent(JS::Realm& realm, FlyString const& event_name, MouseEvent , m_client_x(event_init.client_x) , m_client_y(event_init.client_y) , m_button(event_init.button) + , m_buttons(event_init.buttons) { set_prototype(&Bindings::cached_web_prototype(realm, "MouseEvent")); set_event_characteristics(); @@ -51,7 +52,7 @@ MouseEvent* MouseEvent::create(JS::Realm& realm, FlyString const& event_name, Mo return realm.heap().allocate(realm, realm, event_name, event_init); } -MouseEvent* MouseEvent::create_from_platform_event(JS::Realm& realm, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button) +MouseEvent* MouseEvent::create_from_platform_event(JS::Realm& realm, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned buttons, unsigned mouse_button) { MouseEventInit event_init {}; event_init.offset_x = offset_x; @@ -59,6 +60,7 @@ MouseEvent* MouseEvent::create_from_platform_event(JS::Realm& realm, FlyString c event_init.client_x = client_x; event_init.client_y = client_y; event_init.button = determine_button(mouse_button); + event_init.buttons = buttons; return MouseEvent::create(realm, event_name, event_init); } diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h index aed8481a01..88c9ec6b98 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.h @@ -19,6 +19,7 @@ struct MouseEventInit : public EventModifierInit { double client_y = 0; i16 button = 0; + u16 buttons = 0; }; class MouseEvent : public UIEvent { @@ -26,7 +27,7 @@ class MouseEvent : public UIEvent { public: static MouseEvent* create(JS::Realm&, FlyString const& event_name, MouseEventInit const& event_init = {}); - static MouseEvent* create_from_platform_event(JS::Realm&, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button = 1); + static MouseEvent* create_from_platform_event(JS::Realm&, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned buttons, unsigned mouse_button = 1); virtual ~MouseEvent() override; @@ -40,6 +41,7 @@ public: double y() const { return client_y(); } i16 button() const { return m_button; } + u16 buttons() const { return m_buttons; } virtual u32 which() const override { return m_button + 1; } @@ -54,6 +56,7 @@ private: double m_client_x { 0 }; double m_client_y { 0 }; i16 m_button { 0 }; + u16 m_buttons { 0 }; }; } diff --git a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl index 2dd839ee9d..eece02e965 100644 --- a/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl +++ b/Userland/Libraries/LibWeb/UIEvents/MouseEvent.idl @@ -10,7 +10,7 @@ interface MouseEvent : UIEvent { readonly attribute double y; readonly attribute short button; - + readonly attribute unsigned short buttons; }; dictionary MouseEventInit : EventModifierInit { diff --git a/Userland/Libraries/LibWeb/UIEvents/WheelEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/WheelEvent.cpp index c2b434baf4..06c84d467d 100644 --- a/Userland/Libraries/LibWeb/UIEvents/WheelEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/WheelEvent.cpp @@ -28,13 +28,15 @@ WheelEvent* WheelEvent::create(JS::Realm& realm, FlyString const& event_name, Wh return realm.heap().allocate(realm, realm, event_name, event_init); } -WheelEvent* WheelEvent::create_from_platform_event(JS::Realm& realm, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, double delta_x, double delta_y) +WheelEvent* WheelEvent::create_from_platform_event(JS::Realm& realm, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, double delta_x, double delta_y, unsigned buttons, unsigned button) { WheelEventInit 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 = button; + event_init.buttons = buttons; event_init.delta_x = delta_x; event_init.delta_y = delta_y; event_init.delta_mode = WheelDeltaMode::DOM_DELTA_PIXEL; diff --git a/Userland/Libraries/LibWeb/UIEvents/WheelEvent.h b/Userland/Libraries/LibWeb/UIEvents/WheelEvent.h index a9c0b37860..7f7408fc7e 100644 --- a/Userland/Libraries/LibWeb/UIEvents/WheelEvent.h +++ b/Userland/Libraries/LibWeb/UIEvents/WheelEvent.h @@ -30,7 +30,7 @@ class WheelEvent final : public MouseEvent { public: static WheelEvent* create(JS::Realm&, FlyString const& event_name, WheelEventInit const& event_init = {}); - static WheelEvent* create_from_platform_event(JS::Realm&, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, double delta_x, double delta_y); + static WheelEvent* create_from_platform_event(JS::Realm&, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, double delta_x, double delta_y, unsigned buttons, unsigned button); virtual ~WheelEvent() override; diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 5d228d2a7a..a3c4cf811c 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -151,9 +151,9 @@ void ConnectionFromClient::flush_pending_paint_requests() m_pending_paint_requests.clear(); } -void ConnectionFromClient::mouse_down(Gfx::IntPoint const& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers) +void ConnectionFromClient::mouse_down(Gfx::IntPoint const& position, unsigned int button, unsigned int buttons, unsigned int modifiers) { - page().handle_mousedown(position, button, modifiers); + page().handle_mousedown(position, button, buttons, modifiers); } void ConnectionFromClient::mouse_move(Gfx::IntPoint const& position, [[maybe_unused]] unsigned int button, unsigned int buttons, unsigned int modifiers) @@ -161,19 +161,19 @@ void ConnectionFromClient::mouse_move(Gfx::IntPoint const& position, [[maybe_unu page().handle_mousemove(position, buttons, modifiers); } -void ConnectionFromClient::mouse_up(Gfx::IntPoint const& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers) +void ConnectionFromClient::mouse_up(Gfx::IntPoint const& position, unsigned int button, unsigned int buttons, unsigned int modifiers) { - page().handle_mouseup(position, button, modifiers); + page().handle_mouseup(position, button, buttons, modifiers); } -void ConnectionFromClient::mouse_wheel(Gfx::IntPoint const& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers, i32 wheel_delta_x, i32 wheel_delta_y) +void ConnectionFromClient::mouse_wheel(Gfx::IntPoint const& position, unsigned int button, unsigned int buttons, unsigned int modifiers, i32 wheel_delta_x, i32 wheel_delta_y) { - page().handle_mousewheel(position, button, modifiers, wheel_delta_x, wheel_delta_y); + page().handle_mousewheel(position, button, buttons, modifiers, wheel_delta_x, wheel_delta_y); } -void ConnectionFromClient::doubleclick(Gfx::IntPoint const& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers) +void ConnectionFromClient::doubleclick(Gfx::IntPoint const& position, unsigned int button, unsigned int buttons, unsigned int modifiers) { - page().handle_doubleclick(position, button, modifiers); + page().handle_doubleclick(position, button, buttons, modifiers); } void ConnectionFromClient::key_down(i32 key, unsigned int modifiers, u32 code_point)