1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 06:17:34 +00:00

Ladybird+LibWeb: Add MouseEvent screenX and screenY support

This commit is contained in:
Bastiaan van der Plaat 2023-09-08 18:48:44 +02:00 committed by Alexander Kalenik
parent e584189b8f
commit 836a7b00dd
19 changed files with 125 additions and 103 deletions

View file

@ -16,6 +16,7 @@ namespace Ladybird {
struct MouseEvent {
Gfx::IntPoint position {};
Gfx::IntPoint screen_position {};
GUI::MouseButton button { GUI::MouseButton::Primary };
KeyModifier modifiers { KeyModifier::Mod_None };
};

View file

@ -39,9 +39,10 @@ static KeyModifier ns_modifiers_to_key_modifiers(NSEventModifierFlags modifier_f
MouseEvent ns_event_to_mouse_event(NSEvent* event, NSView* view, GUI::MouseButton button)
{
auto position = [view convertPoint:event.locationInWindow fromView:nil];
auto screen_position = [NSEvent mouseLocation];
auto modifiers = ns_modifiers_to_key_modifiers(event.modifierFlags, button);
return { ns_point_to_gfx_point(position), button, modifiers };
return { ns_point_to_gfx_point(position), ns_point_to_gfx_point(screen_position), button, modifiers };
}
NSEvent* create_context_menu_mouse_event(NSView* view, Gfx::IntPoint position)

View file

@ -956,58 +956,58 @@ static void copy_text_to_clipboard(StringView text)
- (void)mouseMoved:(NSEvent*)event
{
auto [position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::None);
m_web_view_bridge->mouse_move_event(position, button, modifiers);
auto [position, screen_position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::None);
m_web_view_bridge->mouse_move_event(position, screen_position, button, modifiers);
}
- (void)mouseDown:(NSEvent*)event
{
[[self window] makeFirstResponder:self];
auto [position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Primary);
auto [position, screen_position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Primary);
if (event.clickCount % 2 == 0) {
m_web_view_bridge->mouse_double_click_event(position, button, modifiers);
m_web_view_bridge->mouse_double_click_event(position, screen_position, button, modifiers);
} else {
m_web_view_bridge->mouse_down_event(position, button, modifiers);
m_web_view_bridge->mouse_down_event(position, screen_position, button, modifiers);
}
}
- (void)mouseUp:(NSEvent*)event
{
auto [position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Primary);
m_web_view_bridge->mouse_up_event(position, button, modifiers);
auto [position, screen_position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Primary);
m_web_view_bridge->mouse_up_event(position, screen_position, button, modifiers);
}
- (void)mouseDragged:(NSEvent*)event
{
auto [position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Primary);
m_web_view_bridge->mouse_move_event(position, button, modifiers);
auto [position, screen_position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Primary);
m_web_view_bridge->mouse_move_event(position, screen_position, button, modifiers);
}
- (void)rightMouseDown:(NSEvent*)event
{
[[self window] makeFirstResponder:self];
auto [position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Secondary);
auto [position, screen_position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Secondary);
if (event.clickCount % 2 == 0) {
m_web_view_bridge->mouse_double_click_event(position, button, modifiers);
m_web_view_bridge->mouse_double_click_event(position, screen_position, button, modifiers);
} else {
m_web_view_bridge->mouse_down_event(position, button, modifiers);
m_web_view_bridge->mouse_down_event(position, screen_position, button, modifiers);
}
}
- (void)rightMouseUp:(NSEvent*)event
{
auto [position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Secondary);
m_web_view_bridge->mouse_up_event(position, button, modifiers);
auto [position, screen_position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Secondary);
m_web_view_bridge->mouse_up_event(position, screen_position, button, modifiers);
}
- (void)rightMouseDragged:(NSEvent*)event
{
auto [position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Secondary);
m_web_view_bridge->mouse_move_event(position, button, modifiers);
auto [position, screen_position, button, modifiers] = Ladybird::ns_event_to_mouse_event(event, self, GUI::MouseButton::Secondary);
m_web_view_bridge->mouse_move_event(position, screen_position, button, modifiers);
}
- (void)keyDown:(NSEvent*)event

View file

@ -100,24 +100,24 @@ void WebViewBridge::set_preferred_color_scheme(Web::CSS::PreferredColorScheme co
client().async_set_preferred_color_scheme(color_scheme);
}
void WebViewBridge::mouse_down_event(Gfx::IntPoint position, GUI::MouseButton button, KeyModifier modifiers)
void WebViewBridge::mouse_down_event(Gfx::IntPoint position, Gfx::IntPoint screen_position, GUI::MouseButton button, KeyModifier modifiers)
{
client().async_mouse_down(to_content_position(position), to_underlying(button), to_underlying(button), modifiers);
client().async_mouse_down(to_content_position(position), screen_position, to_underlying(button), to_underlying(button), modifiers);
}
void WebViewBridge::mouse_up_event(Gfx::IntPoint position, GUI::MouseButton button, KeyModifier modifiers)
void WebViewBridge::mouse_up_event(Gfx::IntPoint position, Gfx::IntPoint screen_position, GUI::MouseButton button, KeyModifier modifiers)
{
client().async_mouse_up(to_content_position(position), to_underlying(button), to_underlying(button), modifiers);
client().async_mouse_up(to_content_position(position), screen_position, to_underlying(button), to_underlying(button), modifiers);
}
void WebViewBridge::mouse_move_event(Gfx::IntPoint position, GUI::MouseButton button, KeyModifier modifiers)
void WebViewBridge::mouse_move_event(Gfx::IntPoint position, Gfx::IntPoint screen_position, GUI::MouseButton button, KeyModifier modifiers)
{
client().async_mouse_move(to_content_position(position), 0, to_underlying(button), modifiers);
client().async_mouse_move(to_content_position(position), screen_position, 0, to_underlying(button), modifiers);
}
void WebViewBridge::mouse_double_click_event(Gfx::IntPoint position, GUI::MouseButton button, KeyModifier modifiers)
void WebViewBridge::mouse_double_click_event(Gfx::IntPoint position, Gfx::IntPoint screen_position, GUI::MouseButton button, KeyModifier modifiers)
{
client().async_doubleclick(to_content_position(position), button, to_underlying(button), modifiers);
client().async_doubleclick(to_content_position(position), screen_position, button, to_underlying(button), modifiers);
}
void WebViewBridge::key_down_event(KeyCode key_code, KeyModifier modifiers, u32 code_point)

View file

@ -39,10 +39,10 @@ public:
void update_palette();
void set_preferred_color_scheme(Web::CSS::PreferredColorScheme);
void mouse_down_event(Gfx::IntPoint, GUI::MouseButton, KeyModifier);
void mouse_up_event(Gfx::IntPoint, GUI::MouseButton, KeyModifier);
void mouse_move_event(Gfx::IntPoint, GUI::MouseButton, KeyModifier);
void mouse_double_click_event(Gfx::IntPoint, GUI::MouseButton, KeyModifier);
void mouse_down_event(Gfx::IntPoint, Gfx::IntPoint, GUI::MouseButton, KeyModifier);
void mouse_up_event(Gfx::IntPoint, Gfx::IntPoint, GUI::MouseButton, KeyModifier);
void mouse_move_event(Gfx::IntPoint, Gfx::IntPoint, GUI::MouseButton, KeyModifier);
void mouse_double_click_event(Gfx::IntPoint, Gfx::IntPoint, GUI::MouseButton, KeyModifier);
void key_down_event(KeyCode, KeyModifier, u32);
void key_up_event(KeyCode, KeyModifier, u32);

View file

@ -325,13 +325,14 @@ void WebContentView::wheelEvent(QWheelEvent* event)
{
if (!event->modifiers().testFlag(Qt::ControlModifier)) {
Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
Gfx::IntPoint screen_position(event->globalPosition().x() / m_inverse_pixel_scaling_ratio, event->globalPosition().y() / m_inverse_pixel_scaling_ratio);
auto button = get_button_from_qt_event(*event);
auto buttons = get_buttons_from_qt_event(*event);
auto modifiers = get_modifiers_from_qt_mouse_event(*event);
auto num_pixels = -event->pixelDelta();
if (!num_pixels.isNull()) {
client().async_mouse_wheel(to_content_position(position), button, buttons, modifiers, num_pixels.x(), num_pixels.y());
client().async_mouse_wheel(to_content_position(position), screen_position, button, buttons, modifiers, num_pixels.x(), num_pixels.y());
} else {
auto num_degrees = -event->angleDelta();
float delta_x = -num_degrees.x() / 120;
@ -339,7 +340,7 @@ void WebContentView::wheelEvent(QWheelEvent* event)
auto step_x = delta_x * QApplication::wheelScrollLines() * devicePixelRatio();
auto step_y = delta_y * QApplication::wheelScrollLines() * devicePixelRatio();
int scroll_step_size = verticalScrollBar()->singleStep();
client().async_mouse_wheel(to_content_position(position), button, buttons, modifiers, step_x * scroll_step_size, step_y * scroll_step_size);
client().async_mouse_wheel(to_content_position(position), screen_position, button, buttons, modifiers, step_x * scroll_step_size, step_y * scroll_step_size);
}
event->accept();
return;
@ -350,14 +351,16 @@ void WebContentView::wheelEvent(QWheelEvent* event)
void WebContentView::mouseMoveEvent(QMouseEvent* event)
{
Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
Gfx::IntPoint screen_position(event->globalPosition().x() / m_inverse_pixel_scaling_ratio, event->globalPosition().y() / m_inverse_pixel_scaling_ratio);
auto buttons = get_buttons_from_qt_event(*event);
auto modifiers = get_modifiers_from_qt_mouse_event(*event);
client().async_mouse_move(to_content_position(position), 0, buttons, modifiers);
client().async_mouse_move(to_content_position(position), screen_position, 0, buttons, modifiers);
}
void WebContentView::mousePressEvent(QMouseEvent* event)
{
Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
Gfx::IntPoint screen_position(event->globalPosition().x() / m_inverse_pixel_scaling_ratio, event->globalPosition().y() / m_inverse_pixel_scaling_ratio);
auto button = get_button_from_qt_event(*event);
if (button == 0) {
// We could not convert Qt buttons to something that Lagom can
@ -367,12 +370,13 @@ void WebContentView::mousePressEvent(QMouseEvent* event)
}
auto modifiers = get_modifiers_from_qt_mouse_event(*event);
auto buttons = get_buttons_from_qt_event(*event);
client().async_mouse_down(to_content_position(position), button, buttons, modifiers);
client().async_mouse_down(to_content_position(position), screen_position, button, buttons, modifiers);
}
void WebContentView::mouseReleaseEvent(QMouseEvent* event)
{
Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
Gfx::IntPoint screen_position(event->globalPosition().x() / m_inverse_pixel_scaling_ratio, event->globalPosition().y() / m_inverse_pixel_scaling_ratio);
auto button = get_button_from_qt_event(*event);
if (event->button() & Qt::MouseButton::BackButton) {
@ -391,12 +395,13 @@ void WebContentView::mouseReleaseEvent(QMouseEvent* event)
}
auto modifiers = get_modifiers_from_qt_mouse_event(*event);
auto buttons = get_buttons_from_qt_event(*event);
client().async_mouse_up(to_content_position(position), button, buttons, modifiers);
client().async_mouse_up(to_content_position(position), screen_position, button, buttons, modifiers);
}
void WebContentView::mouseDoubleClickEvent(QMouseEvent* event)
{
Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
Gfx::IntPoint screen_position(event->globalPosition().x() / m_inverse_pixel_scaling_ratio, event->globalPosition().y() / m_inverse_pixel_scaling_ratio);
auto button = get_button_from_qt_event(*event);
if (button == 0) {
// We could not convert Qt buttons to something that Lagom can
@ -406,7 +411,7 @@ void WebContentView::mouseDoubleClickEvent(QMouseEvent* event)
}
auto modifiers = get_modifiers_from_qt_mouse_event(*event);
auto buttons = get_buttons_from_qt_event(*event);
client().async_doubleclick(to_content_position(position), button, buttons, modifiers);
client().async_doubleclick(to_content_position(position), screen_position, button, buttons, modifiers);
}
void WebContentView::dragEnterEvent(QDragEnterEvent* event)