use core::fmt;
use embedded_graphics::{geometry::Size, primitives::Rectangle};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyPosition {
pub page: usize,
pub row: usize,
pub col: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ConfigError {
EmptyLayout,
InvalidPage(KeyPosition),
LabelTooLarge(KeyPosition),
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyLayout => f.write_str("the layout has an empty page or row"),
Self::InvalidPage(p) => write!(
f,
"key at page {}, row {}, column {} points to a missing page",
p.page, p.row, p.col
),
Self::LabelTooLarge(p) => write!(
f,
"label of the key at page {}, row {}, column {} does not fit inside the key",
p.page, p.row, p.col
),
}
}
}
impl core::error::Error for ConfigError {}
pub(crate) fn size_fits(text_size: Size, bounds: Rectangle, padding: u32) -> bool {
let margin = padding.saturating_mul(2);
text_size.width <= bounds.size.width.saturating_sub(margin)
&& text_size.height <= bounds.size.height.saturating_sub(margin)
}