Struct tui_tree_widget::TreeItem
source · pub struct TreeItem<'a, Identifier> { /* private fields */ }
Expand description
One item inside a Tree
.
Can have zero or more children
.
§Identifier
The generic argument Identifier
is used to keep the state like the currently selected or opened TreeItem
s in the TreeState
.
It needs to be unique among its siblings but can be used again on parent or child TreeItem
s.
A common example would be a filename which has to be unique in its directory while it can exist in another.
The text
can be different from its identifier
.
To repeat the filename analogy: File browsers sometimes hide file extensions.
The filename main.rs
is the identifier while its shown as main
.
Two files main.rs
and main.toml
can exist in the same directory and can both be displayed as main
but their identifier is different.
Just like every file in a file system can be uniquely identified with its file and directory names each TreeItem
in a Tree
can be with these identifiers.
As an example the following two identifiers describe the main file in a Rust cargo project: vec!["src", "main.rs"]
.
The identifier does not need to be a String
and is therefore generic.
Until version 0.14 this crate used usize
and indices.
This might still be perfect for your use case.
§Example
let a = TreeItem::new_leaf("l", "Leaf");
let b = TreeItem::new("r", "Root", vec![a])?;
Implementations§
source§impl<'a, Identifier> Item<'a, Identifier>
impl<'a, Identifier> Item<'a, Identifier>
sourcepub fn new_leaf<T>(identifier: Identifier, text: T) -> Self
pub fn new_leaf<T>(identifier: Identifier, text: T) -> Self
Create a new TreeItem
without children.
sourcepub fn new<T>(
identifier: Identifier,
text: T,
children: Vec<Item<'a, Identifier>>
) -> Result<Self>
pub fn new<T>( identifier: Identifier, text: T, children: Vec<Item<'a, Identifier>> ) -> Result<Self>
Create a new TreeItem
with children.
§Errors
Errors when there are duplicate identifiers in the children.
pub fn children(&self) -> &[Item<'_, Identifier>]
sourcepub fn child_mut(&mut self, index: usize) -> Option<&mut Self>
pub fn child_mut(&mut self, index: usize) -> Option<&mut Self>
Get a mutable reference to a child by index.
When you choose to change the identifier
the TreeState
might not work as expected afterwards.