pub struct Node<BackendT>where
BackendT: TreeBackend,{
pub depth: usize,
pub kind: NodeKind,
pub id: BackendT::ID,
pub representation: Representation,
pub representation_size: Vec2,
pub branch_state: BranchState,
pub children: Option<NodeList<BackendT>>,
pub data: Option<BackendT::Data>,
/* private fields */
}Expand description
Tree node.
Fields§
§depth: usizeDepth.
kind: NodeKindKind.
id: BackendT::IDID.
representation: RepresentationRepresentation.
representation_size: Vec2Representation size.
Cached so we only have to calculate it once.
Note that nodes can ostensibly have heights >1, i.e. they can be multiline, however this is not currently supported by TreeView.
branch_state: BranchStateState 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,
impl<BackendT> Node<BackendT>where
BackendT: TreeBackend,
Sourcepub fn new(
depth: usize,
kind: NodeKind,
id: BackendT::ID,
representation: Representation,
) -> Self
pub fn new( depth: usize, kind: NodeKind, id: BackendT::ID, representation: Representation, ) -> Self
Constructor.
Sourcepub fn symbol(&self, context: BackendT::Context) -> Symbol<'_>
pub fn symbol(&self, context: BackendT::Context) -> Symbol<'_>
Symbol.
Its char count is always 1.
Sourcepub fn at_path_mut(&mut self, path: NodePath) -> Option<&mut Self>
pub fn at_path_mut(&mut self, path: NodePath) -> Option<&mut Self>
Get node at path.
Sourcepub fn fill_path(&self, path: &mut NodePath, node: &Self) -> bool
pub fn fill_path(&self, path: &mut NodePath, node: &Self) -> bool
Fill path to node.
Returns true if found.
Sourcepub fn populate(
&mut self,
depth: Option<usize>,
context: BackendT::Context,
) -> Result<(), BackendT::Error>
pub fn populate( &mut self, depth: Option<usize>, context: BackendT::Context, ) -> Result<(), BackendT::Error>
Sourcepub fn add_child(
&mut self,
kind: NodeKind,
id: BackendT::ID,
representation: Representation,
)
pub fn add_child( &mut self, kind: NodeKind, id: BackendT::ID, representation: Representation, )
Add a child node.
Will do nothing if not a Branch.
Examples found in repository?
8fn main() {
9 let mut cursive = 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 // and expand it
20
21 let world = tree.model.at_path_mut([1].into()).unwrap();
22 for i in 0..10 {
23 world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1).into());
24 }
25 world.expand(()).unwrap();
26
27 // Add 10 grandchildren each to "Child #5" -> "Child #10"
28 // Note that we change them to branch in order to support having children
29
30 let mut gc = 0;
31 for c in 4..10 {
32 let child = tree.model.at_path_mut([1, c].into()).unwrap();
33 child.kind = NodeKind::Branch;
34 for _ in 0..10 {
35 child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1).into());
36 gc += 1;
37 }
38 }
39
40 cursive.add_fullscreen_layer(Panel::new(tree.scrollable()));
41 cursive.add_global_callback('q', |cursive| cursive.quit());
42
43 cursive.run();
44}Sourcepub fn insert_child(
&mut self,
index: usize,
kind: NodeKind,
id: BackendT::ID,
representation: Representation,
)
pub fn insert_child( &mut self, index: usize, kind: NodeKind, id: BackendT::ID, representation: Representation, )
Inserts a child node.
Will do nothing if not a Branch.
Sourcepub fn expand(
&mut self,
context: BackendT::Context,
) -> Result<bool, BackendT::Error>
pub fn expand( &mut self, context: BackendT::Context, ) -> Result<bool, BackendT::Error>
Expand the branch.
Will return false if not a Branch or already expanded.
Note that this will populate the node from the backend.
Examples found in repository?
8fn main() {
9 let mut cursive = 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 // and expand it
20
21 let world = tree.model.at_path_mut([1].into()).unwrap();
22 for i in 0..10 {
23 world.add_child(NodeKind::Leaf, (), format!("Child #{}", i + 1).into());
24 }
25 world.expand(()).unwrap();
26
27 // Add 10 grandchildren each to "Child #5" -> "Child #10"
28 // Note that we change them to branch in order to support having children
29
30 let mut gc = 0;
31 for c in 4..10 {
32 let child = tree.model.at_path_mut([1, c].into()).unwrap();
33 child.kind = NodeKind::Branch;
34 for _ in 0..10 {
35 child.add_child(NodeKind::Leaf, (), format!("Grandchild #{}", gc + 1).into());
36 gc += 1;
37 }
38 }
39
40 cursive.add_fullscreen_layer(Panel::new(tree.scrollable()));
41 cursive.add_global_callback('q', |cursive| cursive.quit());
42
43 cursive.run();
44}Sourcepub fn collapse(&mut self) -> bool
pub fn collapse(&mut self) -> bool
Collapse the branch.
Will return false if not a Branch or already collapsed.
Sourcepub fn toggle_branch_state(
&mut self,
context: BackendT::Context,
) -> Result<(), BackendT::Error>
pub fn toggle_branch_state( &mut self, context: BackendT::Context, ) -> Result<(), BackendT::Error>
Toggle the branch state.
Expand if collapsed, collapse if expanded.
Will do nothing if not a Branch.
Sourcepub fn data(
&mut self,
context: BackendT::Context,
) -> Result<Option<Cow<'_, BackendT::Data>>, BackendT::Error>
pub fn data( &mut self, context: BackendT::Context, ) -> Result<Option<Cow<'_, BackendT::Data>>, BackendT::Error>
Data.
If not already cached will fetch it from the backend.
Examples found in repository?
63 fn handle_selection_changed(cursive: &mut Cursive) {
64 let content = match cursive.call_on_name("tree", |tree: &mut TreeView<Self>| {
65 // Although we're not using the context in data() but we still need to provide it
66 let base_directory = tree.model.context.clone();
67 Ok(match tree.selected_node_mut() {
68 Some(node) => match node.data(base_directory)? {
69 Some(metadata) => Some(format_metadata(&metadata)?),
70 None => None,
71 },
72 None => None,
73 })
74 }) {
75 Some(Ok(Some(text))) => text,
76 Some(Err(error)) => return Self::handle_error(cursive, error),
77 _ => "".into(),
78 };
79
80 cursive.call_on_name("details", |details: &mut TextView| {
81 details.set_content(content);
82 });
83 }