11//! Event conversion utilities for WebAssembly.
22//!
3- //! This module provides functions to convert web-sys events (KeyboardEvent, MouseEvent)
3+ //! This module provides functions to convert web-sys events (` KeyboardEvent`, ` MouseEvent` )
44//! into cTUI's native Event types for seamless integration with the TUI framework.
55
66use ctui_core:: event:: {
77 Event , KeyCode , KeyEvent , KeyEventKind , KeyModifiers , MouseButton , MouseEvent ,
88} ;
99use web_sys:: { KeyboardEvent , MouseEvent as WebMouseEvent } ;
1010
11- /// Converts a web-sys KeyboardEvent to a cTUI Event.
11+ /// Converts a web-sys ` KeyboardEvent` to a cTUI ` Event` .
1212///
13- /// This function maps browser keyboard events to cTUI's KeyEvent structure,
13+ /// This function maps browser keyboard events to cTUI's ` KeyEvent` structure,
1414/// handling special keys, function keys, modifiers, and character keys.
1515///
1616/// # Arguments
1717///
18- /// * `js_event` - The web-sys KeyboardEvent from the browser
18+ /// * `js_event` - The web-sys ` KeyboardEvent` from the browser
1919///
2020/// # Returns
2121///
@@ -43,14 +43,14 @@ pub fn keyboard_event_to_key(js_event: &KeyboardEvent) -> Event {
4343 Event :: Key ( KeyEvent :: with_kind ( code, modifiers, kind) )
4444}
4545
46- /// Converts a web-sys MouseEvent to a cTUI Event.
46+ /// Converts a web-sys ` MouseEvent` to a cTUI ` Event` .
4747///
48- /// This function maps browser mouse events to cTUI's MouseEvent structure,
48+ /// This function maps browser mouse events to cTUI's ` MouseEvent` structure,
4949/// converting pixel coordinates to cell coordinates and mapping button states.
5050///
5151/// # Arguments
5252///
53- /// * `js_event` - The web-sys MouseEvent from the browser
53+ /// * `js_event` - The web-sys ` MouseEvent` from the browser
5454/// * `char_width` - Width of a single character cell in pixels
5555/// * `char_height` - Height of a single character cell in pixels
5656///
@@ -79,15 +79,18 @@ pub fn mouse_event_to_mouse(
7979 char_width : f64 ,
8080 char_height : f64 ,
8181) -> Event {
82- let column = ( js_event. client_x ( ) as f64 / char_width) . floor ( ) as u16 ;
83- let row = ( js_event. client_y ( ) as f64 / char_height) . floor ( ) as u16 ;
82+ // Intentional truncation: screen coordinates are always positive and fit in u16
83+ #[ allow( clippy:: cast_possible_truncation, clippy:: cast_sign_loss) ]
84+ let column = ( f64:: from ( js_event. client_x ( ) ) / char_width) . floor ( ) as u16 ;
85+ #[ allow( clippy:: cast_possible_truncation, clippy:: cast_sign_loss) ]
86+ let row = ( f64:: from ( js_event. client_y ( ) ) / char_height) . floor ( ) as u16 ;
8487 let button = map_mouse_button ( js_event. button ( ) ) ;
8588 let modifiers = extract_mouse_modifiers ( js_event) ;
8689
8790 Event :: Mouse ( MouseEvent :: new ( column, row, button, modifiers) )
8891}
8992
90- /// Maps a web-sys KeyboardEvent to a KeyCode.
93+ /// Maps a web-sys ` KeyboardEvent` to a ` KeyCode` .
9194///
9295/// Uses the `code` property for physical key identification (layout-independent)
9396/// and falls back to `key` for character keys.
@@ -114,7 +117,7 @@ fn map_key_code(event: &KeyboardEvent) -> KeyCode {
114117 "Backspace" => KeyCode :: Backspace ,
115118
116119 // Action keys
117- "Enter" => KeyCode :: Enter ,
120+ "Enter" | "NumpadEnter" => KeyCode :: Enter ,
118121 "Tab" => KeyCode :: Tab ,
119122 "Escape" => KeyCode :: Esc ,
120123
@@ -133,12 +136,11 @@ fn map_key_code(event: &KeyboardEvent) -> KeyCode {
133136 "F12" => KeyCode :: F ( 12 ) ,
134137
135138 // Numpad keys (treat as their primary equivalents)
136- "NumpadEnter" => KeyCode :: Enter ,
137- "NumpadAdd" => KeyCode :: Char ( '+' ) ,
138- "NumpadSubtract" => KeyCode :: Char ( '-' ) ,
139- "NumpadMultiply" => KeyCode :: Char ( '*' ) ,
140- "NumpadDivide" => KeyCode :: Char ( '/' ) ,
141- "NumpadDecimal" => KeyCode :: Char ( '.' ) ,
139+ "NumpadAdd" => KeyCode :: Char ( '+' ) ,
140+ "NumpadSubtract" => KeyCode :: Char ( '-' ) ,
141+ "NumpadMultiply" => KeyCode :: Char ( '*' ) ,
142+ "NumpadDivide" => KeyCode :: Char ( '/' ) ,
143+ "NumpadDecimal" => KeyCode :: Char ( '.' ) ,
142144 "Numpad0" => KeyCode :: Char ( '0' ) ,
143145 "Numpad1" => KeyCode :: Char ( '1' ) ,
144146 "Numpad2" => KeyCode :: Char ( '2' ) ,
@@ -170,7 +172,7 @@ fn map_key_code(event: &KeyboardEvent) -> KeyCode {
170172 }
171173}
172174
173- /// Extracts KeyModifiers from a web-sys KeyboardEvent.
175+ /// Extracts ` KeyModifiers` from a web-sys ` KeyboardEvent` .
174176fn extract_modifiers ( event : & KeyboardEvent ) -> KeyModifiers {
175177 KeyModifiers {
176178 shift : event. shift_key ( ) ,
@@ -184,24 +186,25 @@ fn extract_modifiers(event: &KeyboardEvent) -> KeyModifiers {
184186 }
185187}
186188
187- /// Maps a web-sys mouse button code to a MouseButton.
189+ /// Maps a web-sys mouse button code to a ` MouseButton` .
188190///
189191/// Web button codes:
190192/// - 0: Left button (primary)
191193/// - 1: Middle button (auxiliary)
192194/// - 2: Right button (secondary)
193195/// - 3: Back button (browser back)
194196/// - 4: Forward button (browser forward)
195- fn map_mouse_button ( button : i16 ) -> MouseButton {
197+ const fn map_mouse_button ( button : i16 ) -> MouseButton {
196198 match button {
197- 0 => MouseButton :: Left ,
199+ // Explicitly handle known button codes for documentation
198200 1 => MouseButton :: Middle ,
199201 2 => MouseButton :: Right ,
200- _ => MouseButton :: Left , // Default to left for unknown buttons
202+ // Default to Left for button code 0 and any unknown/future codes
203+ _ => MouseButton :: Left ,
201204 }
202205}
203206
204- /// Extracts KeyModifiers from a web-sys MouseEvent.
207+ /// Extracts ` KeyModifiers` from a web-sys ` MouseEvent` .
205208fn extract_mouse_modifiers ( event : & WebMouseEvent ) -> KeyModifiers {
206209 KeyModifiers {
207210 shift : event. shift_key ( ) ,
@@ -226,11 +229,14 @@ fn extract_mouse_modifiers(event: &WebMouseEvent) -> KeyModifiers {
226229///
227230/// # Returns
228231///
229- /// A cTUI `Event::Mouse` with MouseButton::WheelUp or WheelDown.
232+ /// A cTUI `Event::Mouse` with ` MouseButton::WheelUp` or ` WheelDown` .
230233#[ must_use]
231234pub fn wheel_event_to_scroll ( delta_y : f64 , char_width : f64 , char_height : f64 , x : i32 , y : i32 ) -> Event {
232- let column = ( x as f64 / char_width) . floor ( ) as u16 ;
233- let row = ( y as f64 / char_height) . floor ( ) as u16 ;
235+ // Intentional truncation: screen coordinates are always positive and fit in u16
236+ #[ allow( clippy:: cast_possible_truncation, clippy:: cast_sign_loss) ]
237+ let column = ( f64:: from ( x) / char_width) . floor ( ) as u16 ;
238+ #[ allow( clippy:: cast_possible_truncation, clippy:: cast_sign_loss) ]
239+ let row = ( f64:: from ( y) / char_height) . floor ( ) as u16 ;
234240 let button = if delta_y < 0.0 {
235241 MouseButton :: WheelUp
236242 } else {
0 commit comments