pub struct TreeModel<T: 'static> { /* private fields */ }Expand description
A concrete reactive tree that stores a hierarchy of T items in a flat arena.
TreeModel<T> is Clone — cloning produces a second handle to the same
underlying data. All handles see the same hierarchy and receive the same
TreeChange notifications from observe_changes.
Nodes are identified by opaque NodeId handles that are stable and
non-reusable across mutations (versioned SlotMap keys).
Implementations§
Source§impl<T: 'static> TreeModel<T>
impl<T: 'static> TreeModel<T>
Sourcepub fn root_count(&self) -> usize
pub fn root_count(&self) -> usize
Number of root-level nodes.
Sourcepub fn child_count(&self, parent: NodeId) -> usize
pub fn child_count(&self, parent: NodeId) -> usize
Number of children of the given node.
Sourcepub fn parent(&self, node: NodeId) -> Option<NodeId>
pub fn parent(&self, node: NodeId) -> Option<NodeId>
Get the parent of a node, or None if it is a root.
Sourcepub fn has_children(&self, node: NodeId) -> bool
pub fn has_children(&self, node: NodeId) -> bool
Whether the given node has any children.
Sourcepub fn children(&self, node: NodeId) -> Vec<NodeId>
pub fn children(&self, node: NodeId) -> Vec<NodeId>
Get the children of a node as a vector of NodeId.
Sourcepub fn with_item<R>(&self, node: NodeId, f: impl FnOnce(&T) -> R) -> Option<R>
pub fn with_item<R>(&self, node: NodeId, f: impl FnOnce(&T) -> R) -> Option<R>
Access a node’s data via a callback. Returns None if the node doesn’t exist.
Sourcepub fn find_by(&self, predicate: impl Fn(&T) -> bool) -> Option<NodeId>
pub fn find_by(&self, predicate: impl Fn(&T) -> bool) -> Option<NodeId>
Find the first node matching a predicate (depth-first from roots).
Sourcepub fn insert_root(&self, index: usize, item: T) -> NodeId
pub fn insert_root(&self, index: usize, item: T) -> NodeId
Sourcepub fn insert_child(&self, parent: NodeId, index: usize, item: T) -> NodeId
pub fn insert_child(&self, parent: NodeId, index: usize, item: T) -> NodeId
Insert a new child node under the given parent at the given index.
§Panics
Panics if the parent is invalid or index > child_count(parent).
Sourcepub fn move_node(&self, node: NodeId, new_parent: NodeId, new_index: usize)
pub fn move_node(&self, node: NodeId, new_parent: NodeId, new_index: usize)
Move a node (and its subtree) to a new parent at the given index.
§Panics
Panics if any of the nodes are invalid, or if the target is a descendant of the source (would create a cycle).
Sourcepub fn move_to_root(&self, node: NodeId, new_index: usize)
pub fn move_to_root(&self, node: NodeId, new_index: usize)
Move a node to the root level at the given index.
Sourcepub fn observe_changes(
&self,
f: impl Fn(&TreeChange) + 'static,
) -> ObserverHandle
pub fn observe_changes( &self, f: impl Fn(&TreeChange) + 'static, ) -> ObserverHandle
Register an observer for tree change notifications.
Returns an ObserverHandle — dropping it removes the callback.
Source§impl<T: Debug + 'static> TreeModel<T>
impl<T: Debug + 'static> TreeModel<T>
Sourcepub fn debug_named(self, _name: impl Into<String>) -> Self
pub fn debug_named(self, _name: impl Into<String>) -> Self
Register this tree with the debug inspector under name. In
release builds (!cfg(debug_assertions)) this is a no-op
pass-through so call sites stay free of #[cfg] lines.
Idempotent on repeated calls — the latest registration wins.
The registration drops automatically when the last TreeModel
handle is freed (the adapter the registry holds is Weak).