From 512afa2fda3a679ce0dc4ed37f85b177b3a215f6 Mon Sep 17 00:00:00 2001 From: Alexandre Pasmantier <47638216+alexpasmantier@users.noreply.github.com> Date: Sat, 16 Nov 2024 20:32:40 +0100 Subject: [PATCH] feat(ui): make help bar display optional (#35) --- .config/config.toml | 2 + Cargo.lock | 2 +- Cargo.toml | 2 +- crates/television/config/ui.rs | 6 +++ crates/television/television.rs | 1 + crates/television/ui/help.rs | 8 ++-- crates/television/ui/layout.rs | 83 +++++++++++++++++++++------------ 7 files changed, 70 insertions(+), 34 deletions(-) diff --git a/.config/config.toml b/.config/config.toml index d0c4b06..172fd39 100644 --- a/.config/config.toml +++ b/.config/config.toml @@ -23,6 +23,8 @@ use_nerd_font_icons = false # │ │ # └───────────────────────────────────────┘ ui_scale = 80 +# Whether to show the top help bar in the UI +show_help_bar = true # Previewers settings # ---------------------------------------------------------------------------- diff --git a/Cargo.lock b/Cargo.lock index 6a3acd4..99f230c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2963,7 +2963,7 @@ dependencies = [ [[package]] name = "television" -version = "0.4.22" +version = "0.4.23" dependencies = [ "anyhow", "better-panic", diff --git a/Cargo.toml b/Cargo.toml index 198df22..7d26e9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "television" -version = "0.4.22" +version = "0.4.23" edition = "2021" description = "The revolution will be televised." license = "MIT" diff --git a/crates/television/config/ui.rs b/crates/television/config/ui.rs index 4030287..480c1b4 100644 --- a/crates/television/config/ui.rs +++ b/crates/television/config/ui.rs @@ -8,6 +8,7 @@ const DEFAULT_UI_SCALE: u16 = 90; pub struct UiConfig { pub use_nerd_font_icons: bool, pub ui_scale: u16, + pub show_help_bar: bool, } impl Default for UiConfig { @@ -15,6 +16,7 @@ impl Default for UiConfig { Self { use_nerd_font_icons: false, ui_scale: DEFAULT_UI_SCALE, + show_help_bar: true, } } } @@ -30,6 +32,10 @@ impl From for ValueKind { String::from("ui_scale"), ValueKind::U64(val.ui_scale.into()).into(), ); + m.insert( + String::from("show_help_bar"), + ValueKind::Boolean(val.show_help_bar).into(), + ); ValueKind::Table(m) } } diff --git a/crates/television/television.rs b/crates/television/television.rs index 5c1ae82..646aa92 100644 --- a/crates/television/television.rs +++ b/crates/television/television.rs @@ -378,6 +378,7 @@ impl Television { &Dimensions::from(self.config.ui.ui_scale), area, !matches!(self.mode, Mode::Channel), + self.config.ui.show_help_bar, ); // help bar (metadata, keymaps, logo) diff --git a/crates/television/ui/help.rs b/crates/television/ui/help.rs index 380e283..761537e 100644 --- a/crates/television/ui/help.rs +++ b/crates/television/ui/help.rs @@ -26,9 +26,11 @@ impl Television { f: &mut Frame, layout: &Layout, ) -> color_eyre::Result<()> { - self.draw_metadata_block(f, layout.help_bar_left); - self.draw_keymaps_block(f, layout.help_bar_middle)?; - draw_logo_block(f, layout.help_bar_right, mode_color(self.mode)); + if let Some(help_bar) = layout.help_bar { + self.draw_metadata_block(f, help_bar.left); + self.draw_keymaps_block(f, help_bar.middle)?; + draw_logo_block(f, help_bar.right, mode_color(self.mode)); + } Ok(()) } diff --git a/crates/television/ui/layout.rs b/crates/television/ui/layout.rs index 209ca35..c45ffd8 100644 --- a/crates/television/ui/layout.rs +++ b/crates/television/ui/layout.rs @@ -24,10 +24,25 @@ impl Default for Dimensions { } } +#[derive(Debug, Clone, Copy)] +pub struct HelpBarLayout { + pub left: Rect, + pub middle: Rect, + pub right: Rect, +} + +impl HelpBarLayout { + pub fn new(left: Rect, middle: Rect, right: Rect) -> Self { + Self { + left, + middle, + right, + } + } +} + pub struct Layout { - pub help_bar_left: Rect, - pub help_bar_middle: Rect, - pub help_bar_right: Rect, + pub help_bar: Option, pub results: Rect, pub input: Rect, pub preview_title: Rect, @@ -38,9 +53,7 @@ pub struct Layout { impl Layout { #[allow(clippy::too_many_arguments)] pub fn new( - help_bar_left: Rect, - help_bar_middle: Rect, - help_bar_right: Rect, + help_bar: Option, results: Rect, input: Rect, preview_title: Rect, @@ -48,9 +61,7 @@ impl Layout { remote_control: Option, ) -> Self { Self { - help_bar_left, - help_bar_middle, - help_bar_right, + help_bar, results, input, preview_title, @@ -63,26 +74,42 @@ impl Layout { dimensions: &Dimensions, area: Rect, with_remote: bool, + with_help_bar: bool, ) -> Self { let main_block = centered_rect(dimensions.x, dimensions.y, area); // split the main block into two vertical chunks (help bar + rest) - let hz_chunks = layout::Layout::default() - .direction(Direction::Vertical) - .constraints([Constraint::Max(9), Constraint::Fill(1)]) - .split(main_block); + let main_rect: Rect; + let help_bar_layout: Option; - // split the help bar into three horizontal chunks (left + center + right) - let help_bar_chunks = layout::Layout::default() - .direction(Direction::Horizontal) - .constraints([ - // metadata - Constraint::Fill(1), - // keymaps - Constraint::Fill(1), - // logo - Constraint::Length(24), - ]) - .split(hz_chunks[0]); + if with_help_bar { + let hz_chunks = layout::Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Max(9), Constraint::Fill(1)]) + .split(main_block); + main_rect = hz_chunks[1]; + + // split the help bar into three horizontal chunks (left + center + right) + let help_bar_chunks = layout::Layout::default() + .direction(Direction::Horizontal) + .constraints([ + // metadata + Constraint::Fill(1), + // keymaps + Constraint::Fill(1), + // logo + Constraint::Length(24), + ]) + .split(hz_chunks[0]); + + help_bar_layout = Some(HelpBarLayout { + left: help_bar_chunks[0], + middle: help_bar_chunks[1], + right: help_bar_chunks[2], + }); + } else { + main_rect = main_block; + help_bar_layout = None; + } // split the main block into two vertical chunks let constraints = if with_remote { @@ -97,7 +124,7 @@ impl Layout { let vt_chunks = layout::Layout::default() .direction(Direction::Horizontal) .constraints(constraints) - .split(hz_chunks[1]); + .split(main_rect); // left block: results + input field let left_chunks = layout::Layout::default() @@ -112,9 +139,7 @@ impl Layout { .split(vt_chunks[1]); Self::new( - help_bar_chunks[0], - help_bar_chunks[1], - help_bar_chunks[2], + help_bar_layout, left_chunks[0], left_chunks[1], right_chunks[0],