use ratatui::{
buffer::Buffer,
layout::Rect,
text::Line,
style::Color,
widgets::Widget,
};
use crate::{BorderType, ConfirmationPopup};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
pub enum ConfirmationVariant {
Success,
Warning,
Danger,
None,
}
#[derive(Clone)]
pub struct HefestoConfirmationPopup<'a> {
title: &'a str,
border_title: Option<&'a str>,
body: Vec<Line<'a>>,
variant: ConfirmationVariant,
confirm_label: &'a str,
cancel_label: &'a str,
border_type: BorderType,
position: Option<Rect>,
}
impl<'a> HefestoConfirmationPopup<'a> {
fn border_color(variant: ConfirmationVariant) -> Color {
match variant {
ConfirmationVariant::Success => Color::Green,
ConfirmationVariant::Warning => Color::Yellow,
ConfirmationVariant::Danger => Color::Red,
ConfirmationVariant::None => Color::Gray,
}
}
fn confirm_color(variant: ConfirmationVariant) -> Color {
match variant {
ConfirmationVariant::Success => Color::Green,
ConfirmationVariant::Warning => Color::Yellow,
ConfirmationVariant::Danger => Color::Red,
ConfirmationVariant::None => Color::Blue,
}
}
fn build_inner(&self) -> ConfirmationPopup<'a> {
let mut popup = ConfirmationPopup::new()
.title(self.title)
.body(self.body.clone())
.border_color(Self::border_color(self.variant))
.confirm_bg(Self::confirm_color(self.variant))
.cancel_bg(Color::Gray)
.border_type(self.border_type)
.options(self.confirm_label, self.cancel_label);
if let Some(bt) = self.border_title {
popup = popup.border_title(bt);
}
if let Some(pos) = self.position {
popup = popup.position(pos);
}
popup
}
pub fn new(title: &'a str, body: Vec<Line<'a>>, variant: ConfirmationVariant) -> Self {
Self {
title,
border_title: None,
body,
variant,
confirm_label: "Confirmar",
cancel_label: "Cancelar",
border_type: BorderType::Rounded,
position: None,
}
}
pub fn options(mut self, confirm: &'a str, cancel: &'a str) -> Self {
self.confirm_label = confirm;
self.cancel_label = cancel;
self
}
#[allow(dead_code)]
pub fn border_type(mut self, border_type: BorderType) -> Self {
self.border_type = border_type;
self
}
#[allow(dead_code)]
pub fn border_title(mut self, border_title: &'a str) -> Self {
self.border_title = Some(border_title);
self
}
#[allow(dead_code)]
pub fn position(mut self, position: Rect) -> Self {
self.position = Some(position);
self
}
}
impl Widget for HefestoConfirmationPopup<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
self.build_inner().render(area, buf);
}
}