use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;
use crate::tui::app::{App, Navigation, Tab};
use crate::tui::hooks::RenderContext;
use crate::tui::screens;
pub fn render(frame: &mut Frame, ctx: &mut RenderContext) {
let area = frame.area();
if area.height < 4 || area.width < 20 {
return;
}
let padded_x = area.x + 1;
let padded_w = area.width.saturating_sub(2);
let show_bottom_bar = ctx.app.navigation.tab == Tab::Speakers && area.height >= 12;
let bar_height: u16 = if show_bottom_bar {
if area.width < 60 {
1
} else {
3
}
} else {
0
};
let bar_overhead = if show_bottom_bar { bar_height + 1 } else { 0 };
let header_area = Rect::new(padded_x, area.y, padded_w, 1);
render_header(frame, header_area, ctx.app);
draw_separator(
frame,
area.y + 1,
area.x,
area.x + area.width - 1,
ctx.app.theme.glyphs.separator,
ctx.app.theme.muted,
);
let content_height = area.height.saturating_sub(4 + bar_overhead);
let content_area = Rect::new(padded_x, area.y + 2, padded_w, content_height);
match ctx.app.navigation.tab {
Tab::Speakers => {
let bar_area = if show_bottom_bar {
let bar_y = area.y + area.height - 1 - 1 - bar_height;
Some(Rect::new(padded_x, bar_y, padded_w, bar_height))
} else {
None
};
screens::speakers::render(frame, content_area, bar_area, ctx);
}
Tab::Settings => {
screens::settings::render(frame, content_area, ctx);
}
}
let content_sep_y = area.y + 2 + content_height;
draw_separator(
frame,
content_sep_y,
area.x,
area.x + area.width - 1,
ctx.app.theme.glyphs.separator,
ctx.app.theme.muted,
);
if show_bottom_bar {
draw_separator(
frame,
area.y + area.height - 2,
area.x,
area.x + area.width - 1,
ctx.app.theme.glyphs.separator,
ctx.app.theme.muted,
);
}
let footer_area = Rect::new(padded_x, area.y + area.height - 1, padded_w, 1);
render_key_legend(frame, footer_area, ctx.app);
}
fn draw_separator(frame: &mut Frame, y: u16, left: u16, right: u16, sep_char: char, style: Style) {
let buf = frame.buffer_mut();
for x in left..=right {
if let Some(cell) = buf.cell_mut((x, y)) {
cell.set_char(sep_char).set_style(style);
}
}
}
fn render_header(frame: &mut Frame, area: Rect, app: &App) {
let logo = app.theme.glyphs.logo;
let tab_spans = build_tab_spans(&app.navigation, &app.theme);
let logo_width = logo.chars().count();
let tab_width: usize = tab_spans.iter().map(|s| s.content.chars().count()).sum();
let padding = (area.width as usize).saturating_sub(logo_width + tab_width);
let mut spans = vec![Span::styled(logo, app.theme.header)];
spans.push(Span::raw(" ".repeat(padding)));
spans.extend(tab_spans);
let paragraph = Paragraph::new(Line::from(spans));
frame.render_widget(paragraph, area);
}
fn build_tab_spans(nav: &Navigation, theme: &crate::tui::theme::Theme) -> Vec<Span<'static>> {
let tabs = [
("Speakers", nav.tab == Tab::Speakers),
("Settings", nav.tab == Tab::Settings),
];
render_tab_labels(&tabs, nav.tab_focused, theme)
}
fn render_tab_labels(
tabs: &[(&str, bool)],
focused: bool,
theme: &crate::tui::theme::Theme,
) -> Vec<Span<'static>> {
let g = &theme.glyphs;
let mut spans = Vec::new();
for (i, (label, is_active)) in tabs.iter().enumerate() {
if i > 0 {
spans.push(Span::raw(" "));
}
if *is_active {
let style = if focused { theme.accent } else { theme.header };
spans.push(Span::styled(
format!(
"{}{}{label}{}",
g.tab_active_left, g.tab_active_indicator, g.tab_active_right
),
style,
));
} else {
let style = theme.muted;
spans.push(Span::styled(label.to_string(), style));
}
}
spans
}
fn render_key_legend(frame: &mut Frame, area: Rect, app: &App) {
let text = match app.navigation.tab {
Tab::Speakers => "\u{2191}\u{2193} Navigate \u{2190}\u{2192} Volume \u{2423} Pick up/Drop p Play/Pause n Next b Prev \u{238b} Quit",
Tab::Settings => {
if app.navigation.settings_state.dropdown_open {
"\u{2191}\u{2193} Select Enter Confirm \u{238b} Cancel"
} else {
"\u{2191}\u{2193} Navigate Enter Open \u{2190}\u{2192} Tabs \u{238b} Quit"
}
}
};
let paragraph = Paragraph::new(text).style(app.theme.legend);
frame.render_widget(paragraph, area);
}