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    ) -> (Option<key::NewPressedKey>, key::KeyEvents<Self::Event>) {
70        match self {
71            ChordedKey::Chorded(key) => key.handle_event(pending_state, context, key_path, event),
72            ChordedKey::Auxiliary(key) => key.handle_event(pending_state, context, key_path, event),
73            ChordedKey::Pass(key) => key.handle_event(pending_state, context, key_path, event),
74        }
75    }
76
77    fn lookup(
78        &self,
79        path: &[u16],
80    ) -> &dyn key::Key<
81        Context = Self::Context,
82        Event = Self::Event,
83        PendingKeyState = Self::PendingKeyState,
84        KeyState = Self::KeyState,
85    > {
86        match self {
87            ChordedKey::Chorded(key) => key.lookup(path),
88            ChordedKey::Auxiliary(key) => key.lookup(path),
89            ChordedKey::Pass(key) => key.lookup(path),
90        }
91    }
92}
93
94impl<K: ChordedNestable> key::Key for Chorded<K> {
95    type Context = Context;
96    type Event = Event;
97    type PendingKeyState = PendingKeyState;
98    type KeyState = KeyState;
99
100    fn new_pressed_key(
101        &self,
102        context: &Self::Context,
103        key_path: key::KeyPath,
104    ) -> (PressedKeyResult, key::KeyEvents<Self::Event>) {
105        let Chorded(key) = self;
106        key.new_pressed_key(context, key_path)
107    }
108
109    fn handle_event(
110        &self,
111        pending_state: &mut Self::PendingKeyState,
112        context: &Self::Context,
113        key_path: key::KeyPath,
114        event: key::Event<Self::Event>,
115    ) -> (Option<key::NewPressedKey>, key::KeyEvents<Self::Event>) {
116        let Chorded(key) = self;
117        key.handle_event(pending_state, context, key_path, event)
118    }
119
120    fn lookup(
121        &self,
122        path: &[u16],
123    ) -> &dyn key::Key<
124        Context = Self::Context,
125        Event = Self::Event,
126        PendingKeyState = Self::PendingKeyState,
127        KeyState = Self::KeyState,
128    > {
129        let Chorded(key) = self;
130        key.lookup(path)
131    }
132}
133
134impl ChordedKey<LayeredKey<TapHoldKey<BaseKey>>> {
135    /// Constructs a [ChordedKey] from the given [key::keyboard::Key].
136    pub const fn keyboard(key: key::keyboard::Key) -> Self {
137        ChordedKey::Pass(LayeredKey::Pass(TapHoldKey::keyboard(key)))
138    }
139
140    /// Constructs a [ChordedKey] from the given [key::tap_hold::Key].
141    pub const fn tap_hold(key: key::tap_hold::Key<BaseKey>) -> Self {
142        ChordedKey::Pass(LayeredKey::Pass(TapHoldKey::tap_hold(key)))
143    }
144
145    /// Constructs a [ChordedKey] from the given [key::layered::ModifierKey].
146    pub const fn layer_modifier(key: key::layered::ModifierKey) -> Self {
147        ChordedKey::Pass(LayeredKey::Pass(TapHoldKey::layer_modifier(key)))
148    }
149}
150
151impl<K: LayeredNestable> ChordedKey<LayeredKey<K>> {
152    /// Constructs a [ChordedKey] from the given [key::layered::LayeredKey].
153    pub const fn layered(key: key::layered::LayeredKey<K>) -> Self {
154        ChordedKey::Pass(LayeredKey::Layered(key))
155    }
156}