#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KeyAction {
Char(char),
Shift,
Backspace,
Space,
Enter,
Back,
Page(u8),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyEvent {
Char(char),
Backspace,
Enter,
Back,
CursorLeft,
CursorRight,
}
impl KeyAction {
pub const fn weight(self) -> u32 {
match self {
Self::Space => 4,
_ => 2,
}
}
pub const fn chars<const N: usize>(letters: &[u8; N]) -> [KeyAction; N] {
let mut keys = [KeyAction::Space; N];
let mut i = 0;
while i < N {
keys[i] = KeyAction::Char(letters[i] as char);
i += 1;
}
keys
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Page {
pub label: &'static str,
pub rows: &'static [&'static [KeyAction]],
}
impl Page {
pub const fn new(label: &'static str, rows: &'static [&'static [KeyAction]]) -> Self {
Self { label, rows }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Layout {
pub pages: &'static [Page],
}
impl Layout {
pub const fn new(pages: &'static [Page]) -> Self {
Self { pages }
}
pub const QWERTY: Self = Self::new(&[
Page::new(
"abc",
&[
&KeyAction::chars(b"qwertyuiop"),
&KeyAction::chars(b"asdfghjkl"),
&KeyAction::chars(b"zxcvbnm"),
&[
KeyAction::Shift,
KeyAction::Page(1),
KeyAction::Space,
KeyAction::Backspace,
KeyAction::Back,
KeyAction::Enter,
],
],
),
SYMBOLS_PAGE,
]);
}
impl Default for Layout {
fn default() -> Self {
Self::QWERTY
}
}
const SYMBOLS_PAGE: Page = Page::new(
"123",
&[
&KeyAction::chars(b"1234567890"),
&KeyAction::chars(b"-/:;()$&@\""),
&KeyAction::chars(b".,?!'#%+="),
&[
KeyAction::Page(0),
KeyAction::Space,
KeyAction::Backspace,
KeyAction::Back,
KeyAction::Enter,
],
],
);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Labels {
pub shift: &'static str,
pub caps_lock: &'static str,
pub backspace: &'static str,
pub space: &'static str,
pub enter: &'static str,
pub back: &'static str,
}
impl Labels {
pub const DEFAULT: Self = Self {
shift: "SHF",
caps_lock: "CAP",
backspace: "DEL",
space: "SPACE",
enter: "OK",
back: "BACK",
};
}
impl Default for Labels {
fn default() -> Self {
Self::DEFAULT
}
}