diff --git a/examples/tui_keys.rs b/examples/tui_keys.rs index 4fde8c2c0..11a96a9b2 100644 --- a/examples/tui_keys.rs +++ b/examples/tui_keys.rs @@ -1,5 +1,7 @@ use dioxus::events::WheelEvent; use dioxus::prelude::*; +use dioxus_html::geometry::ScreenPoint; +use dioxus_html::input::MouseButtonSet; use dioxus_html::on::{KeyboardEvent, MouseEvent}; use dioxus_html::KeyCode; @@ -9,9 +11,9 @@ fn main() { fn app(cx: Scope) -> Element { let key = use_state(&cx, || "".to_string()); - let mouse = use_state(&cx, || (0, 0)); + let mouse = use_state(&cx, || ScreenPoint::zero()); let count = use_state(&cx, || 0); - let buttons = use_state(&cx, || 0); + let buttons = use_state(&cx, || MouseButtonSet::empty()); let mouse_clicked = use_state(&cx, || false); cx.render(rsx! { @@ -36,21 +38,21 @@ fn app(cx: Scope) -> Element { count.set(count + evt.data.delta_y as i64); }, ondrag: move |evt: MouseEvent| { - mouse.set((evt.data.screen_x, evt.data.screen_y)); + mouse.set(evt.data.screen_coordinates()); }, onmousedown: move |evt: MouseEvent| { - mouse.set((evt.data.screen_x, evt.data.screen_y)); - buttons.set(evt.data.buttons); + mouse.set(evt.data.screen_coordinates()); + buttons.set(evt.data.held_buttons()); mouse_clicked.set(true); }, onmouseup: move |evt: MouseEvent| { - buttons.set(evt.data.buttons); + buttons.set(evt.data.held_buttons()); mouse_clicked.set(false); }, "count: {count:?}", "key: {key}", - "mouse buttons: {buttons:b}", + "mouse buttons: {buttons:?}", "mouse pos: {mouse:?}", "mouse button pressed: {mouse_clicked}" } diff --git a/packages/html/src/web_sys_bind/events.rs b/packages/html/src/web_sys_bind/events.rs index d43130193..0bf1179e4 100644 --- a/packages/html/src/web_sys_bind/events.rs +++ b/packages/html/src/web_sys_bind/events.rs @@ -1,8 +1,11 @@ +use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint}; +use crate::input::{decode_mouse_button_set, MouseButton}; use crate::on::{ AnimationData, CompositionData, KeyboardData, MouseData, PointerData, TouchData, TransitionData, WheelData, }; use crate::KeyCode; +use keyboard_types::Modifiers; use wasm_bindgen::JsCast; use web_sys::{ AnimationEvent, CompositionEvent, Event, KeyboardEvent, MouseEvent, PointerEvent, TouchEvent, @@ -71,22 +74,32 @@ impl From<&KeyboardEvent> for KeyboardData { impl From<&MouseEvent> for MouseData { fn from(e: &MouseEvent) -> Self { - Self { - alt_key: e.alt_key(), - button: e.button(), - buttons: e.buttons(), - client_x: e.client_x(), - client_y: e.client_y(), - ctrl_key: e.ctrl_key(), - meta_key: e.meta_key(), - offset_x: e.offset_x(), - offset_y: e.offset_y(), - screen_x: e.screen_x(), - screen_y: e.screen_y(), - shift_key: e.shift_key(), - page_x: e.page_x(), - page_y: e.page_y(), + let mut modifiers = Modifiers::empty(); + + if e.alt_key() { + modifiers.insert(Modifiers::ALT); } + if e.ctrl_key() { + modifiers.insert(Modifiers::CONTROL); + } + if e.meta_key() { + modifiers.insert(Modifiers::META); + } + if e.shift_key() { + modifiers.insert(Modifiers::SHIFT); + } + + MouseData::new( + Coordinates::new( + ScreenPoint::new(e.screen_x().into(), e.screen_y().into()), + ClientPoint::new(e.client_x().into(), e.client_y().into()), + ElementPoint::new(e.offset_x().into(), e.offset_y().into()), + PagePoint::new(e.page_x().into(), e.page_y().into()), + ), + Some(MouseButton::from_web_code(e.button().into())), + decode_mouse_button_set(e.buttons()), + modifiers, + ) } }