ratatui_toolkit/primitives/tree_view/tree_view/mod.rs
1//! Tree view widget for rendering hierarchical data.
2
3pub mod constructors;
4pub mod methods;
5pub mod traits;
6
7use ratatui::{style::Style, text::Line, widgets::Block};
8
9use crate::primitives::tree_view::node_state::NodeState;
10
11/// Type alias for node render function to reduce complexity.
12pub type NodeRenderFn<'a, T> = Box<dyn Fn(&T, &NodeState) -> Line<'a> + 'a>;
13
14/// Tree view widget.
15///
16/// A widget for rendering hierarchical tree data with expand/collapse
17/// functionality, custom rendering, and selection highlighting.
18///
19/// # Type Parameters
20///
21/// * `T` - The type of data stored in tree nodes.
22///
23/// # Example
24///
25/// ```rust
26/// use ratatui_toolkit::tree_view::{TreeNode, TreeView};
27///
28/// let nodes = vec![TreeNode::new("Root")];
29/// let tree = TreeView::new(nodes)
30/// .render_fn(|data, state| {
31/// ratatui::text::Line::from(*data)
32/// });
33/// ```
34pub struct TreeView<'a, T> {
35 /// Root nodes of the tree
36 pub(crate) nodes: Vec<crate::primitives::tree_view::tree_node::TreeNode<T>>,
37 /// Block to wrap the tree
38 pub(crate) block: Option<Block<'a>>,
39 /// Render callback for custom node display
40 pub(crate) render_fn: NodeRenderFn<'a, T>,
41 /// Default expand icon
42 pub(crate) expand_icon: &'a str,
43 /// Default collapse icon
44 pub(crate) collapse_icon: &'a str,
45 /// Style for selected row background (full-width highlight)
46 pub(crate) highlight_style: Option<Style>,
47 /// Style for expand/collapse icons
48 pub(crate) icon_style: Style,
49}