use embedded_graphics::{
Drawable,
draw_target::DrawTarget,
geometry::{AnchorPoint, Point, Size},
mono_font::MonoTextStyle,
pixelcolor::PixelColor,
primitives::{
Primitive, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle, RoundedRectangle,
StrokeAlignment,
},
text::{Baseline, Text, TextStyleBuilder, renderer::TextRenderer},
};
pub trait FontStyle<C: PixelColor> {
fn measure(&self, text: &str) -> Size;
fn draw_anchored<D: DrawTarget<Color = C>>(
&self,
target: &mut D,
text: &str,
position: Point,
anchor: AnchorPoint,
) -> Result<(), D::Error>;
fn draw_centered<D: DrawTarget<Color = C>>(
&self,
target: &mut D,
text: &str,
center: Point,
) -> Result<(), D::Error> {
self.draw_anchored(target, text, center, AnchorPoint::Center)
}
}
impl<C: PixelColor> FontStyle<C> for MonoTextStyle<'_, C> {
fn measure(&self, text: &str) -> Size {
self.measure_string(text, Point::zero(), Baseline::Top)
.bounding_box
.size
}
fn draw_anchored<D: DrawTarget<Color = C>>(
&self,
target: &mut D,
text: &str,
position: Point,
anchor: AnchorPoint,
) -> Result<(), D::Error> {
let offset = Rectangle::new(Point::zero(), self.measure(text)).anchor_point(anchor);
let style = TextStyleBuilder::new().baseline(Baseline::Top).build();
Text::with_text_style(text, position - offset, *self, style)
.draw(target)
.map(drop)
}
}
#[cfg(feature = "glyphix")]
impl<C: PixelColor> FontStyle<C> for embedded_graphics_glyphix::GlyphixStyle<'_, C> {
fn measure(&self, text: &str) -> Size {
embedded_graphics_glyphix::GlyphixStyle::measure(self, text)
}
fn draw_anchored<D: DrawTarget<Color = C>>(
&self,
target: &mut D,
text: &str,
position: Point,
anchor: AnchorPoint,
) -> Result<(), D::Error> {
embedded_graphics_glyphix::GlyphixStyle::draw_anchored(self, target, text, position, anchor)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct FrameStyle<C> {
pub fill_color: Option<C>,
pub border_color: Option<C>,
pub border_width: u32,
pub corner_radius: u32,
}
impl<C: PixelColor> FrameStyle<C> {
#[must_use]
pub const fn new() -> Self {
Self {
fill_color: None,
border_color: None,
border_width: 0,
corner_radius: 0,
}
}
#[must_use]
pub const fn with_fill(mut self, color: C) -> Self {
self.fill_color = Some(color);
self
}
#[must_use]
pub const fn with_border(mut self, color: C, width: u32) -> Self {
self.border_color = Some(color);
self.border_width = width;
self
}
#[must_use]
pub const fn with_corner_radius(mut self, radius: u32) -> Self {
self.corner_radius = radius;
self
}
#[must_use]
pub const fn effective_border_width(&self) -> u32 {
if self.border_color.is_some() {
self.border_width
} else {
0
}
}
#[must_use]
pub fn content_area(&self, bounds: Rectangle) -> Rectangle {
bounds.offset(-(self.effective_border_width() as i32))
}
fn primitive_style(&self) -> PrimitiveStyle<C> {
let mut builder = PrimitiveStyleBuilder::new().stroke_alignment(StrokeAlignment::Inside);
if let Some(fill) = self.fill_color {
builder = builder.fill_color(fill);
}
if let Some(border) = self.border_color {
builder = builder.stroke_color(border).stroke_width(self.border_width);
}
builder.build()
}
pub fn draw<D: DrawTarget<Color = C>>(
&self,
target: &mut D,
bounds: Rectangle,
) -> Result<(), D::Error> {
if self.fill_color.is_none() && self.effective_border_width() == 0 {
return Ok(());
}
let style = self.primitive_style();
if self.corner_radius > 0 {
let radius = Size::new_equal(self.corner_radius);
RoundedRectangle::with_equal_corners(bounds, radius)
.into_styled(style)
.draw(target)
} else {
bounds.into_styled(style).draw(target)
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyStyle<C, S> {
pub frame: FrameStyle<C>,
pub font: S,
}
impl<C: PixelColor, S> KeyStyle<C, S> {
#[must_use]
pub const fn new(font: S) -> Self {
Self {
frame: FrameStyle::new(),
font,
}
}
#[must_use]
pub fn with_font(mut self, font: S) -> Self {
self.font = font;
self
}
#[must_use]
pub const fn with_frame(mut self, frame: FrameStyle<C>) -> Self {
self.frame = frame;
self
}
#[must_use]
pub const fn with_fill(mut self, color: C) -> Self {
self.frame = self.frame.with_fill(color);
self
}
#[must_use]
pub const fn with_border(mut self, color: C, width: u32) -> Self {
self.frame = self.frame.with_border(color, width);
self
}
#[must_use]
pub const fn with_corner_radius(mut self, radius: u32) -> Self {
self.frame = self.frame.with_corner_radius(radius);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyboardTheme<C, S> {
pub idle: KeyStyle<C, S>,
pub selected: KeyStyle<C, S>,
pub pressed: KeyStyle<C, S>,
}
impl<C, S> KeyboardTheme<C, S> {
pub(crate) fn styles(&self) -> [&KeyStyle<C, S>; 3] {
[&self.idle, &self.selected, &self.pressed]
}
}