Skip to main content

TreeState

Struct TreeState 

Source
pub struct TreeState<T> { /* private fields */ }
Expand description

State for a Tree component.

Implementations§

Source§

impl<T: Clone> TreeState<T>

Source

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);
Source

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));
Source

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");
Source

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);
Source

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");
Source

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));
Source

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);
Source

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));
Source

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));
Source

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());
Source

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]));
Source

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);
Source

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());
Source

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());
Source

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());
Source

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);
Source

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(), "");
Source

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);
Source

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>

Source

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<T: Clone> Clone for TreeState<T>

Source§

fn clone(&self) -> TreeState<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for TreeState<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Clone> Default for TreeState<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: Clone + PartialEq> PartialEq for TreeState<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for TreeState<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> StateExt for T

Source§

fn updated(self, cmd: Command<impl Clone>) -> UpdateResult<Self, impl Clone>

Updates self and returns a command.
Source§

fn unchanged(self) -> UpdateResult<Self, ()>

Returns self with no command.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,