smart_keymap/key/
tap_hold.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#![doc = include_str!("doc_de_tap_hold.md")]

use core::fmt::Debug;

use serde::Deserialize;

use crate::input;
use crate::key;

use super::PressedKey as _;

/// How the tap hold key should respond to interruptions (input events from other keys).
#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum InterruptResponse {
    /// The tap-hold key ignores other key presses/taps.
    /// (Only resolves to hold on timeout).
    Ignore,
    /// The tap-hold key resolves as "hold" when interrupted by a key press.
    HoldOnKeyPress,
    /// The tap-hold key resolves as "hold" when interrupted by a key tap.
    /// (Another key was pressed and released).
    HoldOnKeyTap,
}

/// Configuration settings for tap hold keys.
#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
pub struct Config {
    /// The timeout (in number of ticks) for a tap-hold key to resolve as hold.
    #[serde(default = "default_timeout")]
    pub timeout: u16,

    /// How the tap-hold key should respond to interruptions.
    #[serde(default = "default_interrupt_response")]
    pub interrupt_response: InterruptResponse,
}

fn default_timeout() -> u16 {
    DEFAULT_CONFIG.timeout
}

fn default_interrupt_response() -> InterruptResponse {
    DEFAULT_CONFIG.interrupt_response
}

/// Default tap hold config.
pub const DEFAULT_CONFIG: Config = Config {
    timeout: 200,
    interrupt_response: InterruptResponse::Ignore,
};

impl Default for Config {
    /// Returns the default context.
    fn default() -> Self {
        DEFAULT_CONFIG
    }
}

/// A key with tap-hold functionality.
#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
pub struct Key<K: key::Key> {
    /// The 'tap' key.
    pub tap: K,
    /// The 'hold' key.
    pub hold: K,
}

impl<K: key::Key> Key<K> {
    /// Constructs a new tap-hold key.
    pub const fn new(tap: K, hold: K) -> Key<K> {
        Key { tap, hold }
    }

    /// Maps the Key of the Key into a new type.
    pub fn map_key<T: key::Key>(self, f: fn(K) -> T) -> Key<T> {
        let Key { tap, hold } = self;
        Key {
            tap: f(tap),
            hold: f(hold),
        }
    }

    /// Maps the Key of the Key into a new type.
    pub fn into_key<T: key::Key>(self) -> Key<T>
    where
        K: Into<T>,
    {
        self.map_key(|k| k.into())
    }
}

impl<K: key::Key> Key<K> {
    /// Constructs a new pressed key state and a scheduled event for the tap-hold key.
    pub fn new_pressed_key(
        &self,
        context: Context,
        keymap_index: u16,
    ) -> (PressedKeyState<K>, key::ScheduledEvent<Event>) {
        let timeout_ev = Event::TapHoldTimeout;
        (
            PressedKeyState::new(),
            key::ScheduledEvent::after(
                context.config.timeout,
                key::Event::key_event(keymap_index, timeout_ev),
            ),
        )
    }
}

/// Context for [Key].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Context {
    config: Config,
}

/// Default context.
pub const DEFAULT_CONTEXT: Context = Context::from_config(DEFAULT_CONFIG);

impl Context {
    /// Constructs a context from the given config
    pub const fn from_config(config: Config) -> Context {
        Context { config }
    }
}

/// The state of a tap-hold key.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TapHoldState {
    /// Not yet resolved as tap or hold.
    Pending,
    /// Resolved as tap.
    Tap,
    /// Resolved as hold.
    Hold,
}

/// Events emitted by a tap-hold key.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Event {
    /// Event indicating the key has been held long enough to resolve as hold.
    TapHoldTimeout,
}

/// The state of a pressed tap-hold key.
#[derive(Debug, PartialEq)]
pub struct PressedKeyState<K: key::Key> {
    state: TapHoldState,
    pressed_key: Option<K::PressedKey>,
    // For tracking 'tap' interruptions
    other_pressed_keymap_index: Option<u16>,
}

/// Convenience type for a pressed tap-hold key.
pub type PressedKey<K> = input::PressedKey<Key<K>, PressedKeyState<K>>;

impl<K: key::Key> PressedKeyState<K> {
    /// Constructs the initial pressed key state
    fn new() -> PressedKeyState<K> {
        PressedKeyState {
            state: TapHoldState::Pending,
            pressed_key: None,
            other_pressed_keymap_index: None,
        }
    }

    /// Maps the Key of the PressedKeyState into a new type.
    pub fn map_pressed_key<T: key::Key>(
        self,
        f: fn(K::PressedKey) -> T::PressedKey,
    ) -> PressedKeyState<T> {
        let PressedKeyState {
            state,
            pressed_key,
            other_pressed_keymap_index,
        } = self;
        PressedKeyState {
            state,
            pressed_key: pressed_key.map(f),
            other_pressed_keymap_index,
        }
    }

