Skip to content

Commit

Permalink
feat(ui): make help bar display optional (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier authored Nov 16, 2024
1 parent 7277a3f commit 512afa2
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 6 additions & 0 deletions crates/television/config/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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 {
fn default() -> Self {
Self {
use_nerd_font_icons: false,
ui_scale: DEFAULT_UI_SCALE,
show_help_bar: true,
}
}
}
Expand All @@ -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)
}
}
1 change: 1 addition & 0 deletions crates/television/television.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions crates/television/ui/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
83 changes: 54 additions & 29 deletions crates/television/ui/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,19 +53,15 @@ 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,
preview_window: Rect,
remote_control: Option<Rect>,
) -> Self {
Self {
help_bar_left,
help_bar_middle,
help_bar_right,
help_bar,
results,
input,
preview_title,
Expand All @@ -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<HelpBarLayout>;

// 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 {
Expand All @@ -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()
Expand All @@ -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],
Expand Down

0 comments on commit 512afa2

Please sign in to comment.