| 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::{PopupConfig, Popups}, |
| 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 ToastConfig { |
| 54 |
fn urgency(&self, urgency: Urgency) -> &UrgencyConfig { |
| 55 |
match urgency { |
| 56 |
Urgency::Normal => &self.normal, |
| 57 |
Urgency::Success => &self.success, |
| 58 |
Urgency::Warning => &self.warning, |
| 59 |
Urgency::Critical => &self.critical, |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl Toast { |
| 65 |
pub fn new<T>( |
| 66 |
config: &ToastConfig, |
| 67 |
title: Option<T>, |
| 68 |
msg: T, |
| 69 |
urgency: Urgency, |
| 70 |
duration: Option<f32>, |
| 71 |
) -> Self |
| 72 |
where |
| 73 |
T: Into<Cow<'static, str>>, |
| 74 |
{ |
| 75 |
let urgency_config = config.urgency(urgency); |
| 76 |
|
| 77 |
let title: Cow<'static, str> = match title { |
| 78 |
Some(t) => t.into(), |
| 79 |
None => urgency_config.default_title.clone().into(), |
| 80 |
}; |
| 81 |
|
| 82 |
Self { |
| 83 |
title, |
| 84 |
msg: msg.into(), |
| 85 |
urgency, |
| 86 |
duration: Duration::from_secs_f32( |
| 87 |
duration.unwrap_or(urgency_config.default_duration_secs), |
| 88 |
), |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
pub fn draw(f: &mut Frame, config: &ToastConfig, toasts: &[Toast]) { |
| 94 |
let visible_height = f.area().height; |
| 95 |
|
| 96 |
for (index, toast) in toasts.iter().enumerate() { |
| 97 |
let top_margin = config.margin.top + (index as u16 * 4); |
| 98 |
if top_margin >= visible_height - 4 { |
| 99 |
break; |
| 100 |
}; |
| 101 |
|
| 102 |
let area = Popups::rect( |
| 103 |
f, |
| 104 |
&PopupConfig { |
| 105 |
width: 4 + toast.msg.chars().count() as u16, |
| 106 |
height: 4, |
| 107 |
position: config.position, |
| 108 |
margin: Margin { |
| 109 |
top: top_margin, |
| 110 |
bottom: config.margin.bottom, |
| 111 |
left: config.margin.left, |
| 112 |
right: config.margin.right, |
| 113 |
}, |
| 114 |
}, |
| 115 |
); |
| 116 |
|
| 117 |
let urgency_config = config.urgency(toast.urgency); |
| 118 |
|
| 119 |
let title = Line::raw(toast.title.as_ref()) |
| 120 |
.style(urgency_config.title_style.format().unwrap_or_default()) |
| 121 |
.alignment(Alignment::Center); |
| 122 |
let msg = Line::raw(toast.msg.as_ref()).alignment(Alignment::Center); |
| 123 |
|
| 124 |
let block = Block::bordered() |
| 125 |
.border_style( |
| 126 |
urgency_config |
| 127 |
.border |
| 128 |
.style_normal |
| 129 |
.format() |
| 130 |
.unwrap_or_default(), |
| 131 |
) |
| 132 |
.border_type(urgency_config.border.type_normal); |
| 133 |
f.render_widget(Paragraph::new(vec![title, msg]).block(block), area); |
| 134 |
} |
| 135 |
} |