mod access;
mod drag;
mod input;
mod model;
mod show;
mod state;
pub use super::disclosure::TreeStyle;
use super::Ui;
use crate::{Id, Rect, Vec2};
pub(crate) use input::TreeInput;
pub use model::{TreeChildren, TreeEvent, TreeIssue, TreeModel, TreeNode};
pub(crate) use state::TreeState;
use std::{collections::HashSet, hash::Hash, ops::Range};
pub struct TreeView<'a> {
id: Id,
initial_open: Vec<Id>,
initial_selected: Option<Id>,
controlled_open: Option<&'a mut HashSet<Id>>,
controlled_selected: Option<&'a mut Option<Id>>,
follows_focus: bool,
double_click_expand: bool,
enabled: bool,
drag: bool,
height: f32,
style: Option<TreeStyle>,
reveal: Option<Id>,
label: String,
}
#[derive(Debug)]
pub struct TreeOutput {
pub id: Id,
pub selected: Option<Id>,
pub focused: Option<Id>,
pub events: Vec<TreeEvent>,
pub issues: Vec<TreeIssue>,
pub visible_rows: Range<usize>,
pub logical_rows: usize,
pub rows_built: usize,
pub rebuilt: bool,
pub viewport: Rect,
pub scroll_offset: Vec2,
}
impl<'a> TreeView<'a> {
pub fn new(source: impl Hash) -> Self {
Self {
id: Id::new(source),
initial_open: Vec::new(),
initial_selected: None,
controlled_open: None,
controlled_selected: None,
follows_focus: false,
double_click_expand: false,
enabled: true,
drag: false,
height: 300.0,
style: None,
reveal: None,
label: String::new(),
}
}
pub fn id_source(mut self, source: impl Hash) -> Self {
self.id = Id::new(source);
self
}
pub fn default_open(mut self, nodes: impl IntoIterator<Item = Id>) -> Self {
self.initial_open = nodes.into_iter().collect();
self
}
pub fn default_selected(mut self, node: Option<Id>) -> Self {
self.initial_selected = node;
self
}
pub fn open(mut self, nodes: &'a mut HashSet<Id>) -> Self {
self.controlled_open = Some(nodes);
self
}
pub fn selected(mut self, node: &'a mut Option<Id>) -> Self {
self.controlled_selected = Some(node);
self
}
pub fn selection_follows_focus(mut self, follows: bool) -> Self {
self.follows_focus = follows;
self
}
pub fn expand_on_double_click(mut self, expand: bool) -> Self {
self.double_click_expand = expand;
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn drag_nodes(mut self, enabled: bool) -> Self {
self.drag = enabled;
self
}
pub fn max_height(mut self, height: f32) -> Self {
self.height = super::disclosure::dimension(height);
self
}
pub fn style(mut self, style: TreeStyle) -> Self {
self.style = Some(style);
self
}
#[track_caller]
pub fn row_height(mut self, height: f32) -> Self {
if let Some(height) = super::sanitize::positive("TreeView::row_height", height) {
self.style.get_or_insert_with(Default::default).row.height = Some(height);
}
self
}
pub fn reveal_node(mut self, node: Id) -> Self {
self.reveal = Some(node);
self
}
pub fn accessible_label(mut self, label: impl Into<String>) -> Self {
self.label = label.into();
self
}
pub fn show(self, ui: &mut Ui<'_>, model: &impl TreeModel) -> TreeOutput {
self.show_with_actions(ui, model, |_, _| ())
}
pub fn show_with_actions(
self,
ui: &mut Ui<'_>,
model: &impl TreeModel,
actions: impl FnMut(&mut Ui<'_>, Id),
) -> TreeOutput {
ui.layout_item(|ui| self.show_inner(ui, model, actions))
}
}
impl Ui<'_> {
pub fn tree(&mut self, source: impl Hash, model: &impl TreeModel) -> TreeOutput {
TreeView::new(source).show(self, model)
}
}