use ratatui::{layout::Rect, widgets::ScrollbarState, Frame};
use crate::ui::theme;
#[derive(Debug, Clone, Default)]
pub struct PaneStyle {
pub is_active: bool,
pub show_border: bool,
pub show_scrollbar: bool,
}
impl PaneStyle {
pub fn new() -> Self {
Self {
is_active: false,
show_border: true,
show_scrollbar: true,
}
}
pub fn active(mut self, active: bool) -> Self {
self.is_active = active;
self
}
pub fn border(mut self, show: bool) -> Self {
self.show_border = show;
self
}
pub fn scrollbar(mut self, show: bool) -> Self {
self.show_scrollbar = show;
self
}
}
pub struct Pane<'a> {
title_left: &'a str,
title_right: Option<&'a str>,
style: PaneStyle,
}
impl<'a> Pane<'a> {
pub fn new(title: &'a str) -> Self {
Self {
title_left: title,
title_right: None,
style: PaneStyle::new(),
}
}
pub fn title_right(mut self, right: &'a str) -> Self {
self.title_right = Some(right);
self
}
pub fn style(mut self, style: PaneStyle) -> Self {
self.style = style;
self
}
pub fn active(mut self, active: bool) -> Self {
self.style.is_active = active;
self
}
pub fn render_header(&self, f: &mut Frame, area: Rect) -> Rect {
if let Some(right) = self.title_right {
theme::render_panel_header_split(f, area, self.title_left, right, self.style.is_active)
} else {
theme::render_panel_header(f, area, self.title_left, self.style.is_active)
}
}
pub fn render_scrollbar(
f: &mut Frame,
area: Rect,
state: &mut ScrollbarState,
is_active: bool,
) {
if area.width < 2 {
return;
}
f.render_stateful_widget(
theme::scrollbar(is_active),
theme::scrollbar_area(area),
state,
);
}
pub fn scrollbar_state(
content_length: usize,
viewport_length: usize,
position: usize,
) -> ScrollbarState {
ScrollbarState::default()
.content_length(content_length)
.viewport_content_length(viewport_length)
.position(position)
}
}