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 fe4e8646e2bc45a602ac725b08a79092958a6784 3663 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
#[allow(dead_code)]
38
pub enum Urgency {
39
    Normal,
40
    Success,
41
    Warning,
42
    Critical,
43
}
44
45
#[derive(Deserialize, Serialize)]
46
pub struct UrgencyConfig {
47
    default_title: String,
48
    title_style: StyleConfig,
49
    border: Border,
50
    default_duration_secs: 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(
81
                duration.unwrap_or(urgency_config.default_duration_secs),
82
            ),
83
        }
84
    }
85
}
86
87
pub fn draw(f: &mut Frame, config: &ToastConfig, toasts: &[Toast]) {
88
    let visible_height = f.area().height;
89
90
    for (index, toast) in toasts.iter().enumerate() {
91
        let top_margin = config.margin.top + (index as u16 * 4);
92
        if top_margin >= visible_height - 4 {
93
            break;
94
        };
95
96
        let area = popup::popup_rect(
97
            f,
98
            &PopupConfig {
99
                width: 4 + toast.msg.chars().count() as u16, // 4 = borders(2) + padding(2)
100
                height: 4,                                   // 4 = borders(2) + title(1) + msg(1)
101
                position: config.position,
102
                margin: Margin {
103
                    top: top_margin,
104
                    bottom: config.margin.bottom,
105
                    left: config.margin.left,
106
                    right: config.margin.right,
107
                },
108
            },
109
        );
110
111
        let urgency_config = match toast.urgency {
112
            Urgency::Normal => &config.normal,
113
            Urgency::Success => &config.success,
114
            Urgency::Warning => &config.warning,
115
            Urgency::Critical => &config.critical,
116
        };
117
118
        let title = Line::raw(toast.title.as_ref())
119
            .style(urgency_config.title_style.format().unwrap_or_default())
120
            .alignment(Alignment::Center);
121
        let msg = Line::raw(toast.msg.as_ref()).alignment(Alignment::Center);
122
123
        let block = Block::bordered()
124
            .border_style(
125
                urgency_config
126
                    .border
127
                    .style_normal
128
                    .format()
129
                    .unwrap_or_default(),
130
            )
131
            .border_type(urgency_config.border.type_normal);
132
        f.render_widget(Paragraph::new(vec![title, msg]).block(block), area);
133
    }
134
}