Refactored all popups to use centrally defined, consistent sizes.
This commit is contained in:
@@ -156,7 +156,9 @@ where
|
||||
let selectable_list = SelectableList::new(content.sort.as_mut().unwrap(), |item| {
|
||||
ListItem::new(Text::from(item.name))
|
||||
});
|
||||
Popup::new(selectable_list, 20, 50).render(table_area, buf);
|
||||
Popup::new(selectable_list)
|
||||
.dimensions(20, 50)
|
||||
.render(table_area, buf);
|
||||
}
|
||||
} else {
|
||||
loading_block.render(table_area, buf);
|
||||
|
||||
+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
|
||||
|
||||
@@ -1,55 +1,73 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::ui::widgets::popup::Popup;
|
||||
use crate::ui::widgets::popup::{Popup, Size};
|
||||
use pretty_assertions::assert_eq;
|
||||
use ratatui::layout::Alignment;
|
||||
use ratatui::widgets::Block;
|
||||
|
||||
#[test]
|
||||
fn test_dimensions_to_percent() {
|
||||
assert_eq!(Size::Prompt.to_percent(), (35, 35));
|
||||
assert_eq!(Size::LargePrompt.to_percent(), (70, 45));
|
||||
assert_eq!(Size::Error.to_percent(), (25, 8));
|
||||
assert_eq!(Size::InputBox.to_percent(), (30, 13));
|
||||
assert_eq!(Size::Dropdown.to_percent(), (20, 30));
|
||||
assert_eq!(Size::Small.to_percent(), (40, 40));
|
||||
assert_eq!(Size::Medium.to_percent(), (60, 60));
|
||||
assert_eq!(Size::Large.to_percent(), (75, 75));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_popup_new() {
|
||||
let popup = Popup::new(Block::new(), 50, 50);
|
||||
let popup = Popup::new(Block::new());
|
||||
|
||||
assert_eq!(popup.widget, Block::new());
|
||||
assert_eq!(popup.percent_x, 50);
|
||||
assert_eq!(popup.percent_y, 50);
|
||||
assert_eq!(popup.percent_x, 0);
|
||||
assert_eq!(popup.percent_y, 0);
|
||||
assert_eq!(popup.block, None);
|
||||
assert_eq!(popup.footer, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_popup_size() {
|
||||
let popup = Popup::new(Block::new()).size(Size::Small);
|
||||
|
||||
assert_eq!(popup.percent_x, 40);
|
||||
assert_eq!(popup.percent_y, 40);
|
||||
assert_eq!(popup.widget, Block::new());
|
||||
assert_eq!(popup.block, None);
|
||||
assert_eq!(popup.footer, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_popup_dimensions() {
|
||||
let popup = Popup::new(Block::new()).dimensions(25, 50);
|
||||
|
||||
assert_eq!(popup.percent_x, 25);
|
||||
assert_eq!(popup.percent_y, 50);
|
||||
assert_eq!(popup.widget, Block::new());
|
||||
assert_eq!(popup.block, None);
|
||||
assert_eq!(popup.footer, None);
|
||||
assert_eq!(popup.footer_alignment, Alignment::Left);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_popup_block() {
|
||||
let popup = Popup::new(Block::new(), 50, 50).block(Block::new());
|
||||
let popup = Popup::new(Block::new()).block(Block::new());
|
||||
|
||||
assert_eq!(popup.block, Some(Block::new()));
|
||||
assert_eq!(popup.widget, Block::new());
|
||||
assert_eq!(popup.percent_x, 50);
|
||||
assert_eq!(popup.percent_y, 50);
|
||||
assert_eq!(popup.percent_x, 0);
|
||||
assert_eq!(popup.percent_y, 0);
|
||||
assert_eq!(popup.footer, None);
|
||||
assert_eq!(popup.footer_alignment, Alignment::Left);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_popup_footer() {
|
||||
let popup = Popup::new(Block::new(), 50, 50).footer("footer");
|
||||
let popup = Popup::new(Block::new()).footer("footer");
|
||||
|
||||
assert_eq!(popup.footer, Some("footer"));
|
||||
assert_eq!(popup.widget, Block::new());
|
||||
assert_eq!(popup.percent_x, 50);
|
||||
assert_eq!(popup.percent_y, 50);
|
||||
assert_eq!(popup.percent_x, 0);
|
||||
assert_eq!(popup.percent_y, 0);
|
||||
assert_eq!(popup.block, None);
|
||||
assert_eq!(popup.footer_alignment, Alignment::Left);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_popup_footer_alignment() {
|
||||
let popup = Popup::new(Block::new(), 50, 50).footer_alignment(Alignment::Center);
|
||||
|
||||
assert_eq!(popup.footer_alignment, Alignment::Center);
|
||||
assert_eq!(popup.widget, Block::new());
|
||||
assert_eq!(popup.percent_x, 50);
|
||||
assert_eq!(popup.percent_y, 50);
|
||||
assert_eq!(popup.block, None);
|
||||
assert_eq!(popup.footer, None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user