embedded-graphics-keyboard 0.2.1

no_std on-screen keyboard and text field widgets for embedded-graphics, driven by buttons or touch
Documentation
#![no_std]
#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]
//! `no_std` on-screen keyboard and text field widgets for `embedded-graphics`,
//! driven by navigation buttons and/or a touch screen.
//!
//! - [`Keyboard`]: QWERTY by default, or any [`Layout`]. Every letter key has the
//!   same size; the bottom action row spans the full width.
//! - [`TextField`]: single-line input with an optional title and a configurable
//!   [`FrameStyle`].
//!
//! Keys are styled by a [`KeyboardTheme`]: one [`KeyStyle`] (frame and font)
//! per state. Text goes through the [`FontStyle`] trait, implemented for `MonoTextStyle`
//! and, with the `glyphix` feature, for `GlyphixStyle`.
//!
//! # Example
//!
//! ```
//! use embedded_graphics::{
//!     mono_font::{ascii::FONT_10X20, MonoTextStyle},
//!     pixelcolor::Rgb565,
//!     prelude::*,
//!     primitives::Rectangle,
//! };
//! use embedded_graphics_keyboard::{
//!     FrameStyle, KeyEvent, KeyStyle, Keyboard, KeyboardConfig, KeyboardTheme, NavButton,
//!     TextField, TextFieldStyle,
//! };
//!
//! fn run<D: DrawTarget<Color = Rgb565>>(display: &mut D) -> Result<(), D::Error> {
//!     let white_text = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
//!     let black_text = MonoTextStyle::new(&FONT_10X20, Rgb565::BLACK);
//!
//!     // One style per key state, the others derived from the idle one.
//!     let idle = KeyStyle::new(white_text)
//!         .with_fill(Rgb565::CSS_DARK_SLATE_GRAY)
//!         .with_border(Rgb565::CSS_GRAY, 1)
//!         .with_corner_radius(8);
//!     let theme = KeyboardTheme {
//!         idle,
//!         selected: idle.with_fill(Rgb565::CSS_DODGER_BLUE).with_font(black_text),
//!         pressed: idle.with_fill(Rgb565::WHITE).with_font(black_text),
//!     };
//!
//!     let area = Rectangle::new(Point::new(4, 76), Size::new(472, 240));
//!     let mut keyboard = Keyboard::new(KeyboardConfig::new(area, theme))
//!         .expect("every label fits inside its key");
//!
//!     // Text field with a title, aligned on the keys.
//!     let field_style = TextFieldStyle::new(white_text, white_text).with_frame(
//!         FrameStyle::new()
//!             .with_fill(Rgb565::BLACK)
//!             .with_border(Rgb565::YELLOW, 2)
//!             .with_corner_radius(8),
//!     );
//!     let keys = keyboard.keys_bounds();
//!     let field_bounds = Rectangle::new(
//!         Point::new(keys.top_left.x, 4),
//!         Size::new(keys.size.width, field_style.height(true)),
//!     );
//!
//!     let mut text = String::new();
//!     // In your event loop: feed button presses or touches...
//!     if let Some(event) = keyboard.handle_button(NavButton::Validate) {
//!         match event {
//!             KeyEvent::Char(c) => text.push(c),
//!             KeyEvent::Backspace => drop(text.pop()),
//!             KeyEvent::Enter => { /* submit */ }
//!             KeyEvent::Back => { /* cancel */ }
//!             KeyEvent::CursorLeft | KeyEvent::CursorRight => { /* see with_field_navigation */ }
//!             _ => {} // `KeyEvent` is `#[non_exhaustive]`.
//!         }
//!     }
//!     keyboard.release();
//!
//!     // ...then draw.
//!     TextField::new(field_bounds, &text, &field_style)
//!         .with_title("Wi-Fi password")
//!         .draw(display)?;
//!     // Only the keys that changed since the last call are sent to the display.
//!     keyboard.redraw(display, Rgb565::BLACK)
//! }
//! # fn main() {
//! #     let mut display = embedded_graphics_simulator::SimulatorDisplay::<Rgb565>::new(Size::new(480, 320));
//! #     run(&mut display).unwrap();
//! # }
//! ```

mod error;
mod keyboard;
mod layout;
mod style;
mod text_field;

pub use error::{ConfigError, KeyPosition};
pub use keyboard::{Key, Keyboard, KeyboardConfig, NavButton, ShiftState};
pub use layout::{KeyAction, KeyEvent, Labels, Layout, Page};
pub use style::{FontStyle, FrameStyle, KeyStyle, KeyboardTheme};
pub use text_field::{CursorStyle, TextField, TextFieldStyle};