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