    /// Maps the Key of the PressedKeyState into a new type.
    pub fn into_pressed_key<T: key::Key>(self) -> PressedKeyState<T>
    where
        T::PressedKey: From<K::PressedKey>,
    {
        self.map_pressed_key(|pk| pk.into())
    }

    /// Resolves the state of the key, unless it has already been resolved.
    fn resolve(&mut self, state: TapHoldState) {
        if let TapHoldState::Pending = self.state {
            self.state = state;
        }
    }

    /// Compute whether the tap-hold key should resolve as tap or hold,
    ///  given the tap hold config, the current state, and the key event.
    fn hold_resolution(
        &self,
        interrupt_response: InterruptResponse,
        keymap_index: u16,
        event: key::Event<Event>,
    ) -> Option<TapHoldState> {
        match self.state {
            TapHoldState::Pending => {
                match interrupt_response {
                    InterruptResponse::HoldOnKeyPress => {
                        match event {
                            key::Event::Input(input::Event::Press { .. }) => {
                                // TapHold: any interruption resolves pending TapHold as Hold.
                                Some(TapHoldState::Hold)
                            }
                            key::Event::Input(input::Event::Release { keymap_index: ki }) => {
                                if keymap_index == ki {
                                    // TapHold: not interrupted; resolved as tap.
                                    Some(TapHoldState::Tap)
                                } else {
                                    None
                                }
                            }
                            key::Event::Key {
                                key_event: Event::TapHoldTimeout,
                                ..
                            } => {
                                // Key held long enough to resolve as hold.
                                Some(TapHoldState::Hold)
                            }
                            _ => None,
                        }
                    }
                    InterruptResponse::HoldOnKeyTap => {
                        match event {
                            key::Event::Input(input::Event::Release { keymap_index: ki }) => {
                                if keymap_index == ki {
                                    // TapHold: not interrupted; resolved as tap.
                                    Some(TapHoldState::Tap)
                                } else if Some(ki) == self.other_pressed_keymap_index {
                                    // TapHold: interrupted by key tap (press + release); resolved as hold.
                                    Some(TapHoldState::Hold)
                                } else {
                                    None
                                }
                            }
                            key::Event::Key {
                                key_event: Event::TapHoldTimeout,
                                ..
                            } => {
                                // Key held long enough to resolve as hold.
                                Some(TapHoldState::Hold)
                            }
                            _ => None,
                        }
                    }
                    InterruptResponse::Ignore => {
                        match event {
                            key::Event::Input(input::Event::Release { keymap_index: ki }) => {
                                if keymap_index == ki {
                                    // TapHold: not interrupted; resolved as tap.
                                    Some(TapHoldState::Tap)
                                } else {
                                    None
                                }
                            }
                            key::Event::Key {
                                key_event: Event::TapHoldTimeout,
                                ..
                            } => {
                                // Key held long enough to resolve as hold.
                                Some(TapHoldState::Hold)
                            }
                            _ => None,
                        }
                    }
                }
            }
            _ => None,
        }
    }

    /// Returns the key output state.
    pub fn key_output(&self) -> key::KeyOutputState {
        match &self.pressed_key {
            Some(pk) => pk.key_output(),
            None => key::KeyOutputState::pending(),
        }
    }
}

impl<K: key::Key> PressedKeyState<K>
where
    K::Context: Into<Context>,
    K::Event: TryInto<Event>,
    K::Event: From<Event>,
{
    /// Returns at most 2 events
    pub fn handle_event_for(
        &mut self,
        context: K::Context,
        keymap_index: u16,
        key: &Key<K>,
        event: key::Event<K::Event>,
    ) -> key::PressedKeyEvents<K::Event> {
        let mut pke = key::PressedKeyEvents::no_events();

        // Add events from inner pk handle_event
        if let Some(pk) = &mut self.pressed_key {
            let pk_ev = pk.handle_event(context, event);
            pke.extend(pk_ev);
        }

        // Check for interrupting taps
        // (track other key press, if this PKS has not resolved)
        match self.state {
            TapHoldState::Pending => match event {
                key::Event::Input(input::Event::Press { keymap_index: ki }) => {
                    self.other_pressed_keymap_index = Some(ki);
                }
                _ => {}
            },
            _ => {}
        }

        // Resolve tap-hold state per the event.
        let Context { config, .. } = context.into();
        if let Ok(ev) = event.try_into_key_event(|e| e.try_into()) {
            match self.hold_resolution(config.interrupt_response, keymap_index, ev) {
                Some(TapHoldState::Hold) => {
                    self.resolve(TapHoldState::Hold);

                    let (hold_pk, hold_pke) = key.hold.new_pressed_key(context, keymap_index);
                    self.pressed_key = Some(hold_pk);
                    pke.extend(hold_pke);
                }
                Some(TapHoldState::Tap) => {
                    self.resolve(TapHoldState::Tap);

                    let (tap_pk, tap_pke) = key.tap.new_pressed_key(context, keymap_index);
                    self.pressed_key = Some(tap_pk);
                    pke.extend(tap_pke);
                }
                _ => {}
            }
        }

        pke
    }
}