pub struct TreeState<T> { /* private fields */ }Expand description
State for a Tree component.
Implementations§
Source§impl<T: Clone> TreeState<T>
impl<T: Clone> TreeState<T>
Sourcepub fn new(roots: Vec<TreeNode<T>>) -> Self
pub fn new(roots: Vec<TreeNode<T>>) -> Self
Creates a new tree state with the given root nodes.
If roots are non-empty, the first node is selected.
§Example
use envision::component::{TreeState, TreeNode};
let roots = vec![
TreeNode::new("Root 1", 1),
TreeNode::new("Root 2", 2),
];
let state = TreeState::new(roots);
assert_eq!(state.roots().len(), 2);Sourcepub fn with_selected(self, index: usize) -> Self
pub fn with_selected(self, index: usize) -> Self
Sets the initially selected index in the flattened view (builder method).
The index is clamped to the valid range of visible nodes. Has no effect on empty trees.
§Example
use envision::component::{TreeState, TreeNode};
let mut root = TreeNode::new_expanded("Root", ());
root.add_child(TreeNode::new("Child 1", ()));
root.add_child(TreeNode::new("Child 2", ()));
let state = TreeState::new(vec![root]).with_selected(2);
assert_eq!(state.selected_index(), Some(2));Sourcepub fn roots(&self) -> &[TreeNode<T>]
pub fn roots(&self) -> &[TreeNode<T>]
Returns the root nodes.
§Examples
use envision::prelude::*;
let state = TreeState::new(vec![
TreeNode::new("Root 1", 1),
TreeNode::new("Root 2", 2),
]);
assert_eq!(state.roots().len(), 2);
assert_eq!(state.roots()[0].label(), "Root 1");Sourcepub fn roots_mut(&mut self) -> &mut Vec<TreeNode<T>>
pub fn roots_mut(&mut self) -> &mut Vec<TreeNode<T>>
Returns a mutable reference to the root nodes.
§Example
use envision::component::{TreeState, TreeNode};
let mut state = TreeState::new(vec![TreeNode::new("Root", ())]);
state.roots_mut().push(TreeNode::new("Another Root", ()));
assert_eq!(state.roots().len(), 2);Sourcepub fn update_root(&mut self, index: usize, f: impl FnOnce(&mut TreeNode<T>))
pub fn update_root(&mut self, index: usize, f: impl FnOnce(&mut TreeNode<T>))
Updates a root node at the given index via a closure.
No-ops if the index is out of bounds. This is safe because it does not change the number of root nodes or their positions, so selection and filter state remain valid.
§Example
use envision::component::{TreeState, TreeNode};
let mut state = TreeState::new(vec![
TreeNode::new("Root 1", 1),
TreeNode::new("Root 2", 2),
]);
state.update_root(0, |root| root.set_label("Updated Root"));
assert_eq!(state.roots()[0].label(), "Updated Root");Sourcepub fn set_roots(&mut self, roots: Vec<TreeNode<T>>)
pub fn set_roots(&mut self, roots: Vec<TreeNode<T>>)
Sets the root nodes.
Resets selection to the first node, or None if the new roots are empty.
Clears any active filter.
§Examples
use envision::prelude::*;
let mut state = TreeState::new(vec![TreeNode::new("Old", 0)]);
state.set_roots(vec![TreeNode::new("New 1", 1), TreeNode::new("New 2", 2)]);
assert_eq!(state.roots().len(), 2);
assert_eq!(state.selected_index(), Some(0));Sourcepub fn selected_index(&self) -> Option<usize>
pub fn selected_index(&self) -> Option<usize>
Returns the currently selected index in the flattened view.
Returns None if the tree is empty.
§Examples
use envision::prelude::*;
let state = TreeState::new(vec![TreeNode::new("Root", ())]);
assert_eq!(state.selected_index(), Some(0));
let empty: TreeState<()> = TreeState::new(vec![]);
assert_eq!(empty.selected_index(), None);Sourcepub fn selected(&self) -> Option<usize>
pub fn selected(&self) -> Option<usize>
Alias for selected_index().
§Example
use envision::component::{TreeState, TreeNode};
let state = TreeState::new(vec![TreeNode::new("Root", ())]);
assert_eq!(state.selected(), Some(0));Sourcepub fn set_selected(&mut self, index: Option<usize>)
pub fn set_selected(&mut self, index: Option<usize>)
Sets the selected index in the flattened view.
The index is clamped to the valid range of visible nodes. Has no effect on empty trees.
§Example
use envision::component::{TreeState, TreeNode};
let mut root = TreeNode::new_expanded("Root", ());
root.add_child(TreeNode::new("Child 1", ()));
root.add_child(TreeNode::new("Child 2", ()));
let mut state = TreeState::new(vec![root]);
state.set_selected(Some(2));
assert_eq!(state.selected_index(), Some(2));Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the tree is empty.
§Example
use envision::component::TreeState;
let empty: TreeState<()> = TreeState::new(vec![]);
assert!(empty.is_empty());Sourcepub fn selected_path(&self) -> Option<Vec<usize>>
pub fn selected_path(&self) -> Option<Vec<usize>>
Returns the path of the currently selected node.
§Example
use envision::component::{TreeState, TreeNode};
let state = TreeState::new(vec![TreeNode::new("Root", ())]);
assert_eq!(state.selected_path(), Some(vec![0]));Sourcepub fn selected_node(&self) -> Option<&TreeNode<T>>
pub fn selected_node(&self) -> Option<&TreeNode<T>>
Returns a reference to the currently selected node.
§Examples
use envision::prelude::*;
let state = TreeState::new(vec![TreeNode::new("Root", 42)]);
let node = state.selected_node().unwrap();
assert_eq!(node.label(), "Root");
assert_eq!(node.data(), &42);Sourcepub fn selected_item(&self) -> Option<&TreeNode<T>>
pub fn selected_item(&self) -> Option<&TreeNode<T>>
Returns a reference to the currently selected node.
This is an alias for selected_node() that provides a
consistent accessor name across all selection-based components.
§Examples
use envision::prelude::*;
let state = TreeState::new(vec![TreeNode::new("Root", 1)]);
assert_eq!(state.selected_item(), state.selected_node());Sourcepub fn expand_all(&mut self)
pub fn expand_all(&mut self)
Expands all nodes in the tree.
§Examples
use envision::prelude::*;
let mut root = TreeNode::new("Root", ());
root.add_child(TreeNode::new("Child", ()));
let mut state = TreeState::new(vec![root]);
state.expand_all();
assert!(state.roots()[0].is_expanded());Sourcepub fn collapse_all(&mut self)
pub fn collapse_all(&mut self)
Collapses all nodes in the tree.
§Examples
use envision::prelude::*;
let mut root = TreeNode::new_expanded("Root", ());
root.add_child(TreeNode::new("Child", ()));
let mut state = TreeState::new(vec![root]);
state.collapse_all();
assert!(!state.roots()[0].is_expanded());Sourcepub fn visible_count(&self) -> usize
pub fn visible_count(&self) -> usize
Returns the number of visible nodes.
§Examples
use envision::prelude::*;
let mut root = TreeNode::new_expanded("Root", ());
root.add_child(TreeNode::new("Child 1", ()));
root.add_child(TreeNode::new("Child 2", ()));
let state = TreeState::new(vec![root]);
// Root + 2 children visible (root is expanded)
assert_eq!(state.visible_count(), 3);Sourcepub fn filter_text(&self) -> &str
pub fn filter_text(&self) -> &str
Returns the current filter text.
§Example
use envision::component::TreeState;
let state: TreeState<()> = TreeState::new(vec![]);
assert_eq!(state.filter_text(), "");Sourcepub fn set_filter_text(&mut self, text: &str)
pub fn set_filter_text(&mut self, text: &str)
Sets the filter text for case-insensitive substring matching on node labels.
When a filter is active, only nodes whose label matches or whose descendants match are shown. Ancestor nodes are auto-expanded to reveal matching descendants without modifying their actual expanded state.
Selection is preserved if the selected node remains visible, otherwise it moves to the first visible node.
§Example
use envision::component::{TreeState, TreeNode};
let mut state = TreeState::new(vec![
TreeNode::new("Alpha", ()),
TreeNode::new("Beta", ()),
]);
state.set_filter_text("alpha");
assert_eq!(state.filter_text(), "alpha");
assert_eq!(state.visible_count(), 1);Sourcepub fn clear_filter(&mut self)
pub fn clear_filter(&mut self)
Clears the filter, showing all nodes with their original expanded state.
§Example
use envision::component::{TreeState, TreeNode};
let mut state = TreeState::new(vec![
TreeNode::new("Alpha", ()),
TreeNode::new("Beta", ()),
]);
state.set_filter_text("alpha");
assert_eq!(state.visible_count(), 1);
state.clear_filter();
assert_eq!(state.filter_text(), "");
assert_eq!(state.visible_count(), 2);Source§impl<T: Clone + 'static> TreeState<T>
impl<T: Clone + 'static> TreeState<T>
Sourcepub fn update(&mut self, msg: TreeMessage) -> Option<TreeOutput>
pub fn update(&mut self, msg: TreeMessage) -> Option<TreeOutput>
Updates the tree state with a message, returning any output.
§Example
use envision::component::{TreeMessage, TreeNode, TreeState};
let mut root = TreeNode::new("Root", ());
root.add_child(TreeNode::new("Child", ()));
let mut state = TreeState::new(vec![root]);
state.update(TreeMessage::Expand);
assert!(state.roots()[0].is_expanded());Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for TreeState<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for TreeState<T>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl<T> Freeze for TreeState<T>
impl<T> RefUnwindSafe for TreeState<T>where
T: RefUnwindSafe,
impl<T> Send for TreeState<T>where
T: Send,
impl<T> Sync for TreeState<T>where
T: Sync,
impl<T> Unpin for TreeState<T>where
T: Unpin,
impl<T> UnsafeUnpin for TreeState<T>
impl<T> UnwindSafe for TreeState<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more