smart_keymap/key/
mouse.rs1use core::fmt::Debug;
2
3use serde::Deserialize;
4
5use crate::key;
6
7#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
9pub enum Ref {
10 Button(u8),
12 CursorLeft,
14 CursorRight,
16 CursorUp,
18 CursorDown,
20 WheelUp,
22 WheelDown,
24 WheelLeft,
26 WheelRight,
28}
29
30#[derive(Debug, Clone, Copy, PartialEq)]
32pub struct Context;
33
34#[derive(Debug, Clone, Copy, PartialEq)]
36pub struct Event;
37
38#[derive(Debug, Clone, Copy, PartialEq)]
40pub struct PendingKeyState;
41
42#[derive(Debug, Clone, Copy, PartialEq)]
44pub struct KeyState;
45
46#[derive(Debug, Clone, Copy, PartialEq)]
48pub struct System<R: Debug> {
49 _marker: core::marker::PhantomData<R>,
50}
51
52impl<R: Debug> System<R> {
53 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}