use embedded_graphics::{geometry::Size, pixelcolor::PixelColor, primitives::Rectangle};
use super::{ShiftState, grid::Grid, render::label};
use crate::{
error::{ConfigError, KeyPosition, max_label_size},
layout::{KeyAction, Labels, Layout, Page},
style::{FontStyle, FrameStyle, KeyboardTheme},
};
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct KeyboardConfig<C, S> {
pub bounds: Rectangle,
pub theme: KeyboardTheme<C, S>,
pub layout: Layout,
pub labels: Labels,
pub spacing: u32,
pub padding: u32,
pub key_padding: u32,
pub frame: FrameStyle<C>,
pub field_navigation: bool,
}
impl<C: PixelColor, S> KeyboardConfig<C, S> {
#[must_use]
pub const fn new(bounds: Rectangle, theme: KeyboardTheme<C, S>) -> Self {
Self {
bounds,
theme,
layout: Layout::QWERTY,
labels: Labels::DEFAULT,
spacing: 4,
padding: 0,
key_padding: 2,
frame: FrameStyle::new(),
field_navigation: false,
}
}
#[must_use]
pub const fn with_layout(mut self, layout: Layout) -> Self {
self.layout = layout;
self
}
#[must_use]
pub const fn with_labels(mut self, labels: Labels) -> Self {
self.labels = labels;
self
}
#[must_use]
pub const fn with_spacing(mut self, spacing: u32) -> Self {
self.spacing = spacing;
self
}
#[must_use]
pub const fn with_padding(mut self, padding: u32) -> Self {
self.padding = padding;
self
}
#[must_use]
pub const fn with_key_padding(mut self, key_padding: u32) -> Self {
self.key_padding = key_padding;
self
}
#[must_use]
pub const fn with_frame(mut self, frame: FrameStyle<C>) -> Self {
self.frame = frame;
self
}
#[must_use]
pub const fn with_field_navigation(mut self, enabled: bool) -> Self {
self.field_navigation = enabled;
self
}
pub(super) fn keys_area(&self) -> Rectangle {
self.frame
.content_area(self.bounds)
.offset(-(self.padding as i32))
}
}
impl<C: PixelColor, S: FontStyle<C>> KeyboardConfig<C, S> {
pub(super) fn validate(&self) -> Result<Grid, ConfigError> {
let config = self;
let layout = &config.layout;
let has_empty =
|page: &Page| page.rows.is_empty() || page.rows.iter().any(|r| r.is_empty());
if layout.pages.is_empty() || layout.pages.iter().any(has_empty) {
return Err(ConfigError::EmptyLayout);
}
let grid = Grid::new(layout, config.keys_area(), config.spacing);
let mut buf = [0; 4];
for (page_idx, page) in layout.pages.iter().enumerate() {
for (row, keys) in page.rows.iter().enumerate() {
for (col, &action) in keys.iter().enumerate() {
let position = KeyPosition {
page: page_idx,
row,
col,
};
let pages = layout.pages.len();
if matches!(action, KeyAction::Page(target) if target as usize >= pages) {
return Err(ConfigError::InvalidPage(position));
}
let max = max_label_size(grid.key_rect(page, row, col), config.key_padding);
let mut size = Size::zero();
for shift in [ShiftState::Off, ShiftState::Locked] {
let text = label(action, shift, layout, &config.labels, &mut buf);
for style in config.theme.styles() {
size = size.component_max(style.font.measure(text));
}
}
if size.width > max.width || size.height > max.height {
return Err(ConfigError::LabelTooLarge {
position,
size,
max,
});
}
}
}
}
Ok(grid)
}
}