rmk_types/
mouse_button.rs

1//! Mouse button state and operations.
2//!
3//! This module handles mouse button combinations and states, supporting up to
4//! 8 mouse buttons.
5use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
6
7use bitfield_struct::bitfield;
8use serde::{Deserialize, Serialize};
9
10/// Mouse buttons
11#[bitfield(u8, order = Lsb, defmt = cfg(feature = "defmt"))]
12#[derive(Eq, PartialEq, Serialize, Deserialize, postcard::experimental::max_size::MaxSize)]
13pub struct MouseButtons {
14    #[bits(1)]
15    pub button1: bool, //left
16    #[bits(1)]
17    pub button2: bool, //right
18    #[bits(1)]
19    pub button3: bool, //middle
20    #[bits(1)]
21    pub button4: bool,
22    #[bits(1)]
23    pub button5: bool,
24    #[bits(1)]
25    pub button6: bool,
26    #[bits(1)]
27    pub button7: bool,
28    #[bits(1)]
29    pub button8: bool,
30}
31
32impl BitOr for MouseButtons {
33    type Output = Self;
34
35    fn bitor(self, rhs: Self) -> Self::Output {
36        Self::from_bits(self.into_bits() | rhs.into_bits())
37    }
38}
39impl BitAnd for MouseButtons {
40    type Output = Self;
41
42    fn bitand(self, rhs: Self) -> Self::Output {
43        Self::from_bits(self.into_bits() & rhs.into_bits())
44    }
45}
46impl Not for MouseButtons {
47    type Output = Self;
48
49    fn not(self) -> Self::Output {
50        Self::from_bits(!self.into_bits())
51    }
52}
53impl BitAndAssign for MouseButtons {
54    fn bitand_assign(&mut self, rhs: Self) {
55        *self = *self & rhs;
56    }
57}
58impl BitOrAssign for MouseButtons {
59    fn bitor_assign(&mut self, rhs: Self) {
60        *self = *self | rhs;
61    }
62}
63
64impl MouseButtons {
65    pub const BUTTON1: Self = Self::new().with_button1(true);
66    pub const BUTTON2: Self = Self::new().with_button2(true);
67    pub const BUTTON3: Self = Self::new().with_button3(true);
68    pub const BUTTON4: Self = Self::new().with_button4(true);
69    pub const BUTTON5: Self = Self::new().with_button5(true);
70    pub const BUTTON6: Self = Self::new().with_button6(true);
71    pub const BUTTON7: Self = Self::new().with_button7(true);
72    pub const BUTTON8: Self = Self::new().with_button8(true);
73
74    #[allow(clippy::too_many_arguments)]
75    pub const fn new_from(
76        button1: bool,
77        button2: bool,
78        button3: bool,
79        button4: bool,
80        button5: bool,
81        button6: bool,
82        button7: bool,
83        button8: bool,
84    ) -> Self {
85        Self::new()
86            .with_button1(button1)
87            .with_button2(button2)
88            .with_button3(button3)
89            .with_button4(button4)
90            .with_button5(button5)
91            .with_button6(button6)
92            .with_button7(button7)
93            .with_button8(button8)
94    }
95}