index : nmrs-tui
A fast, Vim-friendly NetworkManager TUI built in Rust.
about summary refs log tree commit diff

Diffstat (limited to 'src/ui/toast.rs')

-rw-r--r-- src/ui/toast.rs 118
 1 file changed, 68 insertions(+), 50 deletions(-)

diff --git a/src/ui/toast.rs b/src/ui/toast.rs
index 22943ff..b9addc4 100644
--- a/src/ui/toast.rs
+++ b/src/ui/toast.rs
@@ -3,105 +3,123 @@
use ratatui::{
    Frame,
    layout::Alignment,
    style::Style,
    text::Line,
    widgets::{Block, BorderType, Paragraph},
    widgets::{Block, Paragraph},
};
use serde::{Deserialize, Serialize};

use crate::ui::{
    Margin, Position,
    popup::{self, Options},
    area::{Border, Position},
    margin::Margin,
    popup::{self, PopupConfig},
    style_config::StyleConfig,
};

#[allow(dead_code)]
#[derive(Clone)]
pub struct Toast {
    pub title: Cow<'static, str>,
    pub msg: Cow<'static, str>,
    pub urgency: Urgency,
    pub duration: Duration,
}

#[derive(Deserialize, Serialize)]
pub struct ToastConfig {
    normal: UrgencyConfig,
    success: UrgencyConfig,
    warning: UrgencyConfig,
    critical: UrgencyConfig,
    position: Position,
    margin: Margin,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Urgency {
    Normal,
    Success,
    #[allow(dead_code)]
    Warning,
    Critical,
}

#[derive(Clone)]
pub struct Toast {
    /// Toast title.
    pub title: Cow<'static, str>,
    /// Toast message.
    pub msg: Cow<'static, str>,
    /// Toast Urgency.
    pub urgency: Urgency,
    /// Toast Duration.
    pub duration: Duration,
#[derive(Default, Deserialize, Serialize)]
pub struct UrgencyConfig {
    default_title: String,
    title_style: StyleConfig,
    border: Border,
    default_duration: f32,
}

impl Toast {
    pub fn new<T>(title: Option<T>, msg: T, urgency: Urgency, duration: Option<Duration>) -> Self
    pub fn new<T>(
        config: &ToastConfig,
        title: Option<T>,
        msg: T,
        urgency: Urgency,
        duration: Option<f32>,
    ) -> Self
    where
        T: Into<Cow<'static, str>>,
    {
        let default_duration = match urgency {
            Urgency::Normal | Urgency::Success => Duration::from_secs_f32(3.0),
            Urgency::Warning => Duration::from_secs_f32(6.0),
            Urgency::Critical => Duration::from_secs_f32(10.0),
        let urgency_config = match urgency {
            Urgency::Normal => &config.normal,
            Urgency::Success => &config.success,
            Urgency::Warning => &config.warning,
            Urgency::Critical => &config.critical,
        };

        let title_unwraped: Cow<'static, str> = match title {
        let title: Cow<'static, str> = match title {
            Some(t) => t.into(),
            None => match urgency {
                Urgency::Normal => "Info".into(),
                Urgency::Success => "Success".into(),
                Urgency::Warning => "Warning".into(),
                Urgency::Critical => "Critical".into(),
            },
            None => urgency_config.default_title.clone().into(),
        };

        Self {
            title: title_unwraped,
            title,
            msg: msg.into(),
            urgency,
            duration: duration.unwrap_or(default_duration),
            duration: Duration::from_secs_f32(duration.unwrap_or(urgency_config.default_duration)),
        }
    }
}

pub fn draw(f: &mut Frame, toasts: &[Toast]) {
pub fn draw(f: &mut Frame, config: &ToastConfig, toasts: &[Toast]) {
    for (index, toast) in toasts.iter().enumerate() {
        let area = popup::popup_rect(
            f,
            Options {
            &PopupConfig {
                width: 4 + toast.msg.chars().count() as u16, // 4 = borders(2) + padding(2)
                height: 4,                                   // 4 = borders(2) + title(1) + msg(1)
                position: Position::RightTop,
                position: config.position,
                margin: Margin {
                    top: 1 + (index as u16 * 4),
                    right: 1,
                    ..Default::default()
                    top: config.margin.top + (index as u16 * 4),
                    bottom: config.margin.bottom,
                    left: config.margin.left,
                    right: config.margin.right,
                },
            },
        );

        let border_style = match toast.urgency {
            Urgency::Normal => Style::new().gray(),
            Urgency::Success => Style::new().green(),
            Urgency::Warning => Style::new().yellow(),
            Urgency::Critical => Style::new().red(),
        };

        let title_style = match toast.urgency {
            Urgency::Normal => Style::new().gray(),
            Urgency::Success => Style::new().green(),
            Urgency::Warning => Style::new().yellow(),
            Urgency::Critical => Style::new().red(),
        let urgency_config = match toast.urgency {
            Urgency::Normal => &config.normal,
            Urgency::Success => &config.success,
            Urgency::Warning => &config.warning,
            Urgency::Critical => &config.critical,
        };

        let title = Line::raw(toast.title.as_ref())
            .style(title_style.bold())
            .style(urgency_config.title_style.format().unwrap_or_default())
            .alignment(Alignment::Center);
        let msg = Line::raw(toast.msg.as_ref()).alignment(Alignment::Center);

        let block = Block::bordered()
            .border_style(border_style)
            .border_type(BorderType::Thick);
            .border_style(
                urgency_config
                    .border
                    .style_normal
                    .format()
                    .unwrap_or_default(),
            )
            .border_type(urgency_config.border.type_normal);
        f.render_widget(Paragraph::new(vec![title, msg]).block(block), area);
    }
}