Pārlūkot izejas kodu

derive more traits for serialized types

Evan Almloff 1 gadu atpakaļ
vecāks
revīzija
93cbfd0dbc

+ 13 - 13
packages/html/Cargo.toml

@@ -16,7 +16,7 @@ serde = { version = "1", features = ["derive"], optional = true }
 serde_repr = { version = "0.1", optional = true }
 wasm-bindgen = { workspace = true, optional = true }
 euclid = "0.22.7"
-enumset = "1.0.11"
+enumset = "1.1.2"
 keyboard-types = "0.7"
 async-trait = "0.1.58"
 serde-value = "0.7.0"
@@ -29,18 +29,18 @@ serde_json = { version = "1", optional = true }
 optional = true
 version = "0.3.56"
 features = [
-     "TouchEvent",
-     "MouseEvent",
-     "InputEvent",
-     "ClipboardEvent",
-     "KeyboardEvent",
-     "TouchEvent",
-     "WheelEvent",
-     "AnimationEvent",
-     "TransitionEvent",
-     "PointerEvent",
-     "FocusEvent",
-     "CompositionEvent",
+    "TouchEvent",
+    "MouseEvent",
+    "InputEvent",
+    "ClipboardEvent",
+    "KeyboardEvent",
+    "TouchEvent",
+    "WheelEvent",
+    "AnimationEvent",
+    "TransitionEvent",
+    "PointerEvent",
+    "FocusEvent",
+    "CompositionEvent",
 ]
 
 [dev-dependencies]

+ 1 - 1
packages/html/src/events/animation.rs

@@ -50,7 +50,7 @@ impl PartialEq for AnimationData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of AnimationData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedAnimationData {
     animation_name: String,
     pseudo_element: String,

+ 1 - 1
packages/html/src/events/clipboard.rs

@@ -38,7 +38,7 @@ impl ClipboardData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of ClipboardData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedClipboardData {}
 
 #[cfg(feature = "serialize")]

+ 1 - 1
packages/html/src/events/composition.rs

@@ -38,7 +38,7 @@ impl CompositionData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of CompositionData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedCompositionData {
     data: String,
 }

+ 1 - 1
packages/html/src/events/drag.rs

@@ -99,7 +99,7 @@ impl DragData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of DragData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedDragData {
     mouse: crate::point_interaction::SerializedPointInteraction,
 }

+ 1 - 1
packages/html/src/events/focus.rs

@@ -33,7 +33,7 @@ impl FocusData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of FocusData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone, Default)]
 pub struct SerializedFocusData {}
 
 #[cfg(feature = "serialize")]

+ 2 - 2
packages/html/src/events/form.rs

@@ -78,7 +78,7 @@ pub trait HasFormData: std::any::Any {
 
 #[cfg(feature = "serialize")]
 /// A serialized form data object
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedFormData {
     value: String,
     values: HashMap<String, Vec<String>>,
@@ -172,7 +172,7 @@ impl<'de> serde::Deserialize<'de> for FormData {
 
 #[cfg(feature = "serialize")]
 /// A file engine that serializes files to bytes
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedFileEngine {
     files: HashMap<String, Vec<u8>>,
 }

+ 1 - 1
packages/html/src/events/image.rs

@@ -43,7 +43,7 @@ impl ImageData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of ImageData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedImageData {
     load_error: bool,
 }

+ 23 - 2
packages/html/src/events/keyboard.rs

@@ -1,6 +1,5 @@
 use dioxus_core::Event;
 use keyboard_types::{Code, Key, Location, Modifiers};
-use std::convert::TryInto;
 use std::fmt::Debug;
 
 #[cfg(feature = "serialize")]
@@ -80,7 +79,7 @@ impl KeyboardData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of KeyboardData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedKeyboardData {
     auto_repeating: bool,
     #[serde(deserialize_with = "resilient_deserialize_code")]
@@ -90,6 +89,26 @@ pub struct SerializedKeyboardData {
     modifiers: Modifiers,
 }
 
+#[cfg(feature = "serialize")]
+impl SerializedKeyboardData {
+    /// Create a new SerializedKeyboardData
+    pub fn new(
+        auto_repeating: bool,
+        code: Code,
+        key: Key,
+        location: Location,
+        modifiers: Modifiers,
+    ) -> Self {
+        Self {
+            auto_repeating,
+            code,
+            key,
+            location,
+            modifiers,
+        }
+    }
+}
+
 #[cfg(feature = "serialize")]
 impl From<&KeyboardData> for SerializedKeyboardData {
     fn from(data: &KeyboardData) -> Self {
@@ -186,6 +205,8 @@ impl<'de> serde::Deserialize<'de> for KeyCode {
     where
         D: serde::Deserializer<'de>,
     {
+        use std::convert::TryInto;
+
         // We could be deserializing a unicode character, so we need to use u64 even if the output only takes u8
         let value = u64::deserialize(deserializer)?;
 

+ 1 - 1
packages/html/src/events/media.rs

@@ -32,7 +32,7 @@ impl MediaData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of MediaData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedMediaData {}
 
 #[cfg(feature = "serialize")]

+ 85 - 3
packages/html/src/events/mouse.rs

@@ -106,6 +106,13 @@ impl_event! {
 }
 
 impl MouseData {
+    /// Create a new instance of MouseData
+    pub fn new(inner: impl HasMouseData + 'static) -> Self {
+        Self {
+            inner: Box::new(inner),
+        }
+    }
+
     /// Downcast this event to a concrete event type
     pub fn downcast<T: 'static>(&self) -> Option<&T> {
         self.inner.as_any().downcast_ref::<T>()
@@ -172,23 +179,98 @@ impl PartialEq for MouseData {
 }
 
 #[cfg(feature = "serialize")]
-impl HasMouseData for crate::point_interaction::SerializedPointInteraction {
+/// A serialized version of [`MouseData`]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone, Default)]
+pub struct SerializedMouseData {
+    /// Common data for all pointer/mouse events
+    #[serde(flatten)]
+    point_data: crate::point_interaction::SerializedPointInteraction,
+}
+
+#[cfg(feature = "serialize")]
+impl SerializedMouseData {
+    /// Create a new instance of SerializedMouseData
+    pub fn new(
+        client_coordinates: ClientPoint,
+        element_coordinates: ElementPoint,
+        page_coordinates: PagePoint,
+        screen_coordinates: ScreenPoint,
+        modifiers: Modifiers,
+        held_buttons: MouseButtonSet,
+        trigger_button: Option<MouseButton>,
+    ) -> Self {
+        Self {
+            point_data: crate::point_interaction::SerializedPointInteraction::new(
+                client_coordinates,
+                element_coordinates,
+                page_coordinates,
+                screen_coordinates,
+                modifiers,
+                held_buttons,
+                trigger_button,
+            ),
+        }
+    }
+}
+
+#[cfg(feature = "serialize")]
+impl From<&MouseData> for SerializedMouseData {
+    fn from(e: &MouseData) -> Self {
+        Self {
+            point_data: crate::point_interaction::SerializedPointInteraction::from(e),
+        }
+    }
+}
+
+#[cfg(feature = "serialize")]
+impl HasMouseData for SerializedMouseData {
     fn as_any(&self) -> &dyn std::any::Any {
         self
     }
 }
 
+#[cfg(feature = "serialize")]
+impl PointInteraction for SerializedMouseData {
+    fn client_coordinates(&self) -> ClientPoint {
+        self.point_data.client_coordinates()
+    }
+
+    fn element_coordinates(&self) -> ElementPoint {
+        self.point_data.element_coordinates()
+    }
+
+    fn page_coordinates(&self) -> PagePoint {
+        self.point_data.page_coordinates()
+    }
+
+    fn screen_coordinates(&self) -> ScreenPoint {
+        self.point_data.screen_coordinates()
+    }
+
+    fn modifiers(&self) -> Modifiers {
+        self.point_data.modifiers()
+    }
+
+    fn held_buttons(&self) -> MouseButtonSet {
+        self.point_data.held_buttons()
+    }
+
+    fn trigger_button(&self) -> Option<MouseButton> {
+        self.point_data.trigger_button()
+    }
+}
+
 #[cfg(feature = "serialize")]
 impl serde::Serialize for MouseData {
     fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
-        crate::point_interaction::SerializedPointInteraction::from(self).serialize(serializer)
+        SerializedMouseData::from(self).serialize(serializer)
     }
 }
 
 #[cfg(feature = "serialize")]
 impl<'de> serde::Deserialize<'de> for MouseData {
     fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
-        let data = crate::point_interaction::SerializedPointInteraction::deserialize(deserializer)?;
+        let data = SerializedMouseData::deserialize(deserializer)?;
         Ok(Self {
             inner: Box::new(data),
         })

+ 1 - 1
packages/html/src/events/pointer.rs

@@ -209,7 +209,7 @@ impl PointInteraction for PointerData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of PointerData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedPointerData {
     /// Common data for all pointer/mouse events
     #[serde(flatten)]

+ 1 - 1
packages/html/src/events/scroll.rs

@@ -33,7 +33,7 @@ impl PartialEq for ScrollData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of ScrollData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedScrollData {}
 
 #[cfg(feature = "serialize")]

+ 1 - 1
packages/html/src/events/selection.rs

@@ -33,7 +33,7 @@ impl PartialEq for SelectionData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of SelectionData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedSelectionData {}
 
 #[cfg(feature = "serialize")]

+ 1 - 1
packages/html/src/events/toggle.rs

@@ -33,7 +33,7 @@ impl ToggleData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of ToggleData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedToggleData {}
 
 #[cfg(feature = "serialize")]

+ 1 - 1
packages/html/src/events/touch.rs

@@ -60,7 +60,7 @@ impl TouchData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of TouchData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedTouchData {
     alt_key: bool,
     ctrl_key: bool,

+ 1 - 1
packages/html/src/events/transition.rs

@@ -39,7 +39,7 @@ impl TransitionData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of TransitionData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedTransitionData {
     property_name: String,
     pseudo_element: String,

+ 9 - 1
packages/html/src/events/wheel.rs

@@ -44,11 +44,19 @@ impl WheelData {
 
 #[cfg(feature = "serialize")]
 /// A serialized version of WheelData
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
 pub struct SerializedWheelData {
     delta: WheelDelta,
 }
 
+#[cfg(feature = "serialize")]
+impl SerializedWheelData {
+    /// Create a new SerializedWheelData
+    pub fn new(delta: WheelDelta) -> Self {
+        Self { delta }
+    }
+}
+
 #[cfg(feature = "serialize")]
 impl From<&WheelData> for SerializedWheelData {
     fn from(data: &WheelData) -> Self {

+ 25 - 62
packages/html/src/point_interaction.rs

@@ -5,67 +5,6 @@ use crate::{
     input_data::{MouseButton, MouseButtonSet},
 };
 
-// #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
-// #[derive(Copy, Clone, Default, PartialEq, Eq)]
-// pub struct PointData {
-//     pub alt_key: bool,
-
-//     /// The button number that was pressed (if applicable) when the mouse event was fired.
-//     pub button: i16,
-
-//     /// Indicates which buttons are pressed on the mouse (or other input device) when a mouse event is triggered.
-//     ///
-//     /// Each button that can be pressed is represented by a given number (see below). If more than one button is pressed, the button values are added together to produce a new number. For example, if the secondary (2) and auxiliary (4) buttons are pressed simultaneously, the value is 6 (i.e., 2 + 4).
-//     ///
-//     /// - 1: Primary button (usually the left button)
-//     /// - 2: Secondary button (usually the right button)
-//     /// - 4: Auxiliary button (usually the mouse wheel button or middle button)
-//     /// - 8: 4th button (typically the "Browser Back" button)
-//     /// - 16 : 5th button (typically the "Browser Forward" button)
-//     pub buttons: u16,
-
-//     /// The horizontal coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page).
-//     ///
-//     /// For example, clicking on the left edge of the viewport will always result in a mouse event with a clientX value of 0, regardless of whether the page is scrolled horizontally.
-//     pub client_x: i32,
-
-//     /// The vertical coordinate within the application's viewport at which the event occurred (as opposed to the coordinate within the page).
-//     ///
-//     /// For example, clicking on the top edge of the viewport will always result in a mouse event with a clientY value of 0, regardless of whether the page is scrolled vertically.
-//     pub client_y: i32,
-
-//     /// True if the control key was down when the mouse event was fired.
-//     pub ctrl_key: bool,
-
-//     /// True if the meta key was down when the mouse event was fired.
-//     pub meta_key: bool,
-
-//     /// The offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node.
-//     pub offset_x: i32,
-
-//     /// The offset in the Y coordinate of the mouse pointer between that event and the padding edge of the target node.
-//     pub offset_y: i32,
-
-//     /// The X (horizontal) coordinate (in pixels) of the mouse, relative to the left edge of the entire document. This includes any portion of the document not currently visible.
-//     ///
-//     /// Being based on the edge of the document as it is, this property takes into account any horizontal scrolling of the page. For example, if the page is scrolled such that 200 pixels of the left side of the document are scrolled out of view, and the mouse is clicked 100 pixels inward from the left edge of the view, the value returned by pageX will be 300.
-//     pub page_x: i32,
-
-//     /// The Y (vertical) coordinate in pixels of the event relative to the whole document.
-//     ///
-//     /// See `page_x`.
-//     pub page_y: i32,
-
-//     /// The X coordinate of the mouse pointer in global (screen) coordinates.
-//     pub screen_x: i32,
-
-//     /// The Y coordinate of the mouse pointer in global (screen) coordinates.
-//     pub screen_y: i32,
-
-//     /// True if the shift key was down when the mouse event was fired.
-//     pub shift_key: bool,
-// }
-
 pub trait PointInteraction: std::any::Any {
     /// Gets the coordinates of the pointer event.
     fn coordinates(&self) -> Coordinates {
@@ -100,7 +39,7 @@ pub trait PointInteraction: std::any::Any {
 }
 
 #[cfg(feature = "serialize")]
-#[derive(serde::Serialize, serde::Deserialize)]
+#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone, Default)]
 pub(crate) struct SerializedPointInteraction {
     pub(crate) client_coordinates: ClientPoint,
     pub(crate) element_coordinates: ElementPoint,
@@ -111,6 +50,30 @@ pub(crate) struct SerializedPointInteraction {
     pub(crate) trigger_button: Option<MouseButton>,
 }
 
+#[cfg(feature = "serialize")]
+impl SerializedPointInteraction {
+    /// Create a new serialized point interaction.
+    pub fn new(
+        client_coordinates: ClientPoint,
+        element_coordinates: ElementPoint,
+        page_coordinates: PagePoint,
+        screen_coordinates: ScreenPoint,
+        modifiers: Modifiers,
+        held_buttons: MouseButtonSet,
+        trigger_button: Option<MouseButton>,
+    ) -> Self {
+        Self {
+            client_coordinates,
+            element_coordinates,
+            page_coordinates,
+            screen_coordinates,
+            modifiers,
+            held_buttons,
+            trigger_button,
+        }
+    }
+}
+
 #[cfg(feature = "serialize")]
 impl<E: PointInteraction> From<&E> for SerializedPointInteraction {
     fn from(data: &E) -> Self {

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

@@ -189,7 +189,7 @@ impl EventData {
 fn test_back_and_forth() {
     let data = HtmlEvent {
         element: ElementId(0),
-        data: EventData::Mouse(MouseData::default()),
+        data: EventData::Mouse(SerializedMouseData::default().into()),
         name: "click".to_string(),
         bubbles: true,
     };