Skip to main content

pointer_types/
mouse.rs

1pub mod button;
2pub use button::*;
3#[cfg(feature = "serde")]
4use serde::{
5  Deserialize,
6  Serialize,
7};
8use {
9  crate::{
10    ButtonState,
11    PointerEvent,
12    PointerType,
13  },
14  dpi::PhysicalPosition,
15  keyboard_types::Modifiers,
16};
17
18/// Mouse events are issued for all pressed and released mouse buttons.
19#[derive(Clone, Debug, PartialEq)]
20#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
21pub enum MouseEvent {
22  Button {
23    /// The position relative to the top-left of the client area.
24    position: PhysicalPosition<f64>,
25    /// Whether the button is pressed or released.
26    state: ButtonState,
27    /// Logical button value.
28    button: MouseButton,
29    /// Flags for pressed modifier keys.
30    modifiers: Modifiers,
31    /// Whether this event is a double click.
32    is_double_click: bool,
33  },
34  Move {
35    /// The position relative to the top-left of the client area.
36    position: PhysicalPosition<f64>,
37  },
38  Enter {
39    /// The position relative to the top-left of the client area.
40    position: PhysicalPosition<f64>,
41  },
42  Leave {
43    /// The position relative to the top-left of the client area.
44    position: PhysicalPosition<f64>,
45  },
46}
47
48impl From<MouseEvent> for PointerEvent {
49  fn from(event: MouseEvent) -> Self {
50    match event {
51      MouseEvent::Button {
52        position,
53        state,
54        button,
55        modifiers,
56        is_double_click,
57      } => Self::Button {
58        pointer: PointerType::Mouse,
59        position,
60        state,
61        button: button.into(),
62        modifiers,
63        is_double: is_double_click,
64      },
65      MouseEvent::Move { position } => Self::Move {
66        pointer: PointerType::Mouse,
67        position,
68      },
69      MouseEvent::Enter { position } => Self::Enter {
70        pointer: PointerType::Mouse,
71        position,
72      },
73      MouseEvent::Leave { position } => Self::Leave {
74        pointer: PointerType::Mouse,
75        position,
76      },
77    }
78  }
79}
80
81impl TryFrom<PointerEvent> for MouseEvent {
82  type Error = ();
83
84  fn try_from(event: PointerEvent) -> Result<Self, Self::Error> {
85    Ok(match event {
86      PointerEvent::Button {
87        pointer: PointerType::Mouse,
88        position,
89        state,
90        button,
91        modifiers,
92        is_double: is_double_click,
93      } => Self::Button {
94        position,
95        state,
96        button: button.into(),
97        modifiers,
98        is_double_click,
99      },
100      PointerEvent::Move {
101        pointer: PointerType::Mouse,
102        position,
103      } => Self::Move { position },
104      PointerEvent::Enter {
105        pointer: PointerType::Mouse,
106        position,
107      } => Self::Enter { position },
108      PointerEvent::Leave {
109        pointer: PointerType::Mouse,
110        position,
111      } => Self::Leave { position },
112      _ => return Err(()),
113    })
114  }
115}