smart_keymap_core/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
34impl Context {
35 pub fn reset(&mut self) {}
37}
38
39#[derive(Debug, Clone, Copy, PartialEq)]
41pub struct Event;
42
43#[derive(Debug, Clone, Copy, PartialEq)]
45pub struct PendingKeyState;
46
47#[derive(Debug, Clone, Copy, PartialEq)]
49pub struct KeyState;
50
51#[derive(Debug, Clone, Copy, PartialEq)]
53pub struct System<R: Debug> {
54 _marker: core::marker::PhantomData<R>,
55}
56
57impl<R: Debug> System<R> {
58 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}