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 b9addc47fedb4d5b307c9412dc7d703a86401b00 3478 bytes (plain)
1
use std::{borrow::Cow, time::Duration};
2
3
use ratatui::{
4
    Frame,
5
    layout::Alignment,
6
    text::Line,
7
    widgets::{Block, Paragraph},
8
};
9
use serde::{Deserialize, Serialize};
10
11
use crate::ui::{
12
    area::{Border, Position},
13
    margin::Margin,
14
    popup::{self, PopupConfig},
15
    style_config::StyleConfig,
16
};
17
18
#[derive(Clone)]
19
pub struct Toast {
20
    pub title: Cow<'static, str>,
21
    pub msg: Cow<'static, str>,
22
    pub urgency: Urgency,
23
    pub duration: Duration,
24
}
25
26
#[derive(Deserialize, Serialize)]
27
pub struct ToastConfig {
28
    normal: UrgencyConfig,
29
    success: UrgencyConfig,
30
    warning: UrgencyConfig,
31
    critical: UrgencyConfig,
32
    position: Position,
33
    margin: Margin,
34
}
35
36
#[derive(Debug, Clone, Copy, PartialEq)]
37
pub enum Urgency {
38
    Normal,
39
    Success,
40
    #[allow(dead_code)]
41
    Warning,
42
    Critical,
43
}
44
45
#[derive(Default, Deserialize, Serialize)]
46
pub struct UrgencyConfig {
47
    default_title: String,
48
    title_style: StyleConfig,
49
    border: Border,
50
    default_duration: f32,
51
}
52
53
impl Toast {
54
    pub fn new<T>(
55
        config: &ToastConfig,
56
        title: Option<T>,
57
        msg: T,
58
        urgency: Urgency,
59
        duration: Option<f32>,
60
    ) -> Self
61
    where
62
        T: Into<Cow<'static, str>>,
63
    {
64
        let urgency_config = match urgency {
65
            Urgency::Normal => &config.normal,
66
            Urgency::Success => &config.success,
67
            Urgency::Warning => &config.warning,
68
            Urgency::Critical => &config.critical,
69
        };
70
71
        let title: Cow<'static, str> = match title {
72
            Some(t) => t.into(),
73
            None => urgency_config.default_title.clone().into(),
74
        };
75
76
        Self {
77
            title,
78
            msg: msg.into(),
79
            urgency,
80
            duration: Duration::from_secs_f32(duration.unwrap_or(urgency_config.default_duration)),
81
        }
82
    }
83
}
84
85
pub fn draw(f: &mut Frame, config: &ToastConfig, toasts: &[Toast]) {
86
    for (index, toast) in toasts.iter().enumerate() {
87
        let area = popup::popup_rect(
88
            f,
89
            &PopupConfig {
90
                width: 4 + toast.msg.chars().count() as u16, // 4 = borders(2) + padding(2)
91
                height: 4,                                   // 4 = borders(2) + title(1) + msg(1)
92
                position: config.position,
93
                margin: Margin {
94
                    top: config.margin.top + (index as u16 * 4),
95
                    bottom: config.margin.bottom,
96
                    left: config.margin.left,
97
                    right: config.margin.right,
98
                },
99
            },
100
        );
101
102
        let urgency_config = match toast.urgency {
103
            Urgency::Normal => &config.normal,
104
            Urgency::Success => &config.success,
105
            Urgency::Warning => &config.warning,
106
            Urgency::Critical => &config.critical,
107
        };
108
109
        let title = Line::raw(toast.title.as_ref())
110
            .style(urgency_config.title_style.format().unwrap_or_default())
111
            .alignment(Alignment::Center);
112
        let msg = Line::raw(toast.msg.as_ref()).alignment(Alignment::Center);
113
114
        let block = Block::bordered()
115
            .border_style(
116
                urgency_config
117
                    .border
118
                    .style_normal
119
                    .format()
120
                    .unwrap_or_default(),
121
            )
122
            .border_type(urgency_config.border.type_normal);
123
        f.render_widget(Paragraph::new(vec![title, msg]).block(block), area);
124
    }
125
}