faststep 0.1.0

UIKit-inspired embedded UI framework built on embedded-graphics
Documentation
use crate::Localized;

/// Semantic alert kind.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AlertKind {
    /// Error or destructive feedback.
    Error,
    /// Warning feedback.
    Warning,
    /// Confirmation dialog.
    Confirm,
}

/// Semantic role for one alert action button.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AlertButtonRole {
    /// Primary action.
    Primary,
    /// Secondary action.
    Secondary,
    /// Destructive action.
    Destructive,
}

/// One alert action definition.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AlertAction<'a> {
    /// Action identifier returned by touch handling.
    pub id: u8,
    /// Localized action label.
    pub label: Localized<'a>,
    /// Semantic button role.
    pub role: AlertButtonRole,
}

/// Complete alert specification for a fixed number of actions.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AlertSpec<'a, const N: usize> {
    /// Semantic alert kind.
    pub kind: AlertKind,
    /// Localized title.
    pub title: Localized<'a>,
    /// Localized message body.
    pub message: Localized<'a>,
    /// Action definitions displayed in the alert.
    pub actions: [AlertAction<'a>; N],
}

impl<'a> AlertSpec<'a, 1> {
    /// Creates a one-button error alert.
    pub const fn error(message: Localized<'a>) -> Self {
        Self {
            kind: AlertKind::Error,
            title: Localized::new("fs.alert.error", "Error"),
            message,
            actions: [AlertAction {
                id: 0,
                label: Localized::new("fs.alert.ok", "OK"),
                role: AlertButtonRole::Destructive,
            }],
        }
    }

    /// Creates a one-button warning alert.
    pub const fn warning(message: Localized<'a>) -> Self {
        Self {
            kind: AlertKind::Warning,
            title: Localized::new("fs.alert.warning", "Warning"),
            message,
            actions: [AlertAction {
                id: 0,
                label: Localized::new("fs.alert.ok", "OK"),
                role: AlertButtonRole::Primary,
            }],
        }
    }
}

impl<'a> AlertSpec<'a, 2> {
    /// Creates a two-button confirmation alert.
    pub const fn confirm(title: Localized<'a>, message: Localized<'a>) -> Self {
        Self {
            kind: AlertKind::Confirm,
            title,
            message,
            actions: [
                AlertAction {
                    id: 0,
                    label: Localized::new("fs.alert.cancel", "Cancel"),
                    role: AlertButtonRole::Secondary,
                },
                AlertAction {
                    id: 1,
                    label: Localized::new("fs.alert.confirm", "Confirm"),
                    role: AlertButtonRole::Primary,
                },
            ],
        }
    }
}