#![deny(unsafe_code)]
pub const NO_PARENT: i32 = -1;
#[allow(unsafe_code)]
pub mod tree_view {
slint::include_modules!();
}
pub use tree_view::{ActivationMode, TreeItem, TreeView, TreeViewStyle};
impl TreeItem {
pub fn branch(
internal_id: i32,
parent_internal_id: i32,
depth: i32,
text: impl Into<slint::SharedString>,
) -> Self {
Self {
internal_id,
parent_internal_id,
depth,
text: text.into(),
label: "".into(),
decoration_text: "".into(),
item_type: 0,
user_data: String::new().into(),
has_children: true,
expanded: true,
selectable: true,
enabled: true,
}
}
pub fn leaf(
internal_id: i32,
parent_internal_id: i32,
depth: i32,
text: impl Into<slint::SharedString>,
user_data: impl Into<slint::SharedString>,
) -> Self {
Self {
internal_id,
parent_internal_id,
depth,
text: text.into(),
label: "".into(),
decoration_text: "".into(),
item_type: 0,
user_data: user_data.into(),
has_children: false,
expanded: false,
selectable: true,
enabled: true,
}
}
pub fn section(
internal_id: i32,
parent_internal_id: i32,
depth: i32,
text: impl Into<slint::SharedString>,
) -> Self {
Self {
internal_id,
parent_internal_id,
depth,
text: text.into(),
label: "".into(),
decoration_text: "".into(),
item_type: 0,
user_data: String::new().into(),
has_children: false,
expanded: false,
selectable: false,
enabled: true,
}
}
pub fn with_icon(self, icon: impl Into<slint::SharedString>) -> Self {
self.with_decoration(icon)
}
pub fn with_label(mut self, label: impl Into<slint::SharedString>) -> Self {
self.label = label.into();
self
}
pub fn with_decoration(mut self, decoration: impl Into<slint::SharedString>) -> Self {
self.decoration_text = decoration.into();
self
}
pub fn collapsed(mut self) -> Self {
self.expanded = false;
self
}
pub fn with_expanded(mut self, expanded: bool) -> Self {
self.expanded = expanded;
self
}
pub fn disabled(mut self) -> Self {
self.enabled = false;
self
}
pub fn non_selectable(mut self) -> Self {
self.selectable = false;
self
}
pub fn with_item_type(mut self, item_type: i32) -> Self {
self.item_type = item_type;
self
}
pub fn with_user_data(mut self, user_data: impl Into<slint::SharedString>) -> Self {
self.user_data = user_data.into();
self
}
}
pub fn index_of_id(items: &[TreeItem], internal_id: i32) -> Option<usize> {
items.iter().position(|r| r.internal_id == internal_id)
}
pub fn index_of_id_model(model: &slint::ModelRc<TreeItem>, internal_id: i32) -> Option<usize> {
use slint::Model as _;
(0..model.row_count()).find(|&i| {
model
.row_data(i)
.is_some_and(|r| r.internal_id == internal_id)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_blank_leaf() {
let item = TreeItem::default();
assert_eq!(item.depth, 0);
assert_eq!(item.internal_id, 0);
assert_eq!(item.parent_internal_id, 0);
assert!(!item.has_children);
assert!(!item.expanded);
}
#[test]
fn branch_constructor_marks_has_children_and_expanded() {
let item = TreeItem::branch(7, NO_PARENT, 0, "Inbox");
assert_eq!(item.internal_id, 7);
assert_eq!(item.parent_internal_id, NO_PARENT);
assert_eq!(item.depth, 0);
assert_eq!(item.text.as_str(), "Inbox");
assert!(item.has_children);
assert!(item.expanded, "branches default to expanded");
assert!(item.selectable);
assert!(item.enabled);
assert_eq!(
item.decoration_text.as_str(),
"",
"no default decoration — host decides"
);
}
#[test]
fn leaf_constructor_marks_no_children() {
let item = TreeItem::leaf(3, 7, 1, "Welcome", "Hello world");
assert_eq!(item.internal_id, 3);
assert_eq!(item.parent_internal_id, 7);
assert_eq!(item.depth, 1);
assert_eq!(item.user_data.as_str(), "Hello world");
assert!(!item.has_children);
assert!(!item.expanded);
}
#[test]
fn section_constructor_is_non_selectable() {
let item = TreeItem::section(100, NO_PARENT, 0, "Archived");
assert!(!item.selectable, "sections are skipped by selection");
assert!(item.enabled, "sections are still visible/enabled");
assert!(!item.has_children);
}
#[test]
fn builder_methods_chain() {
let item = TreeItem::branch(1, NO_PARENT, 0, "Inbox")
.with_icon("📥")
.collapsed()
.with_item_type(42);
assert_eq!(item.decoration_text.as_str(), "📥");
assert!(!item.expanded, "collapsed() flips the default");
assert_eq!(item.item_type, 42);
assert!(
item.has_children,
"chaining must not clobber structural fields"
);
}
#[test]
fn disabled_and_non_selectable_chain() {
let item = TreeItem::leaf(2, NO_PARENT, 0, "x", "y")
.disabled()
.non_selectable();
assert!(!item.enabled);
assert!(!item.selectable);
}
#[test]
fn index_of_id_finds_first_match() {
let items = [
TreeItem::leaf(1, NO_PARENT, 0, "a", ""),
TreeItem::leaf(2, NO_PARENT, 0, "b", ""),
TreeItem::leaf(2, NO_PARENT, 0, "dup-of-b", ""),
];
assert_eq!(index_of_id(&items, 1), Some(0));
assert_eq!(index_of_id(&items, 2), Some(1), "first match wins");
assert_eq!(index_of_id(&items, 99), None);
}
#[test]
fn index_of_id_on_empty() {
let items: [TreeItem; 0] = [];
assert_eq!(index_of_id(&items, 1), None);
}
#[test]
fn no_parent_is_minus_one() {
assert_eq!(NO_PARENT, -1);
}
}