smart_keymap/key/composite/
chorded.rs

1use core::fmt::Debug;
2
3#[cfg(feature = "std")]
4use serde::Deserialize;
5
6use crate::key;
7
8use super::BaseKey;
9use super::TapHoldKey;
10use super::{Context, Event, KeyState, PendingKeyState, PressedKeyResult};
11use super::{Layered, LayeredKey, LayeredNestable};
12
13/// Trait for types which can be nested in [ChordedKey] variants.
14pub trait ChordedNestable:
15    key::Key<
16        Context = Context,
17        Event = Event,
18        KeyState = KeyState,
19        PendingKeyState = PendingKeyState,
20    > + Copy
21    + PartialEq
22{
23}
24
25impl<K: LayeredNestable> ChordedNestable for Layered<K> {}
26impl<K: LayeredNestable> ChordedNestable for LayeredKey<K> {}
27
28/// An aggregate of [key::Key] types.
29#[derive(Debug, Clone, Copy, PartialEq)]
30#[cfg_attr(feature = "std", derive(Deserialize))]
31#[cfg_attr(feature = "std", serde(untagged))]
32pub enum ChordedKey<K: ChordedNestable> {
33    /// A chorded key.
34    Chorded(key::chorded::Key<K>),
35    /// A chorded key.
36    Auxiliary(key::chorded::AuxiliaryKey<K>),
37    /// Non-chorded,
38    Pass(K),
39}
40
41/// Newtype for [ChordedNestable] keys so they can implement [key::Key].
42#[derive(Debug, Clone, Copy)]
43pub struct Chorded<K: ChordedNestable>(pub K);
44
45impl<K: ChordedNestable> key::Key for ChordedKey<K> {
46    type Context = Context;
47    type Event = Event;
48    type PendingKeyState = PendingKeyState;
49    type KeyState = KeyState;
50
51    fn new_pressed_key(
52        &self,
53        context: &Self::Context,
54        key_path: key::KeyPath,
55    ) -> (PressedKeyResult, key::KeyEvents<Self::Event>) {
56        match self {
57            ChordedKey::Chorded(key) => key.new_pressed_key(context, key_path),
58            ChordedKey::Auxiliary(key) => key.new_pressed_key(context, key_path),
59            ChordedKey::Pass(key) => key.new_pressed_key(context, key_path),
60        }
61    }
62
63    fn handle_event(
64        &self,
65        pending_state: &mut Self::PendingKeyState,
66        context: &Self::Context,
67        key_path: key::KeyPath,
68        event: key::Event<Self::Event>,
69    ) -> (
70        Option<key::PressedKeyResult<Self::PendingKeyState, Self::KeyState>>,
71        key::KeyEvents<Self::Event>,
72    ) {
73        match self {
74            ChordedKey::Chorded(key) => key.handle_event(pending_state, context, key_path, event),
75            ChordedKey::Auxiliary(key) => key.handle_event(pending_state, context, key_path, event),
76            ChordedKey::Pass(key) => key.handle_event(pending_state, context, key_path, event),
77        }
78    }
79
80    fn lookup(
81        &self,
82        path: &[u16],
83    ) -> &dyn key::Key<
84        Context = Self::Context,
85        Event = Self::Event,
86        PendingKeyState = Self::PendingKeyState,
87        KeyState = Self::KeyState,
88    > {
89        match self {
90            ChordedKey::Chorded(key) => key.lookup(path),
91            ChordedKey::Auxiliary(key) => key.lookup(path),
92            ChordedKey::Pass(key) => key.lookup(path),
93        }
94    }
95}
96
97impl<K: ChordedNestable> key::Key for Chorded<K> {
98    type Context = Context;
99    type Event = Event;
100    type PendingKeyState = PendingKeyState;
101    type KeyState = KeyState;
102
103    fn new_pressed_key(
104        &self,
105        context: &Self::Context,
106        key_path: key::KeyPath,
107    ) -> (PressedKeyResult, key::KeyEvents<Self::Event>) {
108        let Chorded(key) = self;
109        key.new_pressed_key(context, key_path)
110    }
111
112    fn handle_event(
113        &self,
114        pending_state: &mut Self::PendingKeyState,
115        context: &Self::Context,
116        key_path: key::KeyPath,
117        event: key::Event<Self::Event>,
118    ) -> (
119        Option<key::PressedKeyResult<Self::PendingKeyState, Self::KeyState>>,
120        key::KeyEvents<Self::Event>,
121    ) {
122        let Chorded(key) = self;
123        key.handle_event(pending_state, context, key_path, event)
124    }
125
126    fn lookup(
127        &self,
128        path: &[u16],
129    ) -> &dyn key::Key<
130        Context = Self::Context,
131        Event = Self::Event,
132        PendingKeyState = Self::PendingKeyState,
133        KeyState = Self::KeyState,
134    > {
135        let Chorded(key) = self;
136        key.lookup(path)
137    }
138}
139
140impl ChordedKey<LayeredKey<TapHoldKey<BaseKey>>> {
141    /// Constructs a [ChordedKey] from the given [key::keyboard::Key].
142    pub const fn keyboard(key: key::keyboard::Key) -> Self {
143        ChordedKey::Pass(LayeredKey::Pass(TapHoldKey::keyboard(key)))
144    }
145
146    /// Constructs a [ChordedKey] from the given [key::tap_hold::Key].
147    pub const fn tap_hold(key: key::tap_hold::Key<BaseKey>) -> Self {
148        ChordedKey::Pass(LayeredKey::Pass(TapHoldKey::tap_hold(key)))
149    }
150
151    /// Constructs a [ChordedKey] from the given [key::layered::ModifierKey].
152    pub const fn layer_modifier(key: key::layered::ModifierKey) -> Self {
153        ChordedKey::Pass(LayeredKey::Pass(TapHoldKey::layer_modifier(key)))
154    }
155}
156
157impl<K: LayeredNestable> ChordedKey<LayeredKey<K>> {
158    /// Constructs a [ChordedKey] from the given [key::layered::LayeredKey].
159    pub const fn layered(key: key::layered::LayeredKey<K>) -> Self {
160        ChordedKey::Pass(LayeredKey::Layered(key))
161    }
162}