smart_keymap/
input.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
use serde::Deserialize;

use crate::key;

/// Input events for [crate::keymap::Keymap].
#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)]
pub enum Event {
    /// A physical key press for a given `keymap_index`.
    Press {
        /// The index of the key in the keymap.
        keymap_index: u16,
    },
    /// A physical key release for a given `keymap_index`.
    Release {
        /// The index of the key in the keymap.
        keymap_index: u16,
    },
    /// A virtual key press for a given `key_code`.
    VirtualKeyPress {
        /// The virtual key code.
        key_code: u8,
        /// Inserts the virtual key before the pressed key with this keymap index.
        pressed_keymap_index: u16,
    },
    /// A virtual key release for a given `key_code`.
    VirtualKeyRelease {
        /// The virtual key code.
        key_code: u8,
    },
    /// No-op
    InputResolved,
}

/// A struct for associating a [crate::key::Key] with a [crate::key::PressedKeyState].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PressedKey<K, S> {
    /// The index of the pressed key in some keymap.
    pub keymap_index: u16,
    /// The pressed key.
    pub key: K,
    /// The pressed key state.
    pub pressed_key_state: S,
}

impl<K, S> PressedKey<K, S> {
    /// Maps the key and pressed key state of the PressedKey to a new type.
    pub fn map_pressed_key<IK, IS>(self, f_k: fn(K) -> IK, f_s: fn(S) -> IS) -> PressedKey<IK, IS> {
        PressedKey {
            keymap_index: self.keymap_index,
            key: f_k(self.key),
            pressed_key_state: f_s(self.pressed_key_state),
        }
    }

    /// Transforms the PressedKey to a new type.
    pub fn into_pressed_key<IK, IS>(self) -> PressedKey<IK, IS>
    where
        K: Into<IK>,
        S: Into<IS>,
    {
        PressedKey {
            keymap_index: self.keymap_index,
            key: self.key.into(),
            pressed_key_state: self.pressed_key_state.into(),
        }
    }
}

impl<
        K: crate::key::Key,
        S: crate::key::PressedKeyState<K, Context = K::Context, Event = K::Event>,
    > crate::key::PressedKey for PressedKey<K, S>
{
    type Context = K::Context;
    type Event = K::Event;

    fn handle_event(
        &mut self,
        context: Self::Context,
        event: crate::key::Event<Self::Event>,
    ) -> crate::key::PressedKeyEvents<Self::Event> {
        self.pressed_key_state
            .handle_event_for(context, self.keymap_index, &self.key, event)
    }

    fn key_output(&self) -> key::KeyOutputState {
        self.pressed_key_state.key_output(&self.key)
    }
}

/// State resulting from [Event].
#[derive(Debug, Clone, Copy)]
pub enum PressedInput<PK> {
    /// Physically pressed key.
    Key {
        /// The pressed key.
        pressed_key: PK,
    },
    /// Virtually pressed key.
    Virtual {
        /// The pressed key code.
        key_code: u8,
    },
}

impl<PK> PressedInput<PK> {
    /// Constructor for a [PressedInput::Key].
    pub fn new_pressed_key(pressed_key: PK) -> Self {
        Self::Key { pressed_key }
    }
}