use ratatui::{style::Style, widgets::Scrollbar};
use ratatui_kit::components::SendBlock;
use ratatui_kit::{Component, Props, State};
use std::hash::Hash;
use tui_tree_widget::{TreeItem, TreeState};
#[derive(Debug, Props, Clone)]
pub struct TreeSelect<T>
where
T: Sync + Send + Clone + Eq + Hash + 'static,
{
pub state: Option<State<TreeState<T>>>,
pub items: Vec<TreeItem<'static, T>>,
pub scrollbar: Option<Scrollbar<'static>>,
pub style: Style,
pub highlight_style: Style,
pub highlight_symbol: &'static str,
pub node_closed_symbol: &'static str,
pub node_open_symbol: &'static str,
pub node_no_children_symbol: &'static str,
pub block: SendBlock,
}
impl<T> Default for TreeSelect<T>
where
T: Sync + Send + Clone + Eq + Hash,
{
fn default() -> Self {
Self {
state: None,
items: vec![],
scrollbar: None,
style: Style::new(),
highlight_style: Style::new(),
highlight_symbol: "",
node_closed_symbol: "\u{25b6} ", node_open_symbol: "\u{25bc} ", node_no_children_symbol: " ",
block: SendBlock::default(),
}
}
}
impl<T> Component for TreeSelect<T>
where
T: Sync + Send + Clone + Eq + Hash + Unpin + 'static,
{
type Props<'a>
= TreeSelect<T>
where
Self: 'a;
fn new(props: &Self::Props<'_>) -> Self {
props.clone()
}
fn update(
&mut self,
props: &mut Self::Props<'_>,
_hooks: crate::Hooks,
_updater: &mut crate::ComponentUpdater,
) {
*self = props.clone();
}
fn draw(&mut self, drawer: &mut ratatui_kit::ComponentDrawer<'_, '_>) {
let mut tree = tui_tree_widget::Tree::new(&self.items)
.unwrap()
.style(self.style)
.highlight_style(self.highlight_style)
.highlight_symbol(self.highlight_symbol)
.node_closed_symbol(self.node_closed_symbol)
.node_open_symbol(self.node_open_symbol)
.node_no_children_symbol(self.node_no_children_symbol)
.experimental_scrollbar(self.scrollbar.clone());
if let Some(block) = self.block.as_ref() {
tree = tree.block(block.clone());
}
if let Some(state) = &mut self.state {
drawer.render_stateful_widget(tree, drawer.area, &mut state.write_no_update());
} else {
drawer.render_widget(tree, drawer.area);
}
}
}