Tree view
tree_view(model, row) shows a TreeModel as an indented, virtualized
list. The arrow before a node with children expands or collapses it, and
row draws the rest of each line. Tree is an in-memory model of Keyed
items. What is expanded is view state, kept apart from the model.
use dfui::Element;
use dfui::model::{Keyed, Tree};
use dfui::widgets::{Styled, column, text, tree_view};
#[derive(Clone)]
struct Entry(u64, &'static str);
impl Keyed for Entry {
fn key(&self) -> u64 {
self.0
}
}
fn files() -> Tree<Entry> {
let mut t = Tree::new();
let src = t.push(None, Entry(1, "src"));
t.push(Some(src), Entry(2, "lib.rs"));
let widgets = t.push(Some(src), Entry(3, "widgets"));
t.push(Some(widgets), Entry(4, "button.rs"));
t.push(Some(widgets), Entry(5, "list.rs"));
t.push(None, Entry(6, "README.md"));
t
}
fn view() -> Element {
let tree = tree_view(files(), |e: &Entry, _| text(e.1));
column([tree.fill().into()]).fill().padding(16.0).into()
}
fn main() {
dfui::run(view);
}
Run it natively: cargo run -p dfui-site --example tree_view