Skip to content

Truly independent web browser

LibWeb: Set pressure for active mouse pointers

Andreas Kling7bf3de812ff8
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index d4d7cd02ad5..81e070d6267 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -3382,6 +3382,8 @@ void Document::set_hovered_node(GC::Ptr<Node> node, Optional<HoverEventData> hov
return HighResolutionTime::current_high_resolution_time(relevant_global_object(*this));
};
auto create_pointer_event = [&](Utf16FlyString const& event_name, UIEvents::PointerEventOptions options, CSSPixelPoint offset) {
+ // https://w3c.github.io/pointerevents/#dom-pointerevent-pressure
+ options.pressure = options.buttons ? UIEvents::PointerEvent::ACTIVE_PRESSURE_DEFAULT_IN_ACTIVE_BUTTON_STATE : 0;
auto utf8_name = event_name.to_utf16_string().to_utf8();
return UIEvents::PointerEvent::create(
MUST(FlyString::from_utf8(utf8_name.bytes_as_string_view())), move(options),
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 39cb890415e..eaf80c409f3 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -805,26 +805,26 @@ UIEvents::MouseButton Internals::button_from_unsigned_short(WebIDL::UnsignedShor
}
}
-void Internals::mouse_down(double x, double y, WebIDL::UnsignedShort click_count, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers)
+void Internals::mouse_down(double x, double y, WebIDL::UnsignedShort click_count, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers, Optional<WebIDL::UnsignedShort> buttons)
{
auto& page = this->page();
auto position = page.css_to_device_point({ x, y });
auto pressed_button = button_from_unsigned_short(button);
- page.handle_mousedown(position, position, pressed_button, pressed_button, modifiers, click_count);
+ page.handle_mousedown(position, position, pressed_button, buttons.value_or(pressed_button), modifiers, click_count);
}
-void Internals::mouse_up(double x, double y, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers)
+void Internals::mouse_up(double x, double y, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers, WebIDL::UnsignedShort buttons)
{
auto& page = this->page();
auto position = page.css_to_device_point({ x, y });
- page.handle_mouseup(position, position, button_from_unsigned_short(button), 0, modifiers);
+ page.handle_mouseup(position, position, button_from_unsigned_short(button), buttons, modifiers);
}
-void Internals::mouse_move(double x, double y, WebIDL::UnsignedShort modifiers)
+void Internals::mouse_move(double x, double y, WebIDL::UnsignedShort modifiers, WebIDL::UnsignedShort buttons)
{
auto& page = this->page();
auto position = page.css_to_device_point({ x, y });
- page.handle_mousemove(position, position, 0, modifiers);
+ page.handle_mousemove(position, position, buttons, modifiers);
}
void Internals::mouse_leave()
@@ -848,7 +848,7 @@ void Internals::click_and_hold(double x, double y, WebIDL::UnsignedShort click_c
auto& page = this->page();
auto position = page.css_to_device_point({ x, y });
auto mouse_button = button_from_unsigned_short(button);
- page.handle_mousedown(position, position, mouse_button, 0, modifiers, click_count);
+ page.handle_mousedown(position, position, mouse_button, mouse_button, modifiers, click_count);
}
void Internals::wheel(GC::Ref<WebIDL::Promise> promise, double x, double y, double delta_x, double delta_y, bool precise, Bindings::ScrollGesturePhase phase, Bindings::WheelTicks const& ticks)
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index 1c8e42b1dfd..4707292be98 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -108,9 +108,9 @@ public:
void commit_text();
// Low-level mouse primitives
- void mouse_down(double x, double y, WebIDL::UnsignedShort click_count, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers);
- void mouse_up(double x, double y, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers);
- void mouse_move(double x, double y, WebIDL::UnsignedShort modifiers);
+ void mouse_down(double x, double y, WebIDL::UnsignedShort click_count, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers, Optional<WebIDL::UnsignedShort> buttons = {});
+ void mouse_up(double x, double y, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers, WebIDL::UnsignedShort buttons = 0);
+ void mouse_move(double x, double y, WebIDL::UnsignedShort modifiers, WebIDL::UnsignedShort buttons = 0);
void mouse_leave();
void mouse_cancel();
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index c68f10a6070..f844e94a28b 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -86,9 +86,9 @@ interface Internals {
undefined commitText();
// Low-level mouse primitives
- undefined mouseDown(double x, double y, optional unsigned short clickCount = 1, optional unsigned short button = 0, optional unsigned short modifiers = 0);
- undefined mouseUp(double x, double y, optional unsigned short button = 0, optional unsigned short modifiers = 0);
- undefined mouseMove(double x, double y, optional unsigned short modifiers = 0);
+ undefined mouseDown(double x, double y, optional unsigned short clickCount = 1, optional unsigned short button = 0, optional unsigned short modifiers = 0, optional unsigned short buttons);
+ undefined mouseUp(double x, double y, optional unsigned short button = 0, optional unsigned short modifiers = 0, optional unsigned short buttons = 0);
+ undefined mouseMove(double x, double y, optional unsigned short modifiers = 0, optional unsigned short buttons = 0);
undefined mouseLeave();
undefined mouseCancel();
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index bccd65fa3b4..58a5c506425 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -1418,7 +1418,7 @@ EventResult EventHandler::handle_mousecancel()
// The values of the following properties of the pointercancel event MUST match the values of the last dispatched
// pointer event with the same pointerId: [...] and the coordinates inherited from Mouse Events.
auto const& last_position = *m_last_dispatched_pointer_event_position;
- dispatch_a_pointer_event_for_a_device_that_supports_hover(read, PointerEventType::PointerCancel, target, nullptr, last_position.coordinates, last_position.screen_position, {}, UIEvents::MouseButton::Primary, m_last_known_mouse_buttons, m_last_known_mouse_modifiers);
+ dispatch_a_pointer_event_for_a_device_that_supports_hover(read, PointerEventType::PointerCancel, target, nullptr, last_position.coordinates, last_position.screen_position, {}, UIEvents::MouseButton::Primary, m_buttons_of_last_dispatched_pointer_event, m_last_known_mouse_modifiers);
// NB: Dispatching an event may have disturbed the world.
if (m_navigable->active_document() != document)
diff --git a/Libraries/LibWeb/UIEvents/PointerEvent.cpp b/Libraries/LibWeb/UIEvents/PointerEvent.cpp
index b56742fd9c1..53013cb156d 100644
--- a/Libraries/LibWeb/UIEvents/PointerEvent.cpp
+++ b/Libraries/LibWeb/UIEvents/PointerEvent.cpp
@@ -56,6 +56,7 @@ WebIDL::ExceptionOr<GC::Ref<PointerEvent>> PointerEvent::create_from_platform_ev
event_options.pointer_id = mouse_pointer_id;
event_options.is_primary = true;
event_options.pointer_type = PointerTypes::Mouse;
+ event_options.pressure = event_name != EventNames::pointerup && buttons ? ACTIVE_PRESSURE_DEFAULT_IN_ACTIVE_BUTTON_STATE : 0;
if (movement.has_value()) {
event_options.movement_x = movement.value().x().to_double();
event_options.movement_y = movement.value().y().to_double();
@@ -85,6 +86,7 @@ WebIDL::ExceptionOr<GC::Ref<PointerEvent>> PointerEvent::create_from_platform_ev
event_options.pointer_id = mouse_pointer_id;
event_options.is_primary = true;
event_options.pointer_type = PointerTypes::Mouse;
+ event_options.pressure = event_name != EventNames::pointerup && buttons ? ACTIVE_PRESSURE_DEFAULT_IN_ACTIVE_BUTTON_STATE : 0;
if (movement.has_value()) {
event_options.movement_x = movement->x().to_double();
event_options.movement_y = movement->y().to_double();
diff --git a/Tests/LibWeb/Text/expected/UIEvents/mouse-pointer-pressure.txt b/Tests/LibWeb/Text/expected/UIEvents/mouse-pointer-pressure.txt
index 6287cb148f0..38954026ef8 100644
--- a/Tests/LibWeb/Text/expected/UIEvents/mouse-pointer-pressure.txt
+++ b/Tests/LibWeb/Text/expected/UIEvents/mouse-pointer-pressure.txt
@@ -1,23 +1,30 @@
pointerover on first: buttons=0, pressure=0
pointerenter on first: buttons=0, pressure=0
pointermove on first: buttons=0, pressure=0
-pointerdown on first: buttons=1, pressure=0
+pointerdown on first: buttons=1, pressure=0.5
pointerup on first: buttons=0, pressure=0
-pointerout on first: buttons=1, pressure=0
-pointerleave on first: buttons=1, pressure=0
-pointerover on second: buttons=1, pressure=0
-pointerenter on second: buttons=1, pressure=0
-pointerdown on second: buttons=1, pressure=0
+pointerout on first: buttons=1, pressure=0.5
+pointerleave on first: buttons=1, pressure=0.5
+pointerover on second: buttons=1, pressure=0.5
+pointerenter on second: buttons=1, pressure=0.5
+pointerdown on second: buttons=1, pressure=0.5
pointerup on second: buttons=0, pressure=0
pointerout on second: buttons=0, pressure=0
pointerleave on second: buttons=0, pressure=0
pointerover on first: buttons=0, pressure=0
pointerenter on first: buttons=0, pressure=0
pointermove on first: buttons=0, pressure=0
-pointerout on first: buttons=2, pressure=0
-pointerleave on first: buttons=2, pressure=0
-pointerover on second: buttons=2, pressure=0
-pointerenter on second: buttons=2, pressure=0
-pointerdown on second: buttons=2, pressure=0
+pointerout on first: buttons=2, pressure=0.5
+pointerleave on first: buttons=2, pressure=0.5
+pointerover on second: buttons=2, pressure=0.5
+pointerenter on second: buttons=2, pressure=0.5
+pointerdown on second: buttons=2, pressure=0.5
pointerup on second: buttons=0, pressure=0
+pointerdown on second: buttons=1, pressure=0.5
+pointerdown on second: buttons=3, pressure=0.5
+pointerup on second: buttons=2, pressure=0
Synthetic pointer pressure: 0
+Cancelled mouse pressure: 0.5
+Click pointerdown pressure: 0.5
+Click-and-hold pointerdown pressure: 0.5
+Held mouse move pressure: 0.5
diff --git a/Tests/LibWeb/Text/input/UIEvents/mouse-pointer-pressure.html b/Tests/LibWeb/Text/input/UIEvents/mouse-pointer-pressure.html
index 56162ac6c65..288bad24174 100644
--- a/Tests/LibWeb/Text/input/UIEvents/mouse-pointer-pressure.html
+++ b/Tests/LibWeb/Text/input/UIEvents/mouse-pointer-pressure.html
@@ -1,9 +1,19 @@
<!doctype html>
<script src="../include.js"></script>
<style>
- #first, #second { position: absolute; top: 0; width: 100px; height: 100px; }
- #first { left: 0; }
- #second { left: 100px; }
+ #first,
+ #second {
+ position: absolute;
+ top: 0;
+ width: 100px;
+ height: 100px;
+ }
+ #first {
+ left: 0;
+ }
+ #second {
+ left: 100px;
+ }
</style>
<div id="first"></div>
<div id="second"></div>
@@ -12,7 +22,15 @@
const first = document.getElementById("first");
const second = document.getElementById("second");
const events = [];
- const types = ["pointerover", "pointerenter", "pointerdown", "pointermove", "pointerup", "pointerout", "pointerleave"];
+ const types = [
+ "pointerover",
+ "pointerenter",
+ "pointerdown",
+ "pointermove",
+ "pointerup",
+ "pointerout",
+ "pointerleave",
+ ];
for (const target of [first, second]) {
for (const type of types) {
target.addEventListener(type, event => {
@@ -32,8 +50,44 @@
internals.mouseDown(110, 10, 1, 2);
internals.mouseUp(110, 10, 2);
- for (const event of events)
- println(event);
+ internals.mouseDown(110, 10);
+ internals.mouseDown(110, 10, 1, 2, 0, 3);
+ internals.mouseUp(110, 10, 0, 0, 2);
+ internals.mouseUp(110, 10, 2);
+
+ for (const event of events) println(event);
println(`Synthetic pointer pressure: ${new PointerEvent("pointerdown", { buttons: 1 }).pressure}`);
+
+ let lastDownPressure;
+ let lastMovePressure;
+ let lastCancelPressure;
+ second.addEventListener("pointerdown", event => {
+ lastDownPressure = event.pressure;
+ });
+ second.addEventListener("pointermove", event => {
+ lastMovePressure = event.pressure;
+ });
+ second.addEventListener("pointercancel", event => {
+ lastCancelPressure = event.pressure;
+ });
+
+ internals.mouseMove(110, 10);
+ internals.mouseDown(110, 10);
+ internals.mouseCancel();
+ println(`Cancelled mouse pressure: ${lastCancelPressure}`);
+
+ lastDownPressure = undefined;
+ internals.click(110, 10);
+ println(`Click pointerdown pressure: ${lastDownPressure}`);
+ lastDownPressure = undefined;
+ internals.clickAndHold(110, 10);
+ println(`Click-and-hold pointerdown pressure: ${lastDownPressure}`);
+ internals.mouseUp(110, 10);
+
+ internals.mouseDown(110, 10);
+ lastMovePressure = undefined;
+ internals.mouseMove(110, 10, 0, 1);
+ println(`Held mouse move pressure: ${lastMovePressure}`);
+ internals.mouseUp(110, 10);
});
</script>