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, matchingretroglyph_core::grid::Pos. JS is responsible for converting a raw pixel position (e.g. from a DOMMouseEvent) into cell coordinates using the terminal emulator’s own cell size, the same way it already trackscols/rowsfor thewasm32-onlywasm::wasm_terminal_resize. This backend has no sub-cell precision to report, so the returned event’spixel_positionis alwaysNone: the same conventionretroglyph-crosstermuses for its own character-mode backend.action: one ofmouse_actions’s constants (DOWN,UP,MOVED,SCROLL_UP,SCROLL_DOWN).button: which button the event applies to, one ofmouse_buttons’s constants (LEFT,MIDDLE,RIGHT), matching the DOMMouseEvent.buttonconvention JS already has on hand. Only consulted whenactionisDOWNorUP; ignored otherwise.mods: the same bitmask layout asdecode_key_event’smods(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());