Skip to main content

decode_mouse_event

Function decode_mouse_event 

Source
pub fn decode_mouse_event(
    x: u16,
    y: u16,
    action: u8,
    button: u8,
    mods: u8,
) -> Option<MouseEvent>
Expand description

Decodes an (x, y, action, button, mods) tuple from JS into a retroglyph_core::event::MouseEvent.

Same pattern as decode_key_event: retroglyph_core::event::MouseEvent (and its MouseEventKind/MouseButton fields) are not wasm-bindgen FFI-safe types, so JS crosses the boundary with plain integers instead:

  • x, y: the cell-grid column/row the pointer is over, matching retroglyph_core::grid::Pos. JS is responsible for converting a raw pixel position (e.g. from a DOM MouseEvent) into cell coordinates using the terminal emulator’s own cell size, the same way it already tracks cols/rows for the wasm32-only wasm::wasm_terminal_resize. This backend has no sub-cell precision to report, so the returned event’s pixel_position is always None: the same convention retroglyph-crossterm uses for its own character-mode backend.
  • action: one of mouse_actions’s constants (DOWN, UP, MOVED, SCROLL_UP, SCROLL_DOWN).
  • button: which button the event applies to, one of mouse_buttons’s constants (LEFT, MIDDLE, RIGHT), matching the DOM MouseEvent.button convention JS already has on hand. Only consulted when action is DOWN or UP; ignored otherwise.
  • mods: the same bitmask layout as decode_key_event’s mods (SHIFT = 1, CONTROL = 2, ALT = 4, SUPER = 8).

Returns None if action doesn’t match a known mouse_actions constant, or if action is DOWN/UP and button doesn’t match a known mouse_buttons constant.

§Examples

use retroglyph_core::event::{MouseButton, MouseEventKind};
use retroglyph_terminal_wasm::{decode_mouse_event, mouse_actions, mouse_buttons};

let mouse = decode_mouse_event(3, 4, mouse_actions::DOWN, mouse_buttons::LEFT, 0).unwrap();
assert_eq!(mouse.kind, MouseEventKind::Down(MouseButton::Left));
assert_eq!(mouse.position, retroglyph_core::grid::Pos { x: 3, y: 4 });

// `button` is ignored for `MOVED`; an out-of-range value doesn't fail decoding.
let mouse = decode_mouse_event(1, 1, mouse_actions::MOVED, 0xFF, 0).unwrap();
assert_eq!(mouse.kind, MouseEventKind::Moved);

// An unknown `action` fails to decode.
assert!(decode_mouse_event(0, 0, 0xFF, mouse_buttons::LEFT, 0).is_none());