feat: Refactor all keybinding tips into a dynamically changing menu that can be invoked via '?' [#32]
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
use crate::app::context_clues::{build_context_clue_string, CONFIRMATION_PROMPT_CONTEXT_CLUES};
|
||||
use crate::ui::styles::ManagarrStyle;
|
||||
use crate::ui::utils::{layout_paragraph_borderless, title_block_centered};
|
||||
use crate::ui::widgets::button::Button;
|
||||
@@ -6,7 +5,6 @@ use crate::ui::widgets::checkbox::Checkbox;
|
||||
use derive_setters::Setters;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::{Constraint, Flex, Layout, Rect};
|
||||
use ratatui::text::Text;
|
||||
use ratatui::widgets::{Paragraph, Widget};
|
||||
use std::iter;
|
||||
|
||||
@@ -40,16 +38,12 @@ impl ConfirmationPrompt<'_> {
|
||||
|
||||
fn render_confirmation_prompt_with_checkboxes(self, area: Rect, buf: &mut Buffer) {
|
||||
title_block_centered(self.title).render(area, buf);
|
||||
let help_text =
|
||||
Text::from(build_context_clue_string(&CONFIRMATION_PROMPT_CONTEXT_CLUES).help());
|
||||
let help_paragraph = Paragraph::new(help_text).centered();
|
||||
|
||||
if let Some(checkboxes) = self.checkboxes {
|
||||
let mut constraints = vec![
|
||||
Constraint::Length(4),
|
||||
Constraint::Fill(1),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(1),
|
||||
];
|
||||
constraints.splice(
|
||||
1..1,
|
||||
@@ -61,7 +55,6 @@ impl ConfirmationPrompt<'_> {
|
||||
.areas(chunks[checkboxes.len() + 2]);
|
||||
|
||||
layout_paragraph_borderless(self.prompt).render(chunks[0], buf);
|
||||
help_paragraph.render(chunks[checkboxes.len() + 3], buf);
|
||||
|
||||
checkboxes
|
||||
.into_iter()
|
||||
@@ -83,38 +76,30 @@ impl ConfirmationPrompt<'_> {
|
||||
|
||||
fn render_confirmation_prompt(self, area: Rect, buf: &mut Buffer) {
|
||||
title_block_centered(self.title).render(area, buf);
|
||||
let help_text =
|
||||
Text::from(build_context_clue_string(&CONFIRMATION_PROMPT_CONTEXT_CLUES).help());
|
||||
let help_paragraph = Paragraph::new(help_text).centered();
|
||||
|
||||
let [prompt_area, buttons_area] = if let Some(content_paragraph) = self.content {
|
||||
let [prompt_area, content_area, _, buttons_area, help_area] = Layout::vertical([
|
||||
let [prompt_area, content_area, _, buttons_area] = Layout::vertical([
|
||||
Constraint::Length(4),
|
||||
Constraint::Length(7),
|
||||
Constraint::Fill(1),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.margin(1)
|
||||
.areas(area);
|
||||
|
||||
content_paragraph.render(content_area, buf);
|
||||
help_paragraph.render(help_area, buf);
|
||||
|
||||
[prompt_area, buttons_area]
|
||||
} else {
|
||||
let [prompt_area, buttons_area, _, help_area] = Layout::vertical([
|
||||
let [prompt_area, _, buttons_area] = Layout::vertical([
|
||||
Constraint::Percentage(72),
|
||||
Constraint::Length(3),
|
||||
Constraint::Fill(0),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(3),
|
||||
])
|
||||
.margin(1)
|
||||
.flex(Flex::SpaceBetween)
|
||||
.areas(area);
|
||||
|
||||
help_paragraph.render(help_area, buf);
|
||||
|
||||
[prompt_area, buttons_area]
|
||||
};
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use crate::ui::styles::ManagarrStyle;
|
||||
use crate::ui::utils::{background_block, borderless_block, centered_rect};
|
||||
use crate::ui::utils::{background_block, centered_rect};
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::{Constraint, Layout, Rect};
|
||||
use ratatui::widgets::{Block, Clear, Paragraph, Widget, WidgetRef};
|
||||
use ratatui::widgets::{Block, Clear, Widget, WidgetRef};
|
||||
|
||||
use super::input_box::InputBox;
|
||||
|
||||
@@ -39,17 +38,10 @@ impl<'a> InputBoxPopup<'a> {
|
||||
Clear.render(popup_area, buf);
|
||||
background_block().render(popup_area, buf);
|
||||
|
||||
let [text_box_area, help_area] =
|
||||
Layout::vertical([Constraint::Length(3), Constraint::Length(1)])
|
||||
.margin(1)
|
||||
.areas(popup_area);
|
||||
let [text_box_area] = Layout::vertical([Constraint::Length(3)])
|
||||
.margin(1)
|
||||
.areas(popup_area);
|
||||
self.input_box.render_ref(text_box_area, buf);
|
||||
|
||||
let help = Paragraph::new("<esc> cancel")
|
||||
.help()
|
||||
.centered()
|
||||
.block(borderless_block());
|
||||
help.render(help_area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-29
@@ -1,9 +1,7 @@
|
||||
use crate::ui::styles::ManagarrStyle;
|
||||
use crate::ui::utils::{background_block, centered_rect, layout_block_top_border};
|
||||
use crate::ui::utils::{background_block, centered_rect};
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::{Constraint, Layout, Rect};
|
||||
use ratatui::prelude::Text;
|
||||
use ratatui::widgets::{Block, Clear, Paragraph, Widget};
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::widgets::{Block, Clear, Widget};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "popup_tests.rs"]
|
||||
@@ -25,6 +23,7 @@ pub enum Size {
|
||||
XLarge,
|
||||
XXLarge,
|
||||
Long,
|
||||
LongNarrowTable,
|
||||
}
|
||||
|
||||
impl Size {
|
||||
@@ -45,6 +44,7 @@ impl Size {
|
||||
Size::XLarge => (83, 83),
|
||||
Size::XXLarge => (90, 90),
|
||||
Size::Long => (65, 75),
|
||||
Size::LongNarrowTable => (55, 85),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,6 @@ pub struct Popup<'a, T: Widget> {
|
||||
percent_x: u16,
|
||||
percent_y: u16,
|
||||
block: Option<Block<'a>>,
|
||||
footer: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl<'a, T: Widget> Popup<'a, T> {
|
||||
@@ -64,7 +63,6 @@ impl<'a, T: Widget> Popup<'a, T> {
|
||||
percent_x: 0,
|
||||
percent_y: 0,
|
||||
block: None,
|
||||
footer: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,11 +84,6 @@ impl<'a, T: Widget> Popup<'a, T> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn footer(mut self, footer: &'a str) -> Self {
|
||||
self.footer = Some(footer);
|
||||
self
|
||||
}
|
||||
|
||||
fn render_popup(self, area: Rect, buf: &mut Buffer) {
|
||||
let mut popup_area = centered_rect(self.percent_x, self.percent_y, area);
|
||||
let height = if popup_area.height < 3 {
|
||||
@@ -109,23 +102,7 @@ impl<'a, T: Widget> Popup<'a, T> {
|
||||
block.render(popup_area, buf);
|
||||
}
|
||||
|
||||
let content_area = if let Some(footer) = self.footer {
|
||||
let [content_area, help_footer_area] =
|
||||
Layout::vertical([Constraint::Fill(0), Constraint::Length(2)])
|
||||
.margin(1)
|
||||
.areas(popup_area);
|
||||
|
||||
Paragraph::new(Text::from(format!(" {footer}").help()))
|
||||
.block(layout_block_top_border())
|
||||
.left_aligned()
|
||||
.render(help_footer_area, buf);
|
||||
|
||||
content_area
|
||||
} else {
|
||||
popup_area
|
||||
};
|
||||
|
||||
self.widget.render(content_area, buf);
|
||||
self.widget.render(popup_area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ mod tests {
|
||||
assert_eq!(Size::XLarge.to_percent(), (83, 83));
|
||||
assert_eq!(Size::XXLarge.to_percent(), (90, 90));
|
||||
assert_eq!(Size::Long.to_percent(), (65, 75));
|
||||
assert_eq!(Size::LongNarrowTable.to_percent(), (55, 85));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -31,7 +32,6 @@ mod tests {
|
||||
assert_eq!(popup.percent_x, 0);
|
||||
assert_eq!(popup.percent_y, 0);
|
||||
assert_eq!(popup.block, None);
|
||||
assert_eq!(popup.footer, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -42,7 +42,6 @@ mod tests {
|
||||
assert_eq!(popup.percent_y, 40);
|
||||
assert_eq!(popup.widget, Block::new());
|
||||
assert_eq!(popup.block, None);
|
||||
assert_eq!(popup.footer, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -53,7 +52,6 @@ mod tests {
|
||||
assert_eq!(popup.percent_y, 50);
|
||||
assert_eq!(popup.widget, Block::new());
|
||||
assert_eq!(popup.block, None);
|
||||
assert_eq!(popup.footer, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -64,17 +62,5 @@ mod tests {
|
||||
assert_eq!(popup.widget, Block::new());
|
||||
assert_eq!(popup.percent_x, 0);
|
||||
assert_eq!(popup.percent_y, 0);
|
||||
assert_eq!(popup.footer, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_popup_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, 0);
|
||||
assert_eq!(popup.percent_y, 0);
|
||||
assert_eq!(popup.block, None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user