Add offset coordinates to mouse events on TUI and desktop.

This commit is contained in:
Reinis Mazeiks 2022-05-04 16:42:14 +03:00
parent 96c178f91f
commit d84d414170
3 changed files with 14 additions and 0 deletions

View file

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

View file

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

View file

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