feat(ui): make help bar display optional (#35)

This commit is contained in:
Alexandre Pasmantier 2024-11-16 20:32:40 +01:00 committed by GitHub
parent 7277a3f3ab
commit 512afa2fda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 70 additions and 34 deletions

View File

@ -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
# ----------------------------------------------------------------------------

2
Cargo.lock generated
View File

@ -2963,7 +2963,7 @@ dependencies = [
[[package]]
name = "television"
version = "0.4.22"
version = "0.4.23"
dependencies = [
"anyhow",
"better-panic",

View File

@ -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"

View File

@ -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<UiConfig> 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)
}
}

View File

@ -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)

View File

@ -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(())
}

View File

@ -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<HelpBarLayout>,
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<HelpBarLayout>,
results: Rect,
input: Rect,
preview_title: Rect,
@ -48,9 +61,7 @@ impl Layout {
remote_control: Option<Rect>,
) -> Self {
Self {
help_bar_left,
help_bar_middle,
help_bar_right,
help_bar,
results,
input,
preview_title,
@ -63,13 +74,19 @@ 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 main_rect: Rect;
let help_bar_layout: Option<HelpBarLayout>;
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()
@ -84,6 +101,16 @@ impl Layout {
])
.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 {
vec![
@ -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],