use ratatui::{
layout::Rect,
text::{Line, Span},
widgets::{Block, Borders, Scrollbar, ScrollbarOrientation, ScrollbarState},
Frame,
};
use crate::Theme;
pub fn panel_block(title: Line<'static>, focused: bool, theme: &Theme) -> Block<'static> {
let border_style = if focused {
theme.border_focused
} else {
theme.border_unfocused
};
Block::default()
.borders(Borders::ALL)
.border_style(border_style)
.title(title)
}
pub fn popup_block(title: Line<'static>, theme: &Theme) -> Block<'static> {
Block::default()
.borders(Borders::ALL)
.border_style(theme.border_popup)
.title(title)
}
pub fn focusable_block(title: &str, shortcut: Option<u8>, focused: bool, theme: &Theme) -> Block<'static> {
let title_line = widget_title(title, shortcut, focused, theme);
panel_block(title_line, focused, theme)
}
pub fn render_scrollbar(f: &mut Frame, area: Rect, total: usize, position: usize) {
let visible = area.height.saturating_sub(2) as usize; if total <= visible {
return;
}
let mut state = ScrollbarState::new(total).position(position);
f.render_stateful_widget(
Scrollbar::new(ScrollbarOrientation::VerticalRight),
area,
&mut state,
);
}
pub fn widget_title(label: &str, shortcut: Option<u8>, active: bool, theme: &Theme) -> Line<'static> {
let label_style = if active { theme.tab_active } else { theme.tab_inactive };
let border_style = if active { theme.border_focused } else { theme.border_unfocused };
match shortcut {
Some(n) => Line::from(vec![
Span::styled(format!("[{}]\u{2500} ", n), border_style),
Span::styled(label.to_string(), label_style),
Span::raw(" "),
]),
None => Line::from(vec![
Span::raw(" "),
Span::styled(label.to_string(), label_style),
Span::raw(" "),
]),
}
}