Refactored all popups to use centrally defined, consistent sizes.
This commit is contained in:
+43
-11
@@ -9,27 +9,64 @@ use ratatui::widgets::{Block, Clear, Paragraph, Widget};
|
||||
#[path = "popup_tests.rs"]
|
||||
mod popup_tests;
|
||||
|
||||
pub enum Size {
|
||||
Prompt,
|
||||
LargePrompt,
|
||||
Error,
|
||||
InputBox,
|
||||
Dropdown,
|
||||
Small,
|
||||
Medium,
|
||||
Large,
|
||||
}
|
||||
|
||||
impl Size {
|
||||
pub fn to_percent(&self) -> (u16, u16) {
|
||||
match self {
|
||||
Size::Prompt => (35, 35),
|
||||
Size::LargePrompt => (70, 45),
|
||||
Size::Error => (25, 8),
|
||||
Size::InputBox => (30, 13),
|
||||
Size::Dropdown => (20, 30),
|
||||
Size::Small => (40, 40),
|
||||
Size::Medium => (60, 60),
|
||||
Size::Large => (75, 75),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Popup<'a, T: Widget> {
|
||||
widget: T,
|
||||
percent_x: u16,
|
||||
percent_y: u16,
|
||||
block: Option<Block<'a>>,
|
||||
footer: Option<&'a str>,
|
||||
footer_alignment: Alignment,
|
||||
}
|
||||
|
||||
impl<'a, T: Widget> Popup<'a, T> {
|
||||
pub fn new(widget: T, percent_x: u16, percent_y: u16) -> Self {
|
||||
pub fn new(widget: T) -> Self {
|
||||
Self {
|
||||
widget,
|
||||
percent_x,
|
||||
percent_y,
|
||||
percent_x: 0,
|
||||
percent_y: 0,
|
||||
block: None,
|
||||
footer: None,
|
||||
footer_alignment: Alignment::Left,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size(mut self, size: Size) -> Self {
|
||||
let (percent_x, percent_y) = size.to_percent();
|
||||
self.percent_x = percent_x;
|
||||
self.percent_y = percent_y;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn dimensions(mut self, percent_x: u16, percent_y: u16) -> Self {
|
||||
self.percent_x = percent_x;
|
||||
self.percent_y = percent_y;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn block(mut self, block: Block<'a>) -> Self {
|
||||
self.block = Some(block);
|
||||
self
|
||||
@@ -40,11 +77,6 @@ impl<'a, T: Widget> Popup<'a, T> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn footer_alignment(mut self, alignment: Alignment) -> Self {
|
||||
self.footer_alignment = alignment;
|
||||
self
|
||||
}
|
||||
|
||||
fn render_popup(self, area: Rect, buf: &mut Buffer) {
|
||||
let popup_area = centered_rect(self.percent_x, self.percent_y, area);
|
||||
Clear.render(popup_area, buf);
|
||||
@@ -62,7 +94,7 @@ impl<'a, T: Widget> Popup<'a, T> {
|
||||
|
||||
Paragraph::new(Text::from(format!(" {footer}").help()))
|
||||
.block(layout_block_top_border())
|
||||
.alignment(self.footer_alignment)
|
||||
.alignment(Alignment::Left)
|
||||
.render(help_footer_area, buf);
|
||||
|
||||
content_area
|
||||
|
||||
Reference in New Issue
Block a user