embedded-graphics-keyboard 0.1.0

no_std on-screen keyboard and text field widgets for embedded-graphics, driven by buttons or touch
//! Keyboard layouts and key labels.
//!
//! A [`Layout`] is a list of [`Page`]s (letters, digits, symbols…). On every page,
//! all rows but the last hold same-sized keys; the last row is the *action row*:
//! its keys are sized by [`KeyAction::weight`] and span the full keyboard width.
//!
//! Keys of type [`KeyAction::Page`] switch between pages; they are labelled with
//! the [`Page::label`] of the page they lead to.
//!
//! ```
//! use embedded_graphics_keyboard::{KeyAction, Layout, Page};
//!
//! const DIGITS: Layout = Layout::new(&[Page::new(
//!     "123",
//!     &[
//!         &KeyAction::chars(b"123"),
//!         &KeyAction::chars(b"456"),
//!         &KeyAction::chars(b"789"),
//!         &[KeyAction::Backspace, KeyAction::Char('0'), KeyAction::Enter],
//!     ],
//! )]);
//! ```

/// What a key of a [`Layout`] does.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KeyAction {
    /// Types a character (upper-cased while Shift is active).
    Char(char),
    /// Shift: once for the next character, twice for caps lock, three times to release.
    Shift,
    /// Deletes the previous character.
    Backspace,
    /// Types a space.
    Space,
    /// Confirms the input.
    Enter,
    /// Leaves the input (cancel / go back).
    Back,
    /// Switches to the page at this index of the layout.
    Page(u8),
}

/// Event returned by the keyboard when a key is pressed.
///
/// [`KeyAction::Shift`] and [`KeyAction::Page`] are handled by the keyboard
/// itself and emit nothing; [`KeyAction::Space`] emits `Char(' ')`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyEvent {
    /// A character to insert, already upper-cased if Shift was active.
    Char(char),
    /// Deletes the previous character.
    Backspace,
    /// Confirms the input.
    Enter,
    /// Leaves the input (cancel / go back).
    Back,
}

impl KeyAction {
    /// Relative width of the key when placed in the action row.
    pub const fn weight(self) -> u32 {
        match self {
            Self::Space => 4,
            _ => 2,
        }
    }

    /// Builds a row of [`KeyAction::Char`] keys from ASCII bytes, in a `const` context.
    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
    }
}

/// A page of keys. The last row is the action row.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Page {
    /// Label of the [`KeyAction::Page`] keys that lead to this page.
    pub label: &'static str,
    /// Rows of keys, from top to bottom.
    pub rows: &'static [&'static [KeyAction]],
}

impl Page {
    /// Creates a page.
    pub const fn new(label: &'static str, rows: &'static [&'static [KeyAction]]) -> Self {
        Self { label, rows }
    }
}

/// A complete keyboard layout: one or more pages, the first one shown initially.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Layout {
    /// Pages of the layout.
    pub pages: &'static [Page],
}

impl Layout {
    /// Creates a layout.
    pub const fn new(pages: &'static [Page]) -> Self {
        Self { pages }
    }

    /// English QWERTY letters, plus a page of digits and symbols.
    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,
        ],
    ],
);

/// Labels of the control keys.
///
/// To show icons instead of text, use a font with icon glyphs in the
/// [`KeyboardTheme`](crate::KeyboardTheme) and put those glyphs here.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Labels {
    /// Shift key, off or active for one character.
    pub shift: &'static str,
    /// Shift key while caps lock is on.
    pub caps_lock: &'static str,
    /// Backspace key.
    pub backspace: &'static str,
    /// Space bar.
    pub space: &'static str,
    /// Enter key.
    pub enter: &'static str,
    /// Back key.
    pub back: &'static str,
}

impl Labels {
    /// Short English labels, fitting narrow keys.
    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
    }
}