1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:24:57 +00:00

LibWeb: Add button property in MouseEvent

This commit is contained in:
Aliaksandr Kalenik 2022-10-20 20:56:38 +03:00 committed by Linus Groh
parent 501fb1cccb
commit 9cbf031b6d
10 changed files with 48 additions and 41 deletions

View file

@ -123,7 +123,7 @@ Painting::PaintableBox const* EventHandler::paint_root() const
return const_cast<Painting::PaintableBox*>(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<HTML::HTMLIFrameElement>(*node)) {
if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*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<HTML::HTMLIFrameElement>(*node)) {
if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*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<HTML::HTMLIFrameElement>(*node)) {
if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*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())

View file

@ -22,11 +22,11 @@ public:
explicit EventHandler(Badge<HTML::BrowsingContext>, 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);

View file

@ -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)

View file

@ -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);

View file

@ -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<MouseEvent>(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);
}

View file

@ -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 };
};
}

View file

@ -10,7 +10,7 @@ interface MouseEvent : UIEvent {
readonly attribute double y;
readonly attribute short button;
readonly attribute unsigned short buttons;
};
dictionary MouseEventInit : EventModifierInit {

View file

@ -28,13 +28,15 @@ WheelEvent* WheelEvent::create(JS::Realm& realm, FlyString const& event_name, Wh
return realm.heap().allocate<WheelEvent>(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;

View file

@ -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;

View file

@ -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)