Skip to main content

TreeModel

Struct TreeModel 

Source
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::Context

Context.

Implementations§

Source§

impl<BackendT> TreeModel<BackendT>
where BackendT: TreeBackend,

Source

pub fn new(context: BackendT::Context) -> Self

Constructor.

Source

pub fn is_empty(&self) -> bool

Whether we have no nodes.

Source

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.

Source

pub fn at_path(&self, path: NodePath) -> Option<&Node<BackendT>>

Get node at path.

Source

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() {
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".into());
16    tree.model.add_root(NodeKind::Branch, (), "World".into());
17
18    // Add 10 children to "World"
19
20    let world = tree.model.at_path_mut([1].into()).unwrap();
21    for i in 0..10 {
22        world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1).into());
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()).unwrap();
31        child.kind = NodeKind::Branch;
32        for _ in 0..10 {
33            child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1).into());
34            gc += 1;
35        }
36    }
37
38    // Expand all nodes
39
40    tree.model.expand(None).unwrap();
41
42    cursive.add_fullscreen_layer(Panel::new(tree.scrollable()));
43    cursive.add_global_callback('q', |cursive| cursive.quit());
44
45    cursive.run();
46}
Source

pub fn at_row(&self, row: usize) -> Option<&Node<BackendT>>

Get node at row.

Source

pub fn path(&self, node: &Node<BackendT>) -> Option<NodePath>

Find path to node.

Source

pub fn extents(&self) -> Vec2

Full extents for drawing.

Source

pub fn add_root( &mut self, kind: NodeKind, id: BackendT::ID, representation: Representation, )

Add a root node.

Examples found in repository?
examples/simple.rs (line 15)
8fn main() {
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".into());
16    tree.model.add_root(NodeKind::Branch, (), "World".into());
17
18    // Add 10 children to "World"
19
20    let world = tree.model.at_path_mut([1].into()).unwrap();
21    for i in 0..10 {
22        world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1).into());
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()).unwrap();
31        child.kind = NodeKind::Branch;
32        for _ in 0..10 {
33            child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1).into());
34            gc += 1;
35        }
36    }
37
38    // Expand all nodes
39
40    tree.model.expand(None).unwrap();
41
42    cursive.add_fullscreen_layer(Panel::new(tree.scrollable()));
43    cursive.add_global_callback('q', |cursive| cursive.quit());
44
45    cursive.run();
46}
Source

pub fn insert_root( &mut self, index: usize, kind: NodeKind, id: BackendT::ID, representation: Representation, )

Insert a root node.

Source

pub fn expand(&mut self, depth: Option<usize>) -> Result<(), BackendT::Error>
where BackendT::Context: Clone,

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() {
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".into());
16    tree.model.add_root(NodeKind::Branch, (), "World".into());
17
18    // Add 10 children to "World"
19
20    let world = tree.model.at_path_mut([1].into()).unwrap();
21    for i in 0..10 {
22        world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1).into());
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()).unwrap();
31        child.kind = NodeKind::Branch;
32        for _ in 0..10 {
33            child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1).into());
34            gc += 1;
35        }
36    }
37
38    // Expand all nodes
39
40    tree.model.expand(None).unwrap();
41
42    cursive.add_fullscreen_layer(Panel::new(tree.scrollable()));
43    cursive.add_global_callback('q', |cursive| cursive.quit());
44
45    cursive.run();
46}
Source

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.

Source

pub fn populate(&mut self, depth: Option<usize>) -> Result<(), BackendT::Error>
where BackendT::Context: Clone,

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 20)
13fn main() {
14    let mut cursive = default();
15
16    let current_dir = current_dir().unwrap();
17    let mut tree = FileBackend::tree_view(current_dir.into());
18
19    // Populate the first level (just the roots)
20    tree.model.populate(Some(1)).unwrap();
21
22    let mut browser = LinearLayout::horizontal();
23    browser.add_child(Panel::new(tree.with_name("tree").scrollable()));
24    browser.add_child(Panel::new(TextView::new("").with_name("details").scrollable()));
25
26    cursive.add_fullscreen_layer(browser);
27    cursive.add_global_callback('q', |cursive| cursive.quit());
28
29    cursive.run();
30}
Source

pub fn clear(&mut self)

Remove all nodes.

Trait Implementations§

Source§

impl<BackendT> From<TreeModel<BackendT>> for TreeView<BackendT>
where BackendT: TreeBackend,

Source§

fn from(model: TreeModel<BackendT>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<BackendT> Freeze for TreeModel<BackendT>
where <BackendT as TreeBackend>::Context: Freeze,

§

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>
where <BackendT as TreeBackend>::Context: UnsafeUnpin,

§

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> 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> 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, 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> With for T

Source§

fn wrap_with<U, F>(self, f: F) -> U
where F: FnOnce(Self) -> U,

Calls the given closure and return the result. Read more
Source§

fn with<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Calls the given closure on self.
Source§

fn try_with<E, F>(self, f: F) -> Result<Self, E>
where F: FnOnce(&mut Self) -> Result<(), E>,

Calls the given closure on self.
Source§

fn with_if<F>(self, condition: bool, f: F) -> Self
where F: FnOnce(&mut Self),

Calls the given closure if condition == true.