pub struct TreeModel<BackendT>where
BackendT: TreeBackend,{
pub roots: NodeList<BackendT>,
pub context: BackendT::Context,
}Expand description
Tree model.
Fields§
§roots: NodeList<BackendT>Roots.
context: BackendT::ContextContext.
Implementations§
Source§impl<BackendT> TreeModel<BackendT>where
BackendT: TreeBackend,
impl<BackendT> TreeModel<BackendT>where
BackendT: TreeBackend,
Sourcepub fn iter(&self, only_expanded: bool) -> NodeIterator<'_, BackendT> ⓘ
pub fn iter(&self, only_expanded: bool) -> NodeIterator<'_, BackendT> ⓘ
Iterate nodes in visual order from top to bottom.
When only_expanded is true will skip the children of collapsed branches.
Sourcepub fn at_path_mut(&mut self, path: NodePath) -> Option<&mut Node<BackendT>>
pub fn at_path_mut(&mut self, path: NodePath) -> Option<&mut Node<BackendT>>
Get node at path.
Examples found in repository?
examples/simple.rs (line 20)
8fn main() -> Result<(), ()> {
9 let mut cursive = default();
10
11 let mut tree = SimpleTreeBackend::tree_view();
12
13 // A few roots
14
15 tree.model.add_root(NodeKind::Leaf, (), "Hello");
16 tree.model.add_root(NodeKind::Branch, (), "World");
17
18 // Add 10 children to "World"
19
20 let world = tree.model.at_path_mut([1].into()).expect("at_path_mut");
21 for i in 0..10 {
22 world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1));
23 }
24
25 // Add 10 grandchildren each to "Child #5" -> "Child #10"
26 // Note that we change them to branch in order to support having children
27
28 let mut gc = 0;
29 for c in 4..10 {
30 let child = tree.model.at_path_mut([1, c].into()).expect("at_path_mut");
31 child.kind = NodeKind::Branch;
32 for _ in 0..10 {
33 child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1));
34 gc += 1;
35 }
36 }
37
38 // Expand all nodes
39
40 tree.model.expand(None)?;
41
42 cursive.add_fullscreen_layer(Panel::new(tree.scrollable()));
43 cursive.add_global_callback('q', |cursive| cursive.quit());
44
45 cursive.run();
46
47 Ok(())
48}Sourcepub fn add_root<IdT, LabelT>(&mut self, kind: NodeKind, id: IdT, label: LabelT)
pub fn add_root<IdT, LabelT>(&mut self, kind: NodeKind, id: IdT, label: LabelT)
Add a root node.
Examples found in repository?
examples/simple.rs (line 15)
8fn main() -> Result<(), ()> {
9 let mut cursive = default();
10
11 let mut tree = SimpleTreeBackend::tree_view();
12
13 // A few roots
14
15 tree.model.add_root(NodeKind::Leaf, (), "Hello");
16 tree.model.add_root(NodeKind::Branch, (), "World");
17
18 // Add 10 children to "World"
19
20 let world = tree.model.at_path_mut([1].into()).expect("at_path_mut");
21 for i in 0..10 {
22 world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1));
23 }
24
25 // Add 10 grandchildren each to "Child #5" -> "Child #10"
26 // Note that we change them to branch in order to support having children
27
28 let mut gc = 0;
29 for c in 4..10 {
30 let child = tree.model.at_path_mut([1, c].into()).expect("at_path_mut");
31 child.kind = NodeKind::Branch;
32 for _ in 0..10 {
33 child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1));
34 gc += 1;
35 }
36 }
37
38 // Expand all nodes
39
40 tree.model.expand(None)?;
41
42 cursive.add_fullscreen_layer(Panel::new(tree.scrollable()));
43 cursive.add_global_callback('q', |cursive| cursive.quit());
44
45 cursive.run();
46
47 Ok(())
48}Sourcepub fn insert_root<IdT, LabelT>(
&mut self,
index: usize,
kind: NodeKind,
id: IdT,
label: LabelT,
)
pub fn insert_root<IdT, LabelT>( &mut self, index: usize, kind: NodeKind, id: IdT, label: LabelT, )
Insert a root node.
Sourcepub fn expand(&mut self, depth: Option<usize>) -> Result<(), BackendT::Error>
pub fn expand(&mut self, depth: Option<usize>) -> Result<(), BackendT::Error>
Expand branch nodes.
If depth is None will expand the entire tree.
If depth is 0 will do nothing.
Note that this will populate expanded nodes from the backend.
Examples found in repository?
examples/simple.rs (line 40)
8fn main() -> Result<(), ()> {
9 let mut cursive = default();
10
11 let mut tree = SimpleTreeBackend::tree_view();
12
13 // A few roots
14
15 tree.model.add_root(NodeKind::Leaf, (), "Hello");
16 tree.model.add_root(NodeKind::Branch, (), "World");
17
18 // Add 10 children to "World"
19
20 let world = tree.model.at_path_mut([1].into()).expect("at_path_mut");
21 for i in 0..10 {
22 world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1));
23 }
24
25 // Add 10 grandchildren each to "Child #5" -> "Child #10"
26 // Note that we change them to branch in order to support having children
27
28 let mut gc = 0;
29 for c in 4..10 {
30 let child = tree.model.at_path_mut([1, c].into()).expect("at_path_mut");
31 child.kind = NodeKind::Branch;
32 for _ in 0..10 {
33 child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1));
34 gc += 1;
35 }
36 }
37
38 // Expand all nodes
39
40 tree.model.expand(None)?;
41
42 cursive.add_fullscreen_layer(Panel::new(tree.scrollable()));
43 cursive.add_global_callback('q', |cursive| cursive.quit());
44
45 cursive.run();
46
47 Ok(())
48}Sourcepub fn collapse(&mut self, depth: Option<usize>)
pub fn collapse(&mut self, depth: Option<usize>)
Collapse branch nodes.
If depth is None will collapse the entire tree.
If depth is 0 will do nothing.
Sourcepub fn populate(&mut self, depth: Option<usize>) -> Result<(), BackendT::Error>
pub fn populate(&mut self, depth: Option<usize>) -> Result<(), BackendT::Error>
Fetch roots and possibly their children from backend.
If depth is None will populate the entire tree.
If depth is 1 will only populate the roots.
If depth is 0 will do nothing.
Note that populating a branch node will not automatically expand it.
Examples found in repository?
examples/file_browser.rs (line 23)
16fn main() -> Result<(), io::Error> {
17 let mut cursive = default();
18
19 // Our base directory (the backend context) will be the current directory
20 let mut tree = FileBackend::tree_view(current_dir()?.into());
21
22 // Populate the first level (just the roots)
23 tree.model.populate(Some(1))?;
24
25 cursive.add_fullscreen_layer(
26 LinearLayout::horizontal()
27 .child(Panel::new(tree.with_name("tree").scrollable()))
28 .child(Panel::new(TextView::new("").with_name("details").scrollable())),
29 );
30
31 cursive.add_global_callback('q', |cursive| cursive.quit());
32
33 cursive.run();
34
35 Ok(())
36}Trait Implementations§
Auto Trait Implementations§
impl<BackendT> Freeze for TreeModel<BackendT>
impl<BackendT> RefUnwindSafe for TreeModel<BackendT>where
<BackendT as TreeBackend>::Context: RefUnwindSafe,
<BackendT as TreeBackend>::ID: RefUnwindSafe,
<BackendT as TreeBackend>::Data: RefUnwindSafe,
BackendT: RefUnwindSafe,
impl<BackendT> Send for TreeModel<BackendT>where
<BackendT as TreeBackend>::Context: Send,
<BackendT as TreeBackend>::ID: Send,
<BackendT as TreeBackend>::Data: Send,
BackendT: Send,
impl<BackendT> Sync for TreeModel<BackendT>where
<BackendT as TreeBackend>::Context: Sync,
<BackendT as TreeBackend>::ID: Sync,
<BackendT as TreeBackend>::Data: Sync,
BackendT: Sync,
impl<BackendT> Unpin for TreeModel<BackendT>where
<BackendT as TreeBackend>::Context: Unpin,
<BackendT as TreeBackend>::ID: Unpin,
<BackendT as TreeBackend>::Data: Unpin,
BackendT: Unpin,
impl<BackendT> UnsafeUnpin for TreeModel<BackendT>
impl<BackendT> UnwindSafe for TreeModel<BackendT>where
<BackendT as TreeBackend>::Context: UnwindSafe,
<BackendT as TreeBackend>::ID: UnwindSafe,
<BackendT as TreeBackend>::Data: UnwindSafe,
BackendT: 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
Mutably borrows from an owned value. Read more