pub trait TreeBackend: Sized {
type Context;
type Error;
type ID;
type Data;
// Provided methods
fn tree_view(context: Self::Context) -> TreeView<Self> { ... }
fn tree_model(context: Self::Context) -> TreeModel<Self> { ... }
fn symbol(node: &Node<Self>, context: Self::Context) -> Symbol<'_> { ... }
fn roots(context: Self::Context) -> Result<NodeList<Self>, Self::Error> { ... }
fn populate(
node: &mut Node<Self>,
context: Self::Context,
) -> Result<(), Self::Error> { ... }
fn data(
node: &mut Node<Self>,
context: Self::Context,
) -> Result<Option<(Self::Data, bool)>, Self::Error> { ... }
fn handle_selection_changed(cursive: &mut Cursive, context: Self::Context) { ... }
fn handle_error(
cursive: &mut Cursive,
context: Self::Context,
error: Self::Error,
) { ... }
}Expand description
Tree view backend.
Also see SimpleTreeBackend.
Required Associated Types§
Sourcetype Context
type Context
A context is provided as an argument to the trait functions.
It is intended to be used to access node data. For example, it can be a database connection or a work directory.
The context is stored in the context field of the model. A clone of it will be produced for every call, thus cloning should best be cheap. Consider wrapping it in an Arc.
If you don’t need it, it can be set to ().
Sourcetype Error
type Error
Returned as the Err of some of the trait functions.
You can implement handle_error to display an error message to the user, log it, etc.
If you don’t need it, it can be set to ().
Provided Methods§
Sourcefn tree_view(context: Self::Context) -> TreeView<Self>
fn tree_view(context: Self::Context) -> TreeView<Self>
Create a tree view with this backend.
Examples found in repository?
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}Sourcefn tree_model(context: Self::Context) -> TreeModel<Self>
fn tree_model(context: Self::Context) -> TreeModel<Self>
Create a tree model with this backend.
Sourcefn symbol(node: &Node<Self>, context: Self::Context) -> Symbol<'_>
fn symbol(node: &Node<Self>, context: Self::Context) -> Symbol<'_>
Get a node’s symbol.
Note that its char count must be 1.
The default implementation uses the node kind and branch state.
Sourcefn roots(context: Self::Context) -> Result<NodeList<Self>, Self::Error>
fn roots(context: Self::Context) -> Result<NodeList<Self>, Self::Error>
Get the root nodes.
The root nodes must be depth 0 but otherwise can be as pre-populated as you wish. For example, you can provide them with children and optional data.
The default implementation returns an empty list.
Sourcefn populate(
node: &mut Node<Self>,
context: Self::Context,
) -> Result<(), Self::Error>
fn populate( node: &mut Node<Self>, context: Self::Context, ) -> Result<(), Self::Error>
Populate a node’s children.
The children must have a depth of +1 of the node.
This is called when expanding the node only if it’s children field is None. Setting children to Some, even to an empty list, will ensure that this function won’t be called again for the node until it is set to None.
The default implementation does nothing.
Sourcefn data(
node: &mut Node<Self>,
context: Self::Context,
) -> Result<Option<(Self::Data, bool)>, Self::Error>
fn data( node: &mut Node<Self>, context: Self::Context, ) -> Result<Option<(Self::Data, bool)>, Self::Error>
Sourcefn handle_selection_changed(cursive: &mut Cursive, context: Self::Context)
fn handle_selection_changed(cursive: &mut Cursive, context: Self::Context)
Called when the selected node changes, including when the selection is set to None.
Accessing the currently selected node must be done via the tree view, e.g. selected_node. Thus, you may want to wrap the tree view in a NamedView. You can then access it here like so:
match cursive.call_on_name("MyTree", |tree: &mut TreeView<MyBackend>| {
Ok(match tree.selected_node_mut() {
Some(node) => node.data(context)?.map(|data| data.something.clone()),
None => None,
})
}) {
Some(Ok(Some(something))) => do_something_with(something),
Some(Err(error)) => BackendT::handle_error(cursive, error),
_ => {},
}Sourcefn handle_error(
cursive: &mut Cursive,
context: Self::Context,
error: Self::Error,
)
fn handle_error( cursive: &mut Cursive, context: Self::Context, error: Self::Error, )
Called when other backend functions return Err.
Examples found in repository?
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 }Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.