smart_keymap_core/key/
consumer.rs1use core::fmt::Debug;
2use core::marker::PhantomData;
3use core::ops::Index;
4
5use serde::Deserialize;
6
7use crate::key;
8
9#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
11pub enum Ref {
12 UsageCode(u8),
14 Key(u8),
16}
17
18#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
22pub struct Key {
23 pub usage_code: u8,
25 #[serde(default)]
27 pub modifiers: key::KeyboardModifiers,
28}
29
30impl Key {
31 pub const fn new(usage_code: u8) -> Self {
33 Self {
34 usage_code,
35 modifiers: key::KeyboardModifiers::new(),
36 }
37 }
38
39 pub const fn new_with_modifiers(usage_code: u8, modifiers: key::KeyboardModifiers) -> Self {
41 Self {
42 usage_code,
43 modifiers,
44 }
45 }
46}
47
48#[derive(Debug, Clone, Copy, PartialEq)]
50pub struct Context;
51
52impl Context {
53 pub fn reset(&mut self) {}
55}
56
57#[derive(Debug, Clone, Copy, PartialEq)]
59pub struct Event;
60
61#[derive(Debug, Clone, Copy, PartialEq)]
63pub struct PendingKeyState;
64
65#[derive(Debug, Clone, Copy, PartialEq)]
67pub struct KeyState;
68
69#[derive(Debug, Clone, Copy, PartialEq)]
71pub struct System<R: Debug, Keys: Index<usize, Output = Key>> {
72 keys: Keys,
73 marker: PhantomData<R>,
74}
75
76impl<R: Debug, Keys: Index<usize, Output = Key>> System<R, Keys> {
77 pub const fn new(keys: Keys) -> Self {
81 Self {
82 keys,
83 marker: PhantomData,
84 }
85 }
86}
87
88impl<R: Debug, Keys: Debug + Index<usize, Output = Key>> key::System<R> for System<R, Keys> {
89 type Ref = Ref;
90 type Context = Context;
91 type Event = Event;
92 type PendingKeyState = PendingKeyState;
93 type KeyState = KeyState;
94
95 fn new_pressed_key(
96 &self,
97 _keymap_index: u16,
98 _context: &Self::Context,
99 _key_ref: Ref,
100 ) -> (
101 key::PressedKeyResult<R, Self::PendingKeyState, Self::KeyState>,
102 key::KeyEvents<Self::Event>,
103 ) {
104 (
105 key::PressedKeyResult::Resolved(KeyState),
106 key::KeyEvents::no_events(),
107 )
108 }
109
110 fn update_pending_state(
111 &self,
112 _pending_state: &mut Self::PendingKeyState,
113 _keymap_index: u16,
114 _context: &Self::Context,
115 _key_ref: Ref,
116 _event: key::Event<Self::Event>,
117 ) -> (Option<key::NewPressedKey<R>>, key::KeyEvents<Self::Event>) {
118 panic!()
119 }
120
121 fn update_state(
122 &self,
123 _key_state: &mut Self::KeyState,
124 _ref: &Self::Ref,
125 _context: &Self::Context,
126 _keymap_index: u16,
127 _event: key::Event<Self::Event>,
128 ) -> key::KeyEvents<Self::Event> {
129 key::KeyEvents::no_events()
130 }
131
132 fn key_output(
133 &self,
134 key_ref: &Self::Ref,
135 _key_state: &Self::KeyState,
136 ) -> Option<key::KeyOutput> {
137 match key_ref {
138 Ref::UsageCode(uc) => Some(key::KeyOutput::from_consumer_code(*uc)),
139 Ref::Key(idx) => {
140 let Key {
141 usage_code,
142 modifiers,
143 } = self.keys[*idx as usize];
144 Some(key::KeyOutput::from_usage_with_modifiers(
145 key::KeyUsage::Consumer(usage_code),
146 modifiers,
147 ))
148 }
149 }
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}