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