use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Clear, Paragraph};
use crate::delights::{ToastKind, ToastQueue};
use crate::theme::Theme;
pub(crate) fn render(
toasts: &ToastQueue,
frame: &mut Frame,
chat_area: Rect,
theme: &Theme,
now: u64,
) {
let active: Vec<_> = toasts.active_items(now).collect();
if active.is_empty() || chat_area.height == 0 || chat_area.width == 0 {
return;
}
#[allow(clippy::cast_possible_truncation)]
let n = active.len().min(3) as u16;
let y_start = chat_area.y + chat_area.height.saturating_sub(n);
for (i, toast) in active.iter().rev().take(3).enumerate() {
#[allow(clippy::cast_possible_truncation)]
let y = y_start + i as u16;
if y >= chat_area.y + chat_area.height {
break;
}
let toast_area = Rect {
x: chat_area.x,
y,
width: chat_area.width,
height: 1,
};
let style = toast_style(toast.kind, theme);
let text = format!(" {} ", toast.text);
let line = Line::from(Span::styled(text, style));
frame.render_widget(Clear, toast_area);
frame.render_widget(Paragraph::new(line), toast_area);
}
}
fn toast_style(kind: ToastKind, theme: &Theme) -> Style {
match kind {
ToastKind::Info => theme.status_bar,
ToastKind::Success => Style::default().fg(Color::Green),
ToastKind::Warn => Style::default().fg(Color::Yellow),
}
}