mouse_types/state.rs
1#[cfg(feature = "serde")]
2use serde::{
3 Deserialize,
4 Serialize,
5};
6
7/// Describes the state a button is in.
8#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10pub enum ButtonState {
11 /// The button is pressed down.
12 ///
13 /// Often emitted in a [mousedown] event, see also [the MDN documentation][mdn] on that.
14 ///
15 /// [mousedown]: https://w3c.github.io/pointerevents/#mousedown
16 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Element/mousedown_event
17 #[default]
18 Down,
19 /// The button is not pressed / was just released.
20 ///
21 /// Often emitted in a [mouseup] event, see also [the MDN documentation][mdn] on that.
22 ///
23 /// [mouseup]: https://w3c.github.io/pointerevents/#mouseup
24 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseup_event
25 Up,
26}
27
28impl ButtonState {
29 /// The [type] name of the corresponding mouse event.
30 ///
31 /// This is either `"mousedown"` or `"mouseup"`.
32 ///
33 /// [type]: https://w3c.github.io/pointerevents/#mouse-event-types
34 pub const fn event_type(self) -> &'static str {
35 match self {
36 Self::Down => "mousedown",
37 Self::Up => "mouseup",
38 }
39 }
40
41 /// True if the button is pressed down.
42 pub const fn is_down(self) -> bool {
43 matches!(self, Self::Down)
44 }
45
46 /// True if the button is released.
47 pub const fn is_up(self) -> bool {
48 matches!(self, Self::Up)
49 }
50}