Skip to main content

Node

Struct Node 

Source
pub struct Node<BackendT>
where BackendT: TreeBackend,
{ pub depth: usize, pub kind: NodeKind, pub id: BackendT::ID, pub label: SpannedString<Style>, pub label_size: Vec2, pub branch_state: BranchState, pub children: Option<NodeList<BackendT>>, pub data: Option<BackendT::Data>, /* private fields */ }
Expand description

Tree node.

Fields§

§depth: usize

Depth.

§kind: NodeKind

Kind.

§id: BackendT::ID

ID.

§label: SpannedString<Style>

Label.

§label_size: Vec2

Label size.

Cached so we only have to calculate it once.

Note that labels can ostensibly have heights >1, i.e. they can be multiline, however this is not currently supported by TreeView.

§branch_state: BranchState

State for Branch node.

§children: Option<NodeList<BackendT>>

Children for Branch node.

§data: Option<BackendT::Data>

Data.

Implementations§

Source§

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

Source

pub fn new<IdT, LabelT>( depth: usize, kind: NodeKind, id: IdT, label: LabelT, ) -> Self
where IdT: Into<BackendT::ID>, LabelT: Into<SpannedString<Style>>,

Constructor.

Source

pub fn symbol(&self, context: BackendT::Context) -> Symbol<'_>

Symbol.

Its char count is always 1.

Source

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

Get node at path.

Source

pub fn at_path_mut(&mut self, path: NodePath) -> Option<&mut Self>

Get node at path.

Source

pub fn fill_path(&self, path: &mut NodePath, node: &Self) -> bool

Fill path to node.

Returns true if found.

Source

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

Fetch the branch’s children from backend.

If depth is None will populate all depths.

If depth is 0 or not a Branch will do nothing.

Note that populating a node will not automatically expand it.

Source

pub fn add_child<IdT, LabelT>(&mut self, kind: NodeKind, id: IdT, label: LabelT)
where IdT: Into<BackendT::ID>, LabelT: Into<SpannedString<Style>>,

Add a child node.

Will do nothing if not a Branch.

Examples found in repository?
examples/simple.rs (line 22)
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}
Source

pub fn insert_child<IdT, LabelT>( &mut self, index: usize, kind: NodeKind, id: IdT, label: LabelT, )
where IdT: Into<BackendT::ID>, LabelT: Into<SpannedString<Style>>,

Inserts a child node.

Will do nothing if not a Branch.

Source

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

Expand the branch.

If depth is None will expand all depths.

If depth is 0 or not a Branch will do nothing.

Note that this will populate expanded nodes from the backend.

Source

pub fn collapse(&mut self, depth: Option<usize>)

Collapse the branch.

If depth is None will collapse all depths.

If depth is 0 or not a Branch will do nothing.

Source

pub fn toggle_branch_state( &mut self, context: BackendT::Context, ) -> Result<(), BackendT::Error>
where BackendT::Context: Clone,

Toggle the branch state.

Expand if collapsed, collapse if expanded.

Will do nothing if not a Branch.

Source

pub fn data( &mut self, context: BackendT::Context, ) -> Result<Option<Cow<'_, BackendT::Data>>, BackendT::Error>
where BackendT::Data: Clone,

Data.

If not already cached will fetch it from the backend.

Examples found in repository?
examples/file_browser.rs (line 75)
69    fn handle_selection_changed(cursive: &mut Cursive, base_directory: Self::Context) {
70        let content = match cursive.call_on_name("tree", |tree: &mut TreeView<Self>| {
71            // Note that although we're not using the context in data() we still need to provide it
72            // (some implementations might need it)
73            let base_directory = tree.model.context.clone();
74            Ok(match tree.selected_node_mut() {
75                Some(node) => match node.data(base_directory)? {
76                    Some(metadata) => Some(format_metadata(&metadata)?),
77                    None => None,
78                },
79                None => None,
80            })
81        }) {
82            Some(Ok(Some(text))) => text,
83            Some(Err(error)) => return Self::handle_error(cursive, base_directory, error),
84            _ => "".into(),
85        };
86
87        cursive.call_on_name("details", |details: &mut TextView| details.set_content(content));
88    }

Trait Implementations§

Source§

impl<BackendT> FromIterator<Node<BackendT>> for NodeList<BackendT>
where BackendT: TreeBackend,

Source§

fn from_iter<IteratorT>(iterator: IteratorT) -> Self
where IteratorT: IntoIterator<Item = Node<BackendT>>,

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<BackendT> Freeze for Node<BackendT>
where <BackendT as TreeBackend>::ID: Freeze, <BackendT as TreeBackend>::Data: Freeze,

§

impl<BackendT> RefUnwindSafe for Node<BackendT>
where <BackendT as TreeBackend>::ID: RefUnwindSafe, <BackendT as TreeBackend>::Data: RefUnwindSafe, BackendT: RefUnwindSafe,

§

impl<BackendT> Send for Node<BackendT>
where <BackendT as TreeBackend>::ID: Send, <BackendT as TreeBackend>::Data: Send, BackendT: Send,

§

impl<BackendT> Sync for Node<BackendT>
where <BackendT as TreeBackend>::ID: Sync, <BackendT as TreeBackend>::Data: Sync, BackendT: Sync,

§

impl<BackendT> Unpin for Node<BackendT>
where <BackendT as TreeBackend>::ID: Unpin, <BackendT as TreeBackend>::Data: Unpin, BackendT: Unpin,

§

impl<BackendT> UnsafeUnpin for Node<BackendT>
where <BackendT as TreeBackend>::ID: UnsafeUnpin, <BackendT as TreeBackend>::Data: UnsafeUnpin,

§

impl<BackendT> UnwindSafe for Node<BackendT>
where <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.