LibWeb: Bring MouseEvent a bit closer to spec

This commit is contained in:
Igor Pissolati 2022-04-09 12:44:42 -03:00 committed by Andreas Kling
parent a2bc97a9c2
commit 1b94b4c593
Notes: sideshowbarker 2024-07-17 14:14:25 +09:00
6 changed files with 75 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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