wijjit.elements.display.tree.Tree
- class wijjit.elements.display.tree.Tree(id=None, classes=None, data=None, multiple=False, selected_ids=None, width=40, height=15, show_scrollbar=True, show_root=True, indent_size=2, indicator_style=TreeIndicatorStyle.TRIANGLES_LARGE, border_style='single', title=None, action=None, tab_index=None, bind=True)[source]
Tree element for displaying hierarchical data with expand/collapse.
This element provides a tree view display with support for: - Hierarchical data visualization with tree drawing characters - Expand/collapse functionality for nodes with children - Node selection with visual indicators (single or multiple) - Scrolling for large trees - Mouse and keyboard interaction
- Parameters:
id (
str, optional) – Element identifierdata (
dictorlist, optional) – Tree data as nested dict or flat listmultiple (
bool, optional) – Enable multiple selection mode (default: False)width (
int, optional) – Display width in columns (default: 40)height (
int, optional) – Display height in rows (default: 15)show_scrollbar (
bool, optional) – Whether to show vertical scrollbar (default: True)show_root (
bool, optional) – Whether to show root node (default: True)indent_size (
int, optional) – Number of spaces per indentation level (default: 2)indicator_style (
TreeIndicatorStyle, optional) – Style for expand/collapse indicators (default: TRIANGLES_LARGE)border_style (
str, optional) – Border style: “single”, “double”, “rounded”, or “none” (default: “none”)title (
str, optional) – Title to display in top border (default: None)action (str | None)
tab_index (int | None)
- Variables:
multiple (
bool) – Whether multiple selection is enabledwidth (
int) – Display widthheight (
int) – Display heightshow_scrollbar (
bool) – Whether scrollbar is visibleshow_root (
bool) – Whether root node is shownindent_size (
int) – Indentation per levelindicator_style (
TreeIndicatorStyle) – Style for expand/collapse indicatorsborder_style (
str) – Border styleexpanded_nodes (
set) – Set of expanded node IDsselected_node_id (
strorNone) – ID of currently selected node (single-select mode)selected_node_ids (
set) – Set of selected node IDs (multi-select mode)highlighted_index (
int) – Index of highlighted node in visible listscroll_manager (
ScrollManager) – Manages scrolling of tree nodes
Notes
Tree data can be specified as: - Nested dict: {“label”: “Root”, “value”: “1”, “children”: […]} - Flat list: [{“id”: “1”, “label”: “Root”, “parent_id”: None}, …]
Navigation: - Up/Down: Navigate nodes - Left: Collapse node or move to parent - Right: Expand node or move to first child - Enter/Space: Toggle expand/collapse (single) or toggle selection (multiple) - Home/End: Jump to first/last node - PageUp/PageDown: Scroll by page
- __init__(id=None, classes=None, data=None, multiple=False, selected_ids=None, width=40, height=15, show_scrollbar=True, show_root=True, indent_size=2, indicator_style=TreeIndicatorStyle.TRIANGLES_LARGE, border_style='single', title=None, action=None, tab_index=None, bind=True)[source]
Initialize scrollable element.
- Parameters:
id (
str, optional) – Element identifierclasses (
strorlistofstrorsetofstr, optional) – CSS class names for stylingtab_index (
int, optional) – Tab order for focus navigationmultiple (bool)
width (int)
height (int)
show_scrollbar (bool)
show_root (bool)
indent_size (int)
indicator_style (TreeIndicatorStyle)
border_style (str)
title (str | None)
action (str | None)
- Return type:
None
Methods
__init__([id, classes, data, multiple, ...])Initialize scrollable element.
add_class(class_name)Add a CSS class to this element.
apply_props(props)Apply props from a VNode, skipping ephemeral props.
can_scroll(direction)Check if the element can scroll in the given direction.
collapse_node(node_id)Collapse a node.
expand_node(node_id)Expand a node.
Get ephemeral state for reconciliation.
get_hardware_cursor_position()Absolute screen cell where the hardware terminal cursor should park.
get_height_for_width(width)Return the height this element needs when laid out at
width.get_intrinsic_size()Get the intrinsic (preferred) size of the element.
handle_key(key)Handle keyboard input for tree navigation.
handle_mouse(event)Handle mouse input.
has_class(class_name)Check if element has a CSS class.
is_selected(node_id)Check if a node is selected.
on_blur()Called when element loses focus.
on_focus()Called when element gains focus.
on_hover_enter()Called when mouse enters element.
on_hover_exit()Called when mouse exits element.
on_mount()Called when element is first added to the element tree.
on_unmount()Called when element is removed from the element tree.
on_update(changed_props)Called when element props are updated during reconciliation.
remove_class(class_name)Remove a CSS class from this element.
render_signature()Hashable/comparable capture of everything
render_to()reads.render_to(ctx)Render tree using cell-based rendering (NEW API).
restore_ephemeral_state(state)Restore ephemeral state after reconciliation.
select_node(node_id)Select a node (single-select mode).
set_bounds(bounds)Set bounds and resize the tree to the space it was allocated.
set_data(data)Update tree data and refresh display.
toggle_class(class_name)Toggle a CSS class on this element.
toggle_node(node_id)Toggle expand/collapse state of a node.
toggle_selection(node_id)Toggle selection of a node (multi-select mode).
Attributes
captures_tabWhether this element wants the Tab key instead of focus movement.
Get the normalized tree data.
Get the state key for expanded nodes.
Get the state key for highlighted index.
parent_frameGet the parent Frame if this element is inside a scrollable frame.
Get the current scroll position.
scroll_state_keyGet the state key for vertical scroll position.
scroll_state_key_xGet the state key for horizontal scroll position.
Get list of selected node IDs.
Get the state key for selected node.
supports_dynamic_sizingWhether this element supports dynamic sizing.
- property expand_state_key: str | None
Get the state key for expanded nodes.
Returns the explicitly set key if provided, otherwise auto-generates from the element id using the convention “{id}:expanded”.
- property highlight_state_key: str | None
Get the state key for highlighted index.
Returns the explicitly set key if provided, otherwise auto-generates from the element id using the convention “{id}:highlight”.
- property selected_state_key: str | None
Get the state key for selected node.
Returns the explicitly set key if provided, otherwise auto-generates from the element id using the convention “{id}:selected”.
- property data: dict
Get the normalized tree data.
- Returns:
Normalized tree data with ‘id’, ‘label’, ‘value’, ‘children’ keys
- Return type:
- property scroll_position: int
Get the current scroll position.
- Returns:
Current scroll offset (0-based)
- Return type:
- toggle_node(node_id)[source]
Toggle expand/collapse state of a node.
- Parameters:
node_id (
str) – ID of node to toggle- Return type:
None
- expand_node(node_id)[source]
Expand a node.
- Parameters:
node_id (
str) – ID of node to expand- Return type:
None
- collapse_node(node_id)[source]
Collapse a node.
- Parameters:
node_id (
str) – ID of node to collapse- Return type:
None
- select_node(node_id)[source]
Select a node (single-select mode).
In multi-select mode, use toggle_selection() instead.
- Parameters:
node_id (
str) – ID of node to select- Return type:
None
- toggle_selection(node_id)[source]
Toggle selection of a node (multi-select mode).
- Parameters:
node_id (
str) – ID of node to toggle- Return type:
None
- property selected_ids: list[str]
Get list of selected node IDs.
In single-select mode, returns a list with one element (or empty). In multi-select mode, returns all selected node IDs.
- handle_key(key)[source]
Handle keyboard input for tree navigation.
- Parameters:
key (
Key) – Key press to handle- Returns:
True if key was handled
- Return type:
- async handle_mouse(event)[source]
Handle mouse input.
- Parameters:
event (
MouseEvent) – Mouse event to handle- Returns:
True if event was handled
- Return type:
- get_ephemeral_state()[source]
Get ephemeral state for reconciliation.
- Returns:
Highlight, selection, expansion, and scroll state that should survive re-renders.
- Return type:
- restore_ephemeral_state(state)[source]
Restore ephemeral state after reconciliation.
- Parameters:
state (
dict) – State dict from get_ephemeral_state()- Return type:
None
- set_bounds(bounds)[source]
Set bounds and resize the tree to the space it was allocated.
self.width/self.heightare the tree’s outer dimensions - the border row and column are drawn from them - so they have to track the bounds the layout engine hands out. Without this the tree keeps drawing at its template-declared size and its right border falls outside the parent’s clip when auto-fit shrinks the row (seewijjit.layout.engine.shrink_to_fit).- Parameters:
bounds (
Bounds) – New bounds for the element.- Return type:
None
- render_to(ctx)[source]
Render tree using cell-based rendering (NEW API).
- Parameters:
ctx (
PaintContext) – Paint context with buffer, style resolver, and bounds- Return type:
None
Notes
This method implements cell-based rendering for tree views with: - Tree drawing characters (├─, └─, │) for hierarchy visualization - Expand/collapse indicators ([+], [-]) - Node selection markers (“>”) - Highlight styling for focused nodes - Theme-based styling for all states - Scrollbar integration
Theme styles:
This element uses the following theme style classes: -
tree: Base tree style -tree:focus: When tree has focus -tree.node: Default node style -tree.node:highlight: Highlighted node (keyboard focus) -tree.node:selected: Selected node with marker -tree.indicator: Expand/collapse indicators -tree.border: Border characters -tree.border:focus: Border when focused