From 2ef332085c852fe86069062903dd71ae55e7759a Mon Sep 17 00:00:00 2001 From: Reinis Mazeiks Date: Sat, 7 May 2022 09:39:53 +0300 Subject: [PATCH] Refactor: move input types to separate module --- packages/html/src/events.rs | 56 ++---------------------------------- packages/html/src/input.rs | 57 +++++++++++++++++++++++++++++++++++++ packages/html/src/lib.rs | 2 ++ 3 files changed, 61 insertions(+), 54 deletions(-) create mode 100644 packages/html/src/input.rs diff --git a/packages/html/src/events.rs b/packages/html/src/events.rs index 898246377..12c946eeb 100644 --- a/packages/html/src/events.rs +++ b/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; - - /// 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; - pub type PointerEvent = UiEvent; #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[derive(Debug, Clone)] diff --git a/packages/html/src/input.rs b/packages/html/src/input.rs new file mode 100644 index 000000000..06787023b --- /dev/null +++ b/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; + +/// 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; diff --git a/packages/html/src/lib.rs b/packages/html/src/lib.rs index bff22ec5e..3de940e25 100644 --- a/packages/html/src/lib.rs +++ b/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::*;