use tui::prelude::*;
use tui::widgets::{Block, BorderType, Borders, Clear, Paragraph};
pub struct InputPopup<'a> {
pub title: &'a str,
pub instructions: &'a str,
pub input_text: &'a str,
pub border_color: Color,
pub info: Option<&'a str>,
}
impl Widget for InputPopup<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
Clear.render(area, buf);
let [_, instruct, inp] = Layout::vertical([
Constraint::Length(1), Constraint::Length(1), Constraint::Length(1), ])
.areas(area);
Paragraph::new(format!(" {}", self.instructions)).render(instruct, buf);
Paragraph::new(format!(" {}", self.input_text)).render(inp, buf);
if let Some(info) = self.info {
let info_width = info.len() as u16 + 2; let input_width = self.input_text.len() as u16 + 2; if inp.width > info_width + input_width {
Paragraph::new(format!("{} ", info))
.alignment(Alignment::Right)
.style(Style::default().fg(Color::DarkGray))
.render(inp, buf);
}
}
Block::default()
.title(self.title)
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(self.border_color))
.render(area, buf);
}
}
pub fn create_popup(area: Rect, height: u16, percent_width: u16) -> Rect {
let [_, popup, _] = Layout::vertical(
[
Constraint::Ratio(1, 2),
Constraint::Length(height),
Constraint::Ratio(1, 2),
]
.as_ref(),
)
.areas(area);
Layout::horizontal(
[
Constraint::Percentage((100 - percent_width) / 2),
Constraint::Percentage(percent_width),
Constraint::Percentage((100 - percent_width) / 2),
]
.as_ref(),
)
.split(popup)[1]
}
pub fn popup_cursor_position(popup_rect: Rect, input_len: u16) -> (u16, u16) {
(
popup_rect.x + input_len + 1, popup_rect.y + 2, )
}