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> key::System<R> for System<R> {
62    type Ref = Ref;
63    type Context = Context;
64    type Event = Event;
65    type PendingKeyState = PendingKeyState;
66    type KeyState = KeyState;
67
68    fn new_pressed_key(
69        &self,
70        _keymap_index: u16,
71        _context: &Self::Context,
72        _key_ref: Ref,
73    ) -> (
74        key::PressedKeyResult<R, Self::PendingKeyState, Self::KeyState>,
75        key::KeyEvents<Self::Event>,
76    ) {
77        (
78            key::PressedKeyResult::Resolved(KeyState),
79            key::KeyEvents::no_events(),
80        )
81    }
82
83    fn update_pending_state(
84        &self,
85        _pending_state: &mut Self::PendingKeyState,
86        _keymap_index: u16,
87        _context: &Self::Context,
88        _key_ref: Ref,
89        _event: key::Event<Self::Event>,
90    ) -> (Option<key::NewPressedKey<R>>, key::KeyEvents<Self::Event>) {
91        panic!()
92    }
93
94    fn update_state(
95        &self,
96        _key_state: &mut Self::KeyState,
97        _ref: &Self::Ref,
98        _context: &Self::Context,
99        _keymap_index: u16,
100        _event: key::Event<Self::Event>,
101    ) -> key::KeyEvents<Self::Event> {
102        key::KeyEvents::no_events()
103    }
104
105    fn key_output(
106        &self,
107        key_ref: &Self::Ref,
108        _key_state: &Self::KeyState,
109    ) -> Option<key::KeyOutput> {
110        const MOVE_AMOUNT: i8 = 5;
111        let mouse_output = match key_ref {
112            Ref::Button(b) => key::MouseOutput {
113                pressed_buttons: 1 << (b - 1),
114                ..key::MouseOutput::NO_OUTPUT
115            },
116            Ref::CursorLeft => key::MouseOutput {
117                x: -MOVE_AMOUNT,
118                ..key::MouseOutput::NO_OUTPUT
119            },
120            Ref::CursorRight => key::MouseOutput {
121                x: MOVE_AMOUNT,
122                ..key::MouseOutput::NO_OUTPUT
123            },
124            Ref::CursorUp => key::MouseOutput {
125                y: -MOVE_AMOUNT,
126                ..key::MouseOutput::NO_OUTPUT
127            },
128            Ref::CursorDown => key::MouseOutput {
129                y: MOVE_AMOUNT,
130                ..key::MouseOutput::NO_OUTPUT
131            },
132            Ref::WheelUp => key::MouseOutput {
133                vertical_scroll: 1,
134                ..key::MouseOutput::NO_OUTPUT
135            },
136            Ref::WheelDown => key::MouseOutput {
137                vertical_scroll: -1,
138                ..key::MouseOutput::NO_OUTPUT
139            },
140            Ref::WheelLeft => key::MouseOutput {
141                horizontal_scroll: -1,
142                ..key::MouseOutput::NO_OUTPUT
143            },
144            Ref::WheelRight => key::MouseOutput {
145                horizontal_scroll: 1,
146                ..key::MouseOutput::NO_OUTPUT
147            },
148        };
149        Some(key::KeyOutput::from_mouse_output(mouse_output))
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156
157    #[test]
158    fn test_sizeof_ref() {
159        assert_eq!(2, core::mem::size_of::<Ref>());
160    }
161
162    #[test]
163    fn test_sizeof_event() {
164        assert_eq!(0, core::mem::size_of::<Event>());
165    }
166}