Evan Simkowitz 1 rok temu
rodzic
commit
e0f42f8173

+ 1 - 13
packages/html/src/events/mouse.rs

@@ -1,12 +1,11 @@
 use crate::point_interaction::{PointData, PointInteraction};
 use dioxus_core::Event;
-use std::fmt::{Debug, Formatter};
 
 pub type MouseEvent = Event<MouseData>;
 
 /// A synthetic event that wraps a web-style [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent)
 #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
-#[derive(Clone, Default, PartialEq, Eq)]
+#[derive(Debug, Clone, Default, PartialEq, Eq)]
 /// Data associated with a mouse event
 pub struct MouseData {
     /// Common data for all pointer/mouse events
@@ -89,14 +88,3 @@ impl PointInteraction for MouseData {
         self.point_data
     }
 }
-
-impl Debug for MouseData {
-    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
-        f.debug_struct("MouseData")
-            .field("coordinates", &self.coordinates())
-            .field("modifiers", &self.modifiers())
-            .field("held_buttons", &self.held_buttons())
-            .field("trigger_button", &self.trigger_button())
-            .finish()
-    }
-}

+ 125 - 49
packages/html/src/events/pointer.rs

@@ -1,25 +1,73 @@
-use std::fmt::{Debug, Formatter};
+use std::fmt::Debug;
 
 use dioxus_core::Event;
 
 use crate::point_interaction::{PointData, PointInteraction};
 
+/// A synthetic event that wraps a web-style [`PointerEvent`](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent)
 pub type PointerEvent = Event<PointerData>;
+
+/// Data associated with a pointer event, aside from the data shared with mouse events
+#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
+#[derive(Debug, Clone, PartialEq)]
+pub struct PointerEventData {
+    pub pointer_id: i32,
+    pub width: i32,
+    pub height: i32,
+    pub pressure: f32,
+    pub tangential_pressure: f32,
+    pub tilt_x: i32,
+    pub tilt_y: i32,
+    pub twist: i32,
+    pub pointer_type: String,
+    pub is_primary: bool,
+}
+
 #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
-#[derive(Clone, PartialEq)]
+#[derive(Debug, Clone, PartialEq)]
 pub struct PointerData {
     /// Common data for all pointer/mouse events
     #[cfg_attr(feature = "serialize", serde(flatten))]
     point_data: PointData,
+
+    /// The unique identifier of the pointer causing the event.
+    #[deprecated(since = "0.5.0", note = "use pointer_id() instead")]
     pub pointer_id: i32,
+
+    /// The width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer.
+    #[deprecated(since = "0.5.0", note = "use width() instead")]
     pub width: i32,
+
+    /// The height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer.
+    #[deprecated(since = "0.5.0", note = "use height() instead")]
     pub height: i32,
+
+    /// The normalized pressure of the pointer input in the range of 0 to 1,
+    #[deprecated(since = "0.5.0", note = "use pressure() instead")]
     pub pressure: f32,
+
+    /// The normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1,
+    #[deprecated(since = "0.5.0", note = "use tangential_pressure() instead")]
     pub tangential_pressure: f32,
+
+    /// The plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis.
+    #[deprecated(since = "0.5.0", note = "use tilt_x() instead")]
     pub tilt_x: i32,
+
+    /// The plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis.
+    #[deprecated(since = "0.5.0", note = "use tilt_y() instead")]
     pub tilt_y: i32,
+
+    /// The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.
+    #[deprecated(since = "0.5.0", note = "use twist() instead")]
     pub twist: i32,
+
+    /// Indicates the device type that caused the event (mouse, pen, touch, etc.).
+    #[deprecated(since = "0.5.0", note = "use pointer_type() instead")]
     pub pointer_type: String,
+
+    /// Indicates if the pointer represents the primary pointer of this pointer type.
+    #[deprecated(since = "0.5.0", note = "use is_primary() instead")]
     pub is_primary: bool,
 }
 
@@ -56,59 +104,87 @@ impl_event![
     onpointerout
 ];
 
-impl PointInteraction for PointerData {
-    fn get_point_data(&self) -> PointData {
-        self.point_data
-    }
-}
-
 impl PointerData {
-    pub fn new(
-        point_data: PointData,
-        pointer_id: i32,
-        width: i32,
-        height: i32,
-        pressure: f32,
-        tangential_pressure: f32,
-        tilt_x: i32,
-        tilt_y: i32,
-        twist: i32,
-        pointer_type: String,
-        is_primary: bool,
-    ) -> Self {
+    pub fn new(point_data: PointData, pointer_event_data: PointerEventData) -> Self {
+        #[allow(deprecated)]
         Self {
             point_data,
-            pointer_id,
-            width,
-            height,
-            pressure,
-            tangential_pressure,
-            tilt_x,
-            tilt_y,
-            twist,
-            pointer_type,
-            is_primary,
+            pointer_id: pointer_event_data.pointer_id,
+            width: pointer_event_data.width,
+            height: pointer_event_data.height,
+            pressure: pointer_event_data.pressure,
+            tangential_pressure: pointer_event_data.tangential_pressure,
+            tilt_x: pointer_event_data.tilt_x,
+            tilt_y: pointer_event_data.tilt_y,
+            twist: pointer_event_data.twist,
+            pointer_type: pointer_event_data.pointer_type,
+            is_primary: pointer_event_data.is_primary,
         }
     }
+
+    /// Gets the unique identifier of the pointer causing the event.
+    pub fn pointer_id(&self) -> i32 {
+        #[allow(deprecated)]
+        self.pointer_id
+    }
+
+    /// Gets the width (magnitude on the X axis), in CSS pixels, of the contact geometry of the pointer.
+    pub fn width(&self) -> i32 {
+        #[allow(deprecated)]
+        self.width
+    }
+
+    /// Gets the height (magnitude on the Y axis), in CSS pixels, of the contact geometry of the pointer.
+    pub fn height(&self) -> i32 {
+        #[allow(deprecated)]
+        self.height
+    }
+
+    /// Gets the normalized pressure of the pointer input in the range of 0 to 1,
+    pub fn pressure(&self) -> f32 {
+        #[allow(deprecated)]
+        self.pressure
+    }
+
+    /// Gets the normalized tangential pressure of the pointer input (also known as barrel pressure or cylinder stress) in the range -1 to 1,
+    pub fn tangential_pressure(&self) -> f32 {
+        #[allow(deprecated)]
+        self.tangential_pressure
+    }
+
+    /// Gets the plane angle (in degrees, in the range of -90 to 90) between the Y-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the Y axis.
+    pub fn tilt_x(&self) -> i32 {
+        #[allow(deprecated)]
+        self.tilt_x
+    }
+
+    /// Gets the plane angle (in degrees, in the range of -90 to 90) between the X-Z plane and the plane containing both the transducer (e.g. pen stylus) axis and the X axis.
+    pub fn tilt_y(&self) -> i32 {
+        #[allow(deprecated)]
+        self.tilt_y
+    }
+
+    /// Gets the clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.The clockwise rotation of the pointer (e.g. pen stylus) around its major axis in degrees, with a value in the range 0 to 359.
+    pub fn twist(&self) -> i32 {
+        #[allow(deprecated)]
+        self.twist
+    }
+
+    /// Gets the device type that caused the event (mouse, pen, touch, etc.).
+    pub fn pointer_type(&self) -> &str {
+        #[allow(deprecated)]
+        self.pointer_type.as_str()
+    }
+
+    /// Gets if the pointer represents the primary pointer of this pointer type.
+    pub fn is_primary(&self) -> bool {
+        #[allow(deprecated)]
+        self.is_primary
+    }
 }
 
-impl Debug for PointerData {
-    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
-        f.debug_struct("PointerData")
-            .field("coordinates", &self.coordinates())
-            .field("modifiers", &self.modifiers())
-            .field("held_buttons", &self.held_buttons())
-            .field("trigger_button", &self.trigger_button())
-            .field("pointer_id", &self.pointer_id)
-            .field("width", &self.width)
-            .field("height", &self.height)
-            .field("pressure", &self.pressure)
-            .field("tangential_pressure", &self.tangential_pressure)
-            .field("tilt_x", &self.tilt_x)
-            .field("tilt_y", &self.tilt_y)
-            .field("twist", &self.twist)
-            .field("pointer_type", &self.pointer_type)
-            .field("is_primary", &self.is_primary)
-            .finish()
+impl PointInteraction for PointerData {
+    fn get_point_data(&self) -> PointData {
+        self.point_data
     }
 }

+ 1 - 1
packages/html/src/lib.rs

@@ -22,7 +22,7 @@ mod global_attributes;
 pub mod input_data;
 #[cfg(feature = "native-bind")]
 pub mod native_bind;
-mod point_interaction;
+pub mod point_interaction;
 mod render_template;
 #[cfg(feature = "wasm-bind")]
 mod web_sys_bind;

+ 22 - 3
packages/html/src/point_interaction.rs

@@ -1,3 +1,5 @@
+use std::fmt::{Debug, Formatter};
+
 use keyboard_types::Modifiers;
 
 use crate::{
@@ -69,8 +71,8 @@ pub struct PointData {
 impl PointData {
     pub fn new(
         trigger_button: Option<MouseButton>,
-        coordinates: Coordinates,
         held_buttons: MouseButtonSet,
+        coordinates: Coordinates,
         modifiers: Modifiers,
     ) -> Self {
         let alt_key = modifiers.contains(Modifiers::ALT);
@@ -103,6 +105,23 @@ impl PointData {
     }
 }
 
+impl Debug for PointData {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        f.debug_struct("PointInteraction")
+            .field("coordinates", &self.coordinates())
+            .field("modifiers", &self.modifiers())
+            .field("held_buttons", &self.held_buttons())
+            .field("trigger_button", &self.trigger_button())
+            .finish()
+    }
+}
+
+impl PointInteraction for PointData {
+    fn get_point_data(&self) -> PointData {
+        *self
+    }
+}
+
 pub trait PointInteraction {
     fn get_point_data(&self) -> PointData;
 
@@ -160,7 +179,7 @@ pub trait PointInteraction {
         decode_mouse_button_set(self.get_point_data().buttons)
     }
 
-    fn trigger_button(&self) -> MouseButton {
-        MouseButton::from_web_code(self.get_point_data().button)
+    fn trigger_button(&self) -> Option<MouseButton> {
+        Some(MouseButton::from_web_code(self.get_point_data().button))
     }
 }

+ 13 - 11
packages/html/src/web_sys_bind/events.rs

@@ -4,7 +4,7 @@ use crate::events::{
 };
 use crate::input_data::decode_key_location;
 use crate::point_interaction::PointData;
-use crate::{DragData, MountedData};
+use crate::{DragData, MountedData, PointerEventData};
 use keyboard_types::{Code, Key, Modifiers};
 use std::convert::TryInto;
 use std::str::FromStr;
@@ -147,16 +147,18 @@ impl From<&PointerEvent> for PointerData {
                 page_x: e.page_x(),
                 page_y: e.page_y(),
             },
-            e.pointer_id(),
-            e.width(),
-            e.height(),
-            e.pressure(),
-            e.tangential_pressure(),
-            e.tilt_x(),
-            e.tilt_y(),
-            e.twist(),
-            e.pointer_type(),
-            e.is_primary(),
+            PointerEventData {
+                pointer_id: e.pointer_id(),
+                width: e.width(),
+                height: e.height(),
+                pressure: e.pressure(),
+                tangential_pressure: e.tangential_pressure(),
+                tilt_x: e.tilt_x(),
+                tilt_y: e.tilt_y(),
+                twist: e.twist(),
+                pointer_type: e.pointer_type(),
+                is_primary: e.is_primary(),
+            },
         )
     }
 }

+ 11 - 10
packages/rink/src/hooks.rs

@@ -13,6 +13,7 @@ use dioxus_html::geometry::{
 use dioxus_html::input_data::keyboard_types::{Code, Key, Location, Modifiers};
 use dioxus_html::input_data::MouseButtonSet as DioxusMouseButtons;
 use dioxus_html::input_data::{MouseButton as DioxusMouseButton, MouseButtonSet};
+use dioxus_html::point_interaction::{PointData, PointInteraction};
 use dioxus_html::{event_bubbles, FocusData, KeyboardData, MouseData, WheelData};
 use std::any::Any;
 use std::collections::HashMap;
@@ -133,12 +134,12 @@ impl InnerInputState {
                     _ => {}
                 }
 
-                let new_mouse_data = MouseData::new(
-                    m.coordinates(),
+                let new_mouse_data = MouseData::new(PointData::new(
                     m.trigger_button(),
                     held_buttons,
+                    m.coordinates(),
                     m.modifiers(),
-                );
+                ));
 
                 self.mouse = Some(new_mouse_data.clone());
                 *m = new_mouse_data;
@@ -291,12 +292,12 @@ impl InnerInputState {
                 mouse_data.page_coordinates(),
             );
 
-            MouseData::new(
-                coordinates,
+            MouseData::new(PointData::new(
                 mouse_data.trigger_button(),
                 mouse_data.held_buttons(),
+                coordinates,
                 mouse_data.modifiers(),
-            )
+            ))
         }
 
         if let Some(mouse_data) = &self.mouse {
@@ -715,12 +716,12 @@ fn get_event(evt: TermEvent) -> Option<(&'static str, EventData)> {
                 }
 
                 // held mouse buttons get set later by maintaining state, as crossterm does not provide them
-                EventData::Mouse(MouseData::new(
-                    coordinates,
+                EventData::Mouse(MouseData::new(PointData::new(
                     button,
-                    DioxusMouseButtons::empty(),
+                    MouseButtonSet::empty(),
+                    coordinates,
                     modifiers,
-                ))
+                )))
             };
 
             let get_wheel_data = |up| {

+ 3 - 1
packages/rink/src/widgets/slider.rs

@@ -1,6 +1,8 @@
 use std::collections::HashMap;
 
-use dioxus_html::{input_data::keyboard_types::Key, KeyboardData, MouseData};
+use dioxus_html::{
+    input_data::keyboard_types::Key, point_interaction::PointInteraction, KeyboardData, MouseData,
+};
 use dioxus_native_core::{
     custom_element::CustomElement,
     node::{OwnedAttributeDiscription, OwnedAttributeValue},

+ 3 - 1
packages/rink/src/widgets/text_like.rs

@@ -1,7 +1,9 @@
 use std::{collections::HashMap, io::stdout};
 
 use crossterm::{cursor::MoveTo, execute};
-use dioxus_html::{input_data::keyboard_types::Key, KeyboardData, MouseData};
+use dioxus_html::{
+    input_data::keyboard_types::Key, point_interaction::PointInteraction, KeyboardData, MouseData,
+};
 use dioxus_native_core::{
     custom_element::CustomElement,
     node::OwnedAttributeDiscription,