use super::*;
fn tree_do<R>(id: WidgetId, f: impl FnOnce(&core::TreeData) -> R) -> Option<R> {
core::read(id, |n| n.tree().map(f)).flatten()
}
impl Tree {
pub fn new(parent: impl Into<WidgetId>) -> Tree {
make(Tree::from_id, Kind::Tree, parent, |_| {})
}
pub fn add(&self, parent: Option<TreeNodeId>, text: &str) -> TreeNodeId {
self.insert(parent, usize::MAX, text)
}
pub fn insert(&self, parent: Option<TreeNodeId>, index: usize, text: &str) -> TreeNodeId {
let mut out = 0;
core::data_update(self.id(), core::Data::TreeRows, |n| {
if let Some(t) = n.tree_mut() {
out = t.insert(parent.map(|p| p.0), index, text);
}
});
TreeNodeId(out)
}
pub fn remove(&self, node: TreeNodeId) {
core::data_update(self.id(), core::Data::TreeRows, |n| {
if let Some(t) = n.tree_mut() {
t.remove(node.0);
}
});
}
pub fn clear(&self) {
core::data_update(self.id(), core::Data::TreeRows, |n| {
if let Some(t) = n.tree_mut() {
t.clear();
}
});
}
pub fn set_text(&self, node: TreeNodeId, text: &str) {
core::data_update(self.id(), core::Data::TreeRows, |n| {
if let Some(x) = n.tree_mut().and_then(|t| t.nodes.get_mut(&node.0)) {
x.text = text.to_string();
}
});
}
pub fn text(&self, node: TreeNodeId) -> String {
tree_do(self.id(), |t| t.nodes.get(&node.0).map(|n| n.text.clone()))
.flatten()
.unwrap_or_default()
}
pub fn children(&self, parent: Option<TreeNodeId>) -> Vec<TreeNodeId> {
tree_do(self.id(), |t| {
t.children_of(parent.map(|p| p.0))
.iter()
.map(|c| TreeNodeId(*c))
.collect()
})
.unwrap_or_default()
}
pub fn parent(&self, node: TreeNodeId) -> Option<TreeNodeId> {
tree_do(self.id(), |t| t.nodes.get(&node.0).and_then(|n| n.parent))
.flatten()
.map(TreeNodeId)
}
pub fn contains(&self, node: TreeNodeId) -> bool {
tree_do(self.id(), |t| t.nodes.contains_key(&node.0)).unwrap_or(false)
}
pub fn len(&self) -> usize {
tree_do(self.id(), |t| t.nodes.len()).unwrap_or(0)
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn set_expanded(&self, node: TreeNodeId, v: bool) {
core::data_update(self.id(), core::Data::TreeRows, |n| {
if let Some(x) = n.tree_mut().and_then(|t| t.nodes.get_mut(&node.0)) {
x.expanded = v;
}
});
}
pub fn expanded(&self, node: TreeNodeId) -> bool {
tree_do(self.id(), |t| {
t.nodes.get(&node.0).is_some_and(|n| n.expanded)
})
.unwrap_or(false)
}
pub fn expand_all(&self, v: bool) {
core::data_update(self.id(), core::Data::TreeRows, |n| {
if let Some(t) = n.tree_mut() {
t.nodes.values_mut().for_each(|x| x.expanded = v);
}
});
}
pub fn set_has_children(&self, node: TreeNodeId, v: bool) {
core::data_update(self.id(), core::Data::TreeRows, |n| {
if let Some(x) = n.tree_mut().and_then(|t| t.nodes.get_mut(&node.0)) {
x.has_children = v;
}
});
}
pub fn set_selected(&self, node: Option<TreeNodeId>) {
core::data_update(self.id(), core::Data::TreeRows, |n| {
if let Some(t) = n.tree_mut() {
match node.filter(|x| t.nodes.contains_key(&x.0)) {
Some(x) => {
t.reveal(x.0);
t.selected = Some(x.0);
}
None => t.selected = None,
}
}
});
}
pub fn selected(&self) -> Option<TreeNodeId> {
tree_do(self.id(), |t| t.selected).flatten().map(TreeNodeId)
}
pub fn batch(&self, f: impl FnOnce(&Tree)) {
core::freeze(self.id(), true);
let _g = Thaw(self.id());
f(self)
}
pub fn on_select(&self, mut f: impl FnMut(Option<TreeNodeId>) + 'static) {
on(self.id(), Ev::TreeSelected, move |e| {
if let Event::TreeSelected(x) = e {
f(x.map(TreeNodeId))
}
})
}
pub fn on_activate(&self, mut f: impl FnMut(TreeNodeId) + 'static) {
on(self.id(), Ev::TreeActivated, move |e| {
if let Event::TreeActivated(x) = e {
f(TreeNodeId(*x))
}
})
}
pub fn on_expand(&self, mut f: impl FnMut(TreeNodeId, bool) + 'static) {
on(self.id(), Ev::TreeExpanded, move |e| {
if let Event::TreeExpanded(x, b) = e {
f(TreeNodeId(*x), *b)
}
})
}
}