use embedded_graphics::{
mono_font::{
MonoTextStyleBuilder,
ascii::{FONT_7X14, FONT_9X18_BOLD},
},
pixelcolor::Rgb565,
prelude::*,
primitives::{PrimitiveStyle, PrimitiveStyleBuilder, Rectangle, RoundedRectangle},
text::{Alignment, Baseline, Text, TextStyleBuilder},
};
use crate::alert_layout::{alert_layout, alert_layout_in_panel, button_frames, message_top};
use crate::{
Button, ButtonKind, ButtonSpec, ButtonTouchResponse, ButtonTouchState, FsTheme, I18n,
TouchEvent,
};
use super::{AlertButtonRole, AlertKind, AlertSpec};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AlertView<'a, const N: usize> {
pub spec: AlertSpec<'a, N>,
touch_state: ButtonTouchState<u8>,
}
impl<'a, const N: usize> AlertView<'a, N> {
pub const fn new(spec: AlertSpec<'a, N>) -> Self {
Self {
spec,
touch_state: ButtonTouchState::new(),
}
}
pub fn clear_touch_state(&mut self) {
self.touch_state.clear();
}
pub fn panel(&self, bounds: Rectangle, theme: &FsTheme, i18n: &I18n<'a>) -> Rectangle {
alert_layout::<N>(bounds, theme, i18n.text(self.spec.message)).panel
}
pub fn handle_touch(&mut self, touch: TouchEvent, panel: Rectangle) -> ButtonTouchResponse<u8> {
let buttons = self.buttons(panel);
self.touch_state.handle_touch(touch, &buttons)
}
pub fn draw<D>(&self, display: &mut D, panel: Rectangle, theme: &FsTheme, i18n: &I18n<'a>)
where
D: embedded_graphics::draw_target::DrawTarget<Color = Rgb565>,
{
self.draw_state(display, panel, theme, i18n);
}
pub fn draw_state<D>(&self, display: &mut D, panel: Rectangle, theme: &FsTheme, i18n: &I18n<'a>)
where
D: embedded_graphics::draw_target::DrawTarget<Color = Rgb565>,
{
let layout = alert_layout_in_panel::<N>(panel, i18n.text(self.spec.message));
RoundedRectangle::with_equal_corners(
Rectangle::new(panel.top_left + Point::new(6, 8), panel.size),
Size::new(22, 22),
)
.into_styled(PrimitiveStyle::with_fill(theme.dim))
.draw(display)
.ok();
let panel_style = PrimitiveStyleBuilder::new()
.fill_color(theme.surface)
.stroke_color(theme.surface_alt)
.stroke_width(2)
.build();
RoundedRectangle::with_equal_corners(panel, Size::new(22, 22))
.into_styled(panel_style)
.draw(display)
.ok();
let title_style = MonoTextStyleBuilder::new()
.font(&FONT_9X18_BOLD)
.text_color(kind_color(self.spec.kind, theme))
.build();
let message_style = MonoTextStyleBuilder::new()
.font(&FONT_7X14)
.text_color(theme.text_secondary)
.build();
let text_style = TextStyleBuilder::new()
.alignment(Alignment::Center)
.baseline(Baseline::Middle)
.build();
Text::with_text_style(
i18n.text(self.spec.title),
panel.center() + Point::new(0, -40),
title_style,
text_style,
)
.draw(display)
.ok();
for (index, line) in layout.message_lines.iter().enumerate() {
Text::with_text_style(
line,
Point::new(panel.center().x, message_top(panel) + (index as i32 * 18)),
message_style,
text_style,
)
.draw(display)
.ok();
}
for button in self.buttons(panel) {
button.draw_state(
display,
theme,
i18n,
self.touch_state.is_highlighted(&button),
);
}
}
fn buttons(&self, panel: Rectangle) -> [Button<'a, u8>; N] {
core::array::from_fn(|index| {
let action = self.spec.actions[index];
Button::new(
button_frames::<N>(panel)[index],
ButtonSpec {
key: action.id,
icon: None,
label: action.label,
kind: button_kind(action.role),
},
)
})
}
}
fn kind_color(kind: AlertKind, theme: &FsTheme) -> Rgb565 {
match kind {
AlertKind::Error => theme.danger,
AlertKind::Warning => theme.warning,
AlertKind::Confirm => theme.accent,
}
}
fn button_kind(role: AlertButtonRole) -> ButtonKind {
match role {
AlertButtonRole::Primary => ButtonKind::Primary,
AlertButtonRole::Secondary => ButtonKind::Secondary,
AlertButtonRole::Destructive => ButtonKind::Destructive,
}
}