Skip to main content

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