From d84d414170b67f12fa5ada55b9a8f406ba5206f3 Mon Sep 17 00:00:00 2001 From: Reinis Mazeiks Date: Wed, 4 May 2022 16:42:14 +0300 Subject: [PATCH] Add offset coordinates to mouse events on TUI and desktop. --- packages/html/src/events.rs | 4 ++++ packages/interpreter/src/interpreter.js | 4 ++++ packages/tui/src/hooks.rs | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/packages/html/src/events.rs b/packages/html/src/events.rs index bee91f4d6..cfa901a2e 100644 --- a/packages/html/src/events.rs +++ b/packages/html/src/events.rs @@ -523,6 +523,10 @@ pub mod on { pub ctrl_key: bool, /// True if the meta key was down when the mouse event was fired. pub meta_key: bool, + /// The offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node. + pub offset_x: i32, + /// The offset in the Y coordinate of the mouse pointer between that event and the padding edge of the target node. + pub offset_y: i32, /// The X (horizontal) coordinate (in pixels) of the mouse, relative to the left edge of the entire document. This includes any portion of the document not currently visible. /// /// Being based on the edge of the document as it is, this property takes into account any horizontal scrolling of the page. For example, if the page is scrolled such that 200 pixels of the left side of the document are scrolled out of view, and the mouse is clicked 100 pixels inward from the left edge of the view, the value returned by pageX will be 300. diff --git a/packages/interpreter/src/interpreter.js b/packages/interpreter/src/interpreter.js index 4725fff31..a908ad74c 100644 --- a/packages/interpreter/src/interpreter.js +++ b/packages/interpreter/src/interpreter.js @@ -409,6 +409,8 @@ export function serialize_event(event) { clientY, ctrlKey, metaKey, + offsetX, + offsetY, pageX, pageY, screenX, @@ -423,6 +425,8 @@ export function serialize_event(event) { client_y: clientY, ctrl_key: ctrlKey, meta_key: metaKey, + offset_x: offsetX, + offset_y: offsetY, page_x: pageX, page_y: pageY, screen_x: screenX, diff --git a/packages/tui/src/hooks.rs b/packages/tui/src/hooks.rs index 8da04fc5e..04d320fcb 100644 --- a/packages/tui/src/hooks.rs +++ b/packages/tui/src/hooks.rs @@ -602,6 +602,10 @@ fn get_event(evt: TermEvent) -> Option<(&'static str, EventData)> { Some(MouseButton::Right) => 2, }; // from https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent + + // The `offset`, `page` and `screen` coordinates are inconsistent with the MDN definition, as they are relative to the viewport (client), not the target element/page/screen, respectively. + // todo? + // But then, MDN defines them in terms of pixels, yet crossterm provides only row/column, and it might not be possible to get pixels. So we can't get 100% consistency anyway. EventData::Mouse(MouseData { alt_key: alt, button: button_state, @@ -610,6 +614,8 @@ fn get_event(evt: TermEvent) -> Option<(&'static str, EventData)> { client_y: y, ctrl_key: ctrl, meta_key: meta, + offset_x: x, + offset_y: y, page_x: x, page_y: y, screen_x: x,