Skip to main content

smart_keymap/key/
mouse.rs

1use core::fmt::Debug;
2
3use serde::Deserialize;
4
5use crate::key;
6
7/// Reference for a mouse key.
8#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
9pub enum Ref {
10    /// A mouse button. (Value is button number, 1-8).
11    Button(u8),
12    /// Move cursor left.
13    CursorLeft,
14    /// Move cursor right.
15    CursorRight,
16    /// Move cursor up.
17    CursorUp,
18    /// Move cursor down.
19    CursorDown,
20    /// Scroll wheel up.
21    WheelUp,
22    /// Scroll wheel down.
23    WheelDown,
24    /// Scroll wheel left.
25    WheelLeft,
26    /// Scroll wheel right.
27    WheelRight,
28}
29
30/// Context for mouse keys. (No context).
31#[derive(Debug, Clone, Copy, PartialEq)]
32pub struct Context;
33
34/// The event type for mouse keys. (No events).
35#[derive(Debug, Clone, Copy, PartialEq)]
36pub struct Event;
37
38/// The pending key state type for mouse keys. (No pending state).
39#[derive(Debug, Clone, Copy, PartialEq)]
40pub struct PendingKeyState;
41
42/// Key state used by [System].
43#[derive(Debug, Clone, Copy, PartialEq)]
44pub struct KeyState;
45
46/// The [key::System] implementation for mouse keys.
47#[derive(Debug, Clone, Copy, PartialEq)]
48pub struct System<R: Debug> {
49    _marker: core::marker::PhantomData<R>,
50}
51
52impl<R: Debug> System<R> {
53    /// Constructs a new [System].
54    pub const fn new() -> Self {
55        Self {
56            _marker: core::marker::PhantomData,
57        }
58    }
59}
60
61impl<R: Debug> Default for System<R> {
62    fn default() -> Self {
63        Self::new()
64    }
65}
66
67impl<R: Debug> key::System<R> for System<R> {
68    type Ref = Ref;
69    type Context = Context;
70    type Event = Event;
71    type PendingKeyState = PendingKeyState;
72    type KeyState = KeyState;
73
74    fn new_pressed_key(
75        &self,
76        _keymap_index: u16,
77        _context: &Self::Context,
78        _key_ref: Ref,
79    ) -> (
80        key::PressedKeyResult<R, Self::PendingKeyState, Self::KeyState>,
81        key::KeyEvents<Self::Event>,
82    ) {
83        (
84            key::PressedKeyResult::Resolved(KeyState),
85            key::KeyEvents::no_events(),
86        )
87    }
88
89    fn update_pending_state(
90        &self,
91        _pending_state: &mut Self::PendingKeyState,
92        _keymap_index: u16,
93        _context: &Self::Context,
94        _key_ref: Ref,
95        _event: key::Event<Self::Event>,
96    ) -> (Option<key::NewPressedKey<R>>, key::KeyEvents<Self::Event>) {
97        panic!()
98    }
99
100    fn update_state(
101        &self,
102        _key_state: &mut Self::KeyState,
103        _ref: &Self::Ref,
104        _context: &Self::Context,
105        _keymap_index: u16,
106        _event: key::Event<Self::Event>,
107    ) -> key::KeyEvents<Self::Event> {
108        key::KeyEvents::no_events()
109    }
110
111    fn key_output(
112        &self,
113        key_ref: &Self::Ref,
114        _key_state: &Self::KeyState,
115    ) -> Option<key::KeyOutput> {
116        const MOVE_AMOUNT: i8 = 5;
117        let mouse_output = match key_ref {
118            Ref::Button(b) => key::MouseOutput {
119                pressed_buttons: 1 << (b - 1),
120                ..key::MouseOutput::NO_OUTPUT
121            },
122            Ref::CursorLeft => key::MouseOutput {
123                x: -MOVE_AMOUNT,
124                ..key::MouseOutput::NO_OUTPUT
125            },
126            Ref::CursorRight => key::MouseOutput {
127                x: MOVE_AMOUNT,
128                ..key::MouseOutput::NO_OUTPUT
129            },
130            Ref::CursorUp => key::MouseOutput {
131                y: -MOVE_AMOUNT,
132                ..key::MouseOutput::NO_OUTPUT
133            },
134            Ref::CursorDown => key::MouseOutput {
135                y: MOVE_AMOUNT,
136                ..key::MouseOutput::NO_OUTPUT
137            },
138            Ref::WheelUp => key::MouseOutput {
139                vertical_scroll: 1,
140                ..key::MouseOutput::NO_OUTPUT
141            },
142            Ref::WheelDown => key::MouseOutput {
143                vertical_scroll: -1,
144                ..key::MouseOutput::NO_OUTPUT
145            },
146            Ref::WheelLeft => key::MouseOutput {
147                horizontal_scroll: -1,
148                ..key::MouseOutput::NO_OUTPUT
149            },
150            Ref::WheelRight => key::MouseOutput {
151                horizontal_scroll: 1,
152                ..key::MouseOutput::NO_OUTPUT
153            },
154        };
155        Some(key::KeyOutput::from_mouse_output(mouse_output))
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    #[test]
164    fn test_sizeof_ref() {
165        assert_eq!(2, core::mem::size_of::<Ref>());
166    }
167
168    #[test]
169    fn test_sizeof_event() {
170        assert_eq!(0, core::mem::size_of::<Event>());
171    }
172}