use ratatui::{Frame, layout::Rect, style::Style, text::Text, widgets::Clear};
use crate::config::Theme;
const ERROR_MESSAGE_DISPLAY_TICKS: u16 = 3 * 10;
pub struct ErrorPopup<'a> {
style: Style,
message: Option<Text<'a>>,
timeout_ticks: Option<u16>,
}
impl<'a> ErrorPopup<'a> {
pub fn empty(theme: &Theme) -> Self {
Self {
style: theme.error.into(),
message: None,
timeout_ticks: None,
}
}
pub fn set_perm_message(&mut self, message: impl Into<Text<'a>>) {
self.message = Some(message.into().centered().style(self.style));
}
pub fn set_temp_message(&mut self, message: impl Into<Text<'a>>) {
self.message = Some(message.into().centered().style(self.style));
self.timeout_ticks.replace(ERROR_MESSAGE_DISPLAY_TICKS);
}
pub fn clear_message(&mut self) {
self.message = None;
self.timeout_ticks = None;
}
pub fn tick(&mut self) {
if let Some(mut remaining_ticks) = self.timeout_ticks {
if remaining_ticks > 0 {
remaining_ticks -= 1;
self.timeout_ticks.replace(remaining_ticks);
} else {
self.message = None;
self.timeout_ticks = None;
}
}
}
pub fn render_in(&mut self, frame: &mut Frame, area: Rect) {
if let Some(ref text) = self.message {
let error_overlay_rect = Rect {
x: area.x,
y: area.bottom() - 1,
width: area.width,
height: 1,
};
frame.render_widget(Clear, error_overlay_rect);
frame.render_widget(text, error_overlay_rect);
}
}
}