ratatui_toolkit/primitives/tree_view/tree_view_ref/mod.rs
1//! Tree view widget that borrows nodes instead of owning them.
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;
10use crate::primitives::tree_view::tree_node::TreeNode;
11
12/// Type alias for node render function that borrows data (for TreeViewRef).
13pub type NodeRenderRefFn<'a, T> = Box<dyn Fn(&T, &NodeState) -> Line<'a> + 'a>;
14
15/// Type alias for node filter function.
16pub type NodeFilterFn<T> = Box<dyn Fn(&T, &Option<String>) -> bool>;
17
18/// Tree view widget that borrows nodes instead of owning them.
19///
20/// This is useful when you want to avoid cloning the tree on every render frame.
21/// Unlike `TreeView` which takes ownership of nodes, `TreeViewRef` borrows them,
22/// allowing the original data to remain in place after rendering.
23///
24/// # Type Parameters
25///
26/// * `'a` - Lifetime for borrowed UI elements.
27/// * `'b` - Lifetime for borrowed tree nodes.
28/// * `T` - The type of data stored in tree nodes.
29///
30/// # Example
31///
32/// ```rust
33/// use ratatui_toolkit::tree_view::{TreeNode, TreeViewRef};
34///
35/// let nodes = vec![TreeNode::new("Root")];
36/// let tree = TreeViewRef::new(&nodes)
37/// .render_fn(|data, state| {
38/// ratatui::text::Line::from(*data)
39/// });
40/// ```
41pub struct TreeViewRef<'a, 'b, T> {
42 /// Reference to root nodes of the tree
43 pub(crate) nodes: &'b [TreeNode<T>],
44 /// Block to wrap the tree
45 pub(crate) block: Option<Block<'a>>,
46 /// Render callback for custom node display
47 pub(crate) render_fn: NodeRenderRefFn<'a, T>,
48 /// Default expand icon
49 pub(crate) expand_icon: &'a str,
50 /// Default collapse icon
51 pub(crate) collapse_icon: &'a str,
52 /// Style for selected row background (full-width highlight)
53 pub(crate) highlight_style: Option<Style>,
54 /// Style for expand/collapse icons
55 pub(crate) icon_style: Style,
56}