mod component;
mod types;
pub(crate) use component::*;
pub use types::*;
use crate::callback::Callback;
use crate::core::element::Element;
use crate::style::{Length, ScrollbarConfig, Style, StyleSlot};
use crate::utils::gradient::ColorGradient;
use crate::widgets::FocusAccordion;
use crate::widgets::ScrollKeymap;
use std::sync::Arc;
#[derive(Clone)]
pub struct Tree {
props: TreeProps,
}
impl Tree {
pub fn new(root: TreeNode) -> Self {
Self {
props: TreeProps {
root,
selected: None,
force_scroll_to_selected: false,
gap: 0,
icon_gap: 1,
show_icons: true,
expanded_icon: "▼".into(),
collapsed_icon: "▶".into(),
leaf_icon: None,
icon_style: Style::default(),
width: Length::Flex(1),
height: Length::Flex(1),
style: Style::default(),
hover_style: StyleSlot::Inherit,
item_hover_style: StyleSlot::Inherit,
selection_style: StyleSlot::Inherit,
unfocused_selection_style: StyleSlot::Inherit,
selection_symbol: None,
selection_symbol_style: None,
unfocused_selection_symbol_style: None,
scrollbar: false,
scrollbar_config: ScrollbarConfig::default(),
scroll_keys: ScrollKeymap::default(),
scroll_wheel: true,
show_scroll_indicators: false,
scroll_indicator_style: Style::default(),
empty_text: None,
empty_text_style: Style::default(),
activate_on_click: true,
focusable: true,
on_select: None,
on_activate: None,
on_toggle: None,
keymap: TreeKeymap::default(),
focus_policy: None,
indent_style: IndentStyle::None,
indent_guide_style: Style::default(),
indent_gradient: None,
solid_indent_connector_gap: false,
selection_full_width: false,
unselected_symbol: None,
key_interceptor: None,
},
}
}
pub fn indent_style(mut self, style: IndentStyle) -> Self {
self.props.indent_style = style;
self
}
pub fn indent_guide_style(mut self, style: Style) -> Self {
self.props.indent_guide_style = style;
self
}
pub fn indent_gradient(mut self, gradient: ColorGradient) -> Self {
self.props.indent_gradient = Some(gradient);
self
}
pub(crate) fn solid_indent_connector_gap(mut self, solid: bool) -> Self {
self.props.solid_indent_connector_gap = solid;
self
}
pub fn selection_full_width(mut self, full_width: bool) -> Self {
self.props.selection_full_width = full_width;
self
}
pub fn unselected_symbol(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
self.props.unselected_symbol = symbol.map(Into::into);
self
}
pub fn gap(mut self, gap: u16) -> Self {
self.props.gap = gap;
self
}
pub fn selected(mut self, selected: usize) -> Self {
self.props.selected = Some(selected);
self
}
pub fn force_scroll_to_selected(mut self, force: bool) -> Self {
self.props.force_scroll_to_selected = force;
self
}
pub fn icon_gap(mut self, gap: u16) -> Self {
self.props.icon_gap = gap;
self
}
pub fn show_icons(mut self, show: bool) -> Self {
self.props.show_icons = show;
self
}
pub fn expanded_icon(mut self, icon: impl Into<Arc<str>>) -> Self {
self.props.expanded_icon = icon.into();
self
}
pub fn collapsed_icon(mut self, icon: impl Into<Arc<str>>) -> Self {
self.props.collapsed_icon = icon.into();
self
}
pub fn leaf_icon(mut self, icon: Option<impl Into<Arc<str>>>) -> Self {
self.props.leaf_icon = icon.map(Into::into);
self
}
pub fn icon_style(mut self, style: Style) -> Self {
self.props.icon_style = style;
self
}
pub fn style(mut self, style: Style) -> Self {
self.props.style = style;
self
}
pub fn hover_style(mut self, style: Style) -> Self {
self.props.hover_style = StyleSlot::Replace(style);
self
}
pub fn extend_hover_style(mut self, style: Style) -> Self {
self.props.hover_style = StyleSlot::Extend(style);
self
}
pub fn inherit_hover_style(mut self) -> Self {
self.props.hover_style = StyleSlot::Inherit;
self
}
pub fn hover_style_slot(mut self, slot: StyleSlot) -> Self {
self.props.hover_style = slot;
self
}
pub fn item_hover_style(mut self, style: Style) -> Self {
self.props.item_hover_style = StyleSlot::Replace(style);
self
}
pub fn extend_item_hover_style(mut self, style: Style) -> Self {
self.props.item_hover_style = StyleSlot::Extend(style);
self
}
pub fn inherit_item_hover_style(mut self) -> Self {
self.props.item_hover_style = StyleSlot::Inherit;
self
}
pub fn item_hover_style_slot(mut self, slot: StyleSlot) -> Self {
self.props.item_hover_style = slot;
self
}
pub fn selection_style(mut self, style: Style) -> Self {
self.props.selection_style = StyleSlot::Replace(style);
self
}
pub fn extend_selection_style(mut self, style: Style) -> Self {
self.props.selection_style = StyleSlot::Extend(style);
self
}
pub fn inherit_selection_style(mut self) -> Self {
self.props.selection_style = StyleSlot::Inherit;
self
}
pub fn selection_style_slot(mut self, slot: StyleSlot) -> Self {
self.props.selection_style = slot;
self
}
pub fn unfocused_selection_style(mut self, style: Style) -> Self {
self.props.unfocused_selection_style = StyleSlot::Replace(style);
self
}
pub fn extend_unfocused_selection_style(mut self, style: Style) -> Self {
self.props.unfocused_selection_style = StyleSlot::Extend(style);
self
}
pub fn inherit_unfocused_selection_style(mut self) -> Self {
self.props.unfocused_selection_style = StyleSlot::Inherit;
self
}
pub fn unfocused_selection_style_slot(mut self, slot: StyleSlot) -> Self {
self.props.unfocused_selection_style = slot;
self
}
pub fn selection_symbol(mut self, symbol: Option<impl Into<Arc<str>>>) -> Self {
self.props.selection_symbol = symbol.map(Into::into);
self
}
pub fn selection_symbol_style(mut self, style: Option<Style>) -> Self {
self.props.selection_symbol_style = style;
self
}
pub fn unfocused_selection_symbol_style(mut self, style: Option<Style>) -> Self {
self.props.unfocused_selection_symbol_style = style;
self
}
pub fn width(mut self, width: Length) -> Self {
self.props.width = width;
self
}
pub fn height(mut self, height: Length) -> Self {
self.props.height = height;
self
}
pub fn scrollbar(mut self, scrollbar: bool) -> Self {
self.props.scrollbar = scrollbar;
self
}
pub fn scrollbar_config(mut self, config: ScrollbarConfig) -> Self {
self.props.scrollbar_config = config;
self
}
pub fn scroll_keys(mut self, keys: ScrollKeymap) -> Self {
self.props.scroll_keys = keys;
self
}
pub fn scroll_wheel(mut self, enabled: bool) -> Self {
self.props.scroll_wheel = enabled;
self
}
pub fn show_scroll_indicators(mut self, show: bool) -> Self {
self.props.show_scroll_indicators = show;
self
}
pub fn scroll_indicator_style(mut self, style: Style) -> Self {
self.props.scroll_indicator_style = style;
self
}
pub fn empty_text(mut self, text: impl Into<Arc<str>>) -> Self {
self.props.empty_text = Some(text.into());
self
}
pub fn empty_text_style(mut self, style: Style) -> Self {
self.props.empty_text_style = style;
self
}
pub fn activate_on_click(mut self, activate: bool) -> Self {
self.props.activate_on_click = activate;
self
}
pub fn focusable(mut self, focusable: bool) -> Self {
self.props.focusable = focusable;
self
}
pub fn on_select(mut self, cb: Callback<TreeEvent>) -> Self {
self.props.on_select = Some(cb);
self
}
pub fn on_activate(mut self, cb: Callback<TreeEvent>) -> Self {
self.props.on_activate = Some(cb);
self
}
pub fn on_toggle(mut self, cb: Callback<TreeToggleEvent>) -> Self {
self.props.on_toggle = Some(cb);
self
}
pub fn keymap(mut self, keymap: TreeKeymap) -> Self {
self.props.keymap = keymap;
self
}
pub fn focus_policy(mut self, policy: FocusAccordion) -> Self {
self.props.focus_policy = Some(policy);
self
}
pub fn key_interceptor(mut self, handler: crate::callback::KeyHandler) -> Self {
self.props.key_interceptor = Some(handler);
self
}
}
impl From<Tree> for Element {
fn from(tree: Tree) -> Self {
crate::child(TreeComponent::new, tree.props)
}
}