2020-11-21 18:32:39 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2022-08-08 22:29:40 +02:00
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
2020-11-21 18:32:39 +00:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-21 18:32:39 +00:00
|
|
|
*/
|
|
|
|
|
|
2022-04-09 12:44:42 -03:00
|
|
|
#include <LibGUI/Event.h>
|
2020-11-21 19:15:57 +00:00
|
|
|
#include <LibWeb/HTML/EventNames.h>
|
2022-08-28 13:42:07 +02:00
|
|
|
#include <LibWeb/HTML/Window.h>
|
2020-11-21 19:15:57 +00:00
|
|
|
#include <LibWeb/UIEvents/EventNames.h>
|
2020-11-21 18:32:39 +00:00
|
|
|
#include <LibWeb/UIEvents/MouseEvent.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::UIEvents {
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
MouseEvent::MouseEvent(HTML::Window& window_object, FlyString const& event_name, MouseEventInit const& event_init)
|
2022-08-08 22:29:40 +02:00
|
|
|
: UIEvent(window_object, event_name, event_init)
|
2022-04-09 12:44:42 -03:00
|
|
|
, 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)
|
2020-11-21 18:32:39 +00:00
|
|
|
{
|
2022-09-03 18:43:24 +02:00
|
|
|
set_prototype(&window_object.cached_web_prototype("MouseEvent"));
|
2020-11-21 18:32:39 +00:00
|
|
|
set_event_characteristics();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 22:29:40 +02:00
|
|
|
MouseEvent::~MouseEvent() = default;
|
|
|
|
|
|
2022-04-09 12:44:42 -03:00
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
MouseEvent* MouseEvent::create(HTML::Window& window_object, FlyString const& event_name, MouseEventInit const& event_init)
|
2022-08-08 22:29:40 +02:00
|
|
|
{
|
|
|
|
|
return window_object.heap().allocate<MouseEvent>(window_object.realm(), window_object, event_name, event_init);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
MouseEvent* MouseEvent::create_from_platform_event(HTML::Window& window_object, FlyString const& event_name, double offset_x, double offset_y, double client_x, double client_y, unsigned mouse_button)
|
2022-04-09 12:44:42 -03:00
|
|
|
{
|
|
|
|
|
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);
|
2022-08-08 22:29:40 +02:00
|
|
|
return MouseEvent::create(window_object, event_name, event_init);
|
2022-04-09 12:44:42 -03:00
|
|
|
}
|
2020-11-21 18:32:39 +00:00
|
|
|
|
|
|
|
|
void MouseEvent::set_event_characteristics()
|
|
|
|
|
{
|
2020-11-21 19:15:57 +00:00
|
|
|
if (type().is_one_of(EventNames::mousedown, EventNames::mousemove, EventNames::mouseout, EventNames::mouseover, EventNames::mouseup, HTML::EventNames::click)) {
|
2020-11-21 18:32:39 +00:00
|
|
|
set_bubbles(true);
|
|
|
|
|
set_cancelable(true);
|
|
|
|
|
set_composed(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|