index : nmrs-tui
A fast, Vim-friendly NetworkManager TUI built in Rust.
about summary refs log tree commit diff
nmrs-tui / src / ui / toast.rs
-rw-r--r-- blob 0a1ba0c4f5a7c96c81b7ac03c7e4441df30c88be 2963 bytes (plain)
1
use std::{borrow::Cow, time::Duration};
2
3
use ratatui::{
4
    Frame, layout::Alignment, style::Style, text::Line, widgets::{Block, Paragraph}
5
};
6
7
use crate::ui::{
8
    Gaps, Position,
9
    popup::{self, Options},
10
};
11
12
#[derive(Clone, Copy, PartialEq)]
13
pub enum Urgency {
14
    Normal,
15
    Success,
16
    Warning,
17
    Critical,
18
}
19
20
#[derive(Clone)]
21
pub struct Toast {
22
    /// Toast title.
23
    pub title: Cow<'static, str>,
24
    /// Toast message.
25
    pub msg: Cow<'static, str>,
26
    /// Toast Urgency.
27
    pub urgency: Urgency,
28
    /// Toast Duration.
29
    pub duration: Duration,
30
}
31
32
impl Toast {
33
    pub fn new<S>(title: Option<S>, msg: S, urgency: Urgency, duration: Option<Duration>) -> Self
34
    where
35
        S: Into<Cow<'static, str>>,
36
    {
37
        let default_duration = match urgency {
38
            Urgency::Normal | Urgency::Success => Duration::from_secs_f32(3.0),
39
            Urgency::Warning => Duration::from_secs_f32(6.0),
40
            Urgency::Critical => Duration::from_secs_f32(10.0),
41
        };
42
43
        let title_unwraped: Cow<'static, str> = match title {
44
            Some(t) => t.into(),
45
            None => match urgency {
46
                Urgency::Normal => "Info".into(),
47
                Urgency::Success => "Success".into(),
48
                Urgency::Warning => "Warning".into(),
49
                Urgency::Critical => "Critical".into(),
50
            },
51
        };
52
53
        Self {
54
            title: title_unwraped,
55
            msg: msg.into(),
56
            urgency,
57
            duration: duration.unwrap_or(default_duration),
58
        }
59
    }
60
}
61
62
pub fn draw(f: &mut Frame, toasts: &[Toast]) {
63
    for (index, toast) in toasts.iter().enumerate() {
64
        let area = popup::popup_area(
65
            f,
66
            Options {
67
                width: 3 + toast.msg.chars().count() as u16,
68
                height: 4,
69
                position: Position::RightTop,
70
                gaps: Gaps {
71
                    top: 1 + (index as u16 * 4),
72
                    right: 1,
73
                    ..Default::default()
74
                },
75
            },
76
        );
77
78
        let border_style = match toast.urgency {
79
            Urgency::Normal => Style::new().gray(),
80
            Urgency::Success => Style::new().green(),
81
            Urgency::Warning => Style::new().yellow(),
82
            Urgency::Critical => Style::new().red(),
83
        };
84
85
        let title_style = match toast.urgency {
86
            Urgency::Normal => Style::new().gray(),
87
            Urgency::Success => Style::new().green(),
88
            Urgency::Warning => Style::new().yellow(),
89
            Urgency::Critical => Style::new().red(),
90
        };
91
92
        let title = Line::raw(toast.title.as_ref()).style(title_style.bold()).alignment(Alignment::Center);
93
        let msg = Line::raw(toast.msg.as_ref());
94
95
        let block = Block::bordered().border_style(border_style).border_type(ratatui::widgets::BorderType::Thick);
96
        f.render_widget(Paragraph::new(vec![title, msg]).block(block), area);
97
    }
98
}