Browse Source

Refactor: move input types to separate module

Reinis Mazeiks 3 năm trước cách đây
mục cha
commit
2ef332085c
3 tập tin đã thay đổi với 61 bổ sung54 xóa
  1. 2 54
      packages/html/src/events.rs
  2. 57 0
      packages/html/src/input.rs
  3. 2 0
      packages/html/src/lib.rs

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

@@ -1,10 +1,10 @@
 use bumpalo::boxed::Box as BumpBox;
 use dioxus_core::exports::bumpalo;
 use dioxus_core::*;
-use enumset::EnumSetType;
 
 pub mod on {
-    use crate::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
+    use crate::geometry::{ClientPoint, ElementPoint, PagePoint, ScreenPoint};
+    use crate::input::{Modifier, ModifierSet, MouseButton, MouseButtonSet};
     use enumset::EnumSet;
     use std::collections::HashMap;
 
@@ -654,58 +654,6 @@ pub mod on {
         }
     }
 
-    // note: EnumSetType also derives Copy and Clone for some reason
-    #[derive(EnumSetType, Debug)]
-    pub enum Modifier {
-        Alt,
-        Ctrl,
-        Meta,
-        Shift,
-    }
-
-    pub type ModifierSet = EnumSet<Modifier>;
-
-    /// A mouse button type (such as Primary/Secondary)
-    // note: EnumSetType also derives Copy and Clone for some reason
-    #[derive(EnumSetType, Debug)]
-    #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
-    pub enum MouseButton {
-        /// Primary button (typically the left button)
-        Primary,
-        /// Secondary button (typically the right button)
-        Secondary,
-        /// Auxiliary button (typically the middle button)
-        Auxiliary,
-        /// Fourth button (typically the "Browser Back" button)
-        Fourth,
-        /// Fifth button (typically the "Browser Forward" button)
-        Fifth,
-        /// A button with an unknown code
-        Unknown,
-    }
-
-    impl MouseButton {
-        /// Constructs a MouseButton for the specified button code
-        ///
-        /// E.g. 0 => Primary; 1 => Auxiliary
-        ///
-        /// Unknown codes get mapped to MouseButton::Unknown.
-        pub fn from_web_code(code: i16) -> Self {
-            match code {
-                0 => MouseButton::Primary,
-                // not a typo; auxiliary and secondary are swapped unlike in the `buttons` field.
-                // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
-                1 => MouseButton::Auxiliary,
-                2 => MouseButton::Secondary,
-                3 => MouseButton::Fourth,
-                4 => MouseButton::Fifth,
-                _ => MouseButton::Unknown,
-            }
-        }
-    }
-
-    pub type MouseButtonSet = EnumSet<MouseButton>;
-
     pub type PointerEvent = UiEvent<PointerData>;
     #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
     #[derive(Debug, Clone)]

+ 57 - 0
packages/html/src/input.rs

@@ -0,0 +1,57 @@
+use enumset::{EnumSet, EnumSetType};
+
+// note: EnumSetType also derives Copy and Clone for some reason
+/// A modifier key, such as Alt or Ctrl
+#[derive(EnumSetType, Debug)]
+pub enum Modifier {
+    Alt,
+    Ctrl,
+    /// The meta key (windows key, or command key)
+    Meta,
+    Shift,
+}
+
+/// A set of modifier keys
+pub type ModifierSet = EnumSet<Modifier>;
+
+/// A mouse button type (such as Primary/Secondary)
+// note: EnumSetType also derives Copy and Clone for some reason
+#[derive(EnumSetType, Debug)]
+#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
+pub enum MouseButton {
+    /// Primary button (typically the left button)
+    Primary,
+    /// Secondary button (typically the right button)
+    Secondary,
+    /// Auxiliary button (typically the middle button)
+    Auxiliary,
+    /// Fourth button (typically the "Browser Back" button)
+    Fourth,
+    /// Fifth button (typically the "Browser Forward" button)
+    Fifth,
+    /// A button with an unknown code
+    Unknown,
+}
+
+impl MouseButton {
+    /// Constructs a MouseButton for the specified button code
+    ///
+    /// E.g. 0 => Primary; 1 => Auxiliary
+    ///
+    /// Unknown codes get mapped to MouseButton::Unknown.
+    pub fn from_web_code(code: i16) -> Self {
+        match code {
+            0 => MouseButton::Primary,
+            // not a typo; auxiliary and secondary are swapped unlike in the `buttons` field.
+            // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
+            1 => MouseButton::Auxiliary,
+            2 => MouseButton::Secondary,
+            3 => MouseButton::Fourth,
+            4 => MouseButton::Fifth,
+            _ => MouseButton::Unknown,
+        }
+    }
+}
+
+/// A set of mouse buttons
+pub type MouseButtonSet = EnumSet<MouseButton>;

+ 2 - 0
packages/html/src/lib.rs

@@ -17,9 +17,11 @@ mod elements;
 mod events;
 pub mod geometry;
 mod global_attributes;
+mod input;
 #[cfg(feature = "wasm-bind")]
 mod web_sys_bind;
 
 pub use elements::*;
 pub use events::*;
 pub use global_attributes::*;
+pub use input::*;