iced-cube

Data

Tree

Nested items that expand and collapse, such as folders and files, with selection, checkboxes and children that load later.

use iced_cube::data::tree::{self, Event, Mode, Output, State, loaded, node, tree};
API reference
File explorer: Folders that open and close, file sizes and a count badge, indentation guides, a disabled folder, a name too long to fit and a context menu on every node.File explorer: Folders that open and close, file sizes and a count badge, indentation guides, a disabled folder, a name too long to fit and a context menu on every node.
Static preview
File explorer. Folders that open and close, file sizes and a count badge, indentation guides, a disabled folder, a name too long to fit and a context menu on every node.

Usage

A tree shows data with a parent and child structure: folders and files, an outline, groups of permissions. The app owns a tree::State holding every node by id and routes the tree’s events into it. Nodes are plain data, so the app can read them back with state.node(id) when a node is activated or chosen from its menu.

Pressing a row highlights it, and in single or multiple mode selects it. In multiple mode Ctrl or Cmd adds or removes one node and Shift selects the range from the last one pressed. In checkbox mode every row has a checkbox: checking a parent checks its children, and a parent with only some children checked shows a dash. Disabled nodes can be seen but not selected, checked, expanded or activated, and a disabled child keeps its state when its parent changes.

A node made with .lazy() has children that are not known yet. Expanding it shows a loading line and returns Output::Load with its id. Fetch the children however you like, on a thread or a task, and send loaded(id, nodes) through the sender from tree::subscription(). The lazy loading example simulates a server that answers after a short wait.

The tree resolves its keys itself, and only while it has focus, which it takes when pressed and gives up on a press elsewhere. That keeps its arrow keys from clashing with tabs, sliders or anything else on the page. Letters and digits jump to the next node starting with them, and * expands the highlighted node and all of its siblings. When the tree has a fixed height it scrolls the highlighted row into view as the highlight moves.

Rows are 32 pixels tall, as in menus, and long labels end in an ellipsis rather than wrapping, so a narrow sidebar on a phone keeps every row the same height and easy to tap.

Examples

Checkboxes

Checking a group checks its members, and a group with some members checked shows a dash. The disabled permission keeps its state.

Checkboxes: Checking a group checks its members, and a group with some members checked shows a dash. The disabled permission keeps its state.Checkboxes: Checking a group checks its members, and a group with some members checked shows a dash. The disabled permission keeps its state.
Static preview

Lazy loading

Folders fetch their children when first opened. A simulated server sends them through the tree's channel after a short wait, using futures-timer so it also runs in the browser.

Lazy loading: Folders fetch their children when first opened. A simulated server sends them through the tree's channel after a short wait, using futures-timer so it also runs in the browser.Lazy loading: Folders fetch their children when first opened. A simulated server sends them through the tree's channel after a short wait, using futures-timer so it also runs in the browser.
Static preview

Keyboard shortcuts

A multiple selection driven by the default keymap with J and K added. The tree resolves keys only while it has focus.

Keyboard shortcuts: A multiple selection driven by the default keymap with J and K added. The tree resolves keys only while it has focus.Keyboard shortcuts: A multiple selection driven by the default keymap with J and K added. The tree resolves keys only while it has focus.
Static preview

API

ItemDescription
State::new(nodes)Holds the nodes, keyed by id, all collapsed. Ids must be unique across the whole tree.
node(id, label)A leaf. Add .children(nodes), .lazy() for children that load later, .folder() or .file() for the usual icons, .icon(glyph) or .icons(closed, open), .badge(text) or .trailing(text) at the end of the row, and .disabled(true).
.with_mode(Mode) / .with_expanded(ids)Mode::Single (the default), Multiple, Checkbox or None, and the nodes to start expanded.
state.update(Event)Handles presses, expanding, keyboard navigation, checkboxes and arriving children. Returns Output::Load(ids) when lazy nodes need their children, Activated(id), Selected(ids) or Checked(ids).
state.selected() / state.checked() / state.check_state(id)The selected or checked nodes in tree order, and whether a node's box is checked, unchecked or indeterminate.
state.visible() / state.highlighted() / state.is_expanded(id)The nodes on screen in order, the node the keyboard acts on, and whether a node is open.
state.node(id) / state.children(id) / state.parent(id)Reads the nodes and their structure.
tree::subscription() / tree::Sender / loaded(parent, children)The channel lazy children arrive on. It sends Event::Ready(sender) first; producers send loaded(parent, nodes) through clones of the sender, and batches arrive as Event::Received.
state.set_loading(id)Shows a node as loading, such as while the app refreshes it.
tree(&state)Renders the rows. Rows render disabled until .on_event is set.
.guides(bool)Draws a vertical line at each level of indentation.
.width(length) / .height(length)A fixed or filling height scrolls the rows. Only the rows in view are built.
.context_menu(&menu, f)Gives every node the same context menu, keyed by node id, opened by a right-click, a long press or Shift+F10 on the highlighted node. The node it opened on carries the ring while it is open.
.keymap(keymap) / .context_menu_keymap(keymap)Replaces the tree's or the context menu's default keymap.
tree::default_keymap() / action.event(&state)The default shortcuts, and the Event a resolved Action sends. State::key_event does both, with typeahead.
tree::row_style(tokens, Status)The row colours, taken from the shared menu rows: selected rows use the accent, the keyboard highlight a ring.

Keyboard

Default shortcuts. Every one can be changed or removed with a keymap; see Keyboard shortcuts.

KeysAction
ArrowDownNext Highlights the next visible node.
ArrowUpPrevious Highlights the previous visible node.
HomeFirst Highlights the first node.
EndLast Highlights the last visible node.
ArrowRightExpand Expands the highlighted node, or moves to its first child when it is open.
ArrowLeftCollapse Collapses the highlighted node, or moves to its parent when it is closed.
EnterActivate Activates the highlighted node.
SpaceSelect Selects, toggles or checks the highlighted node.
*ExpandSiblings Expands the highlighted node and all of its siblings.
Shift+ArrowDownExtendNext Moves down and extends the selection, in a multiple selection.
Shift+ArrowUpExtendPrevious Moves up and extends the selection, in a multiple selection.
Ctrl+ASelectAll Selects every visible node, in a multiple selection.
Esc