CamoTree
Concept
1. Why
A tree shows hierarchical data you can expand and collapse: a file explorer, a settings outline, an org chart (Fluent Tree, Carbon's tree view, macOS outline views). Nodes may load their children on demand. It is not navigation (that is the drawer, whose groups are one level) and not an accordion (independent sections).
2. Shape
Split of duties
| Core | Skin (renderer) |
|---|---|
the node model, flattening the expanded nodes into a lazy list, expansion and selection (app-owned sets), lazy children (onExpand), keyboard (Up, Down, Left, Right, Home, End, typing), semantics (tree, level, expanded) | indent, the chevron, icons, the row look, the selected and focus states, lines between levels |
Material mapping preview: the M3 has no tree; rows follow list items with a chevron.
- Row 40 tall, indent 24 per level, selected
secondaryContainer.
3. API
Contract
data class CamoTreeNode<T>(
val key: Any,
val value: T,
val children: List<CamoTreeNode<T>>? = null, // null: a leaf; empty: loaded, nothing in it
val hasChildren: Boolean = children != null, // true with null children: not loaded yet
)
fun <T> CamoTree(
roots: List<CamoTreeNode<T>>,
expanded: Set<Any>, onExpandedChange: (Set<Any>) -> Unit,
selected: Set<Any>, onSelectedChange: (Set<Any>) -> Unit,
multiSelect: Boolean = false,
onLoadChildren: ((CamoTreeNode<T>) -> Unit)? = null, // called once when a not-yet-loaded node expands
label: (T) -> String,
icon: ((T, expanded: Boolean) -> Slot)? = null,
caption: String,
)
State → renderer: TreeNodeState(level, expanded, hasChildren, loading, selected, interaction). Slots: TreeNodeSlots(icon, label, trailing). The renderer draws one row; core does the indentation inputs (level) and the list.
Tokens used
colors.surface,secondaryContainer(selected),onSurface,onSurfaceVariant(chevron)typography.bodyMedium;spacing.s,spacing.m
Component theme
theme.components.tree. Every field is nullable in the theme. The skin's defaults(theme) fills whatever isn't set (Material defaults shown). See Theme §3.10.
| Field | Type | Material default | What it's for |
|---|---|---|---|
rowHeight | Dp | 40 | node row height |
indent | Dp | 24 | per level |
showLines | Boolean | false | guide lines (Carbon, Fluent) |
chevron | IconSource | chevron right | rotates when expanded |
iconSize | Dp | 20 | node icons |
colors | TreeColors(selected, hover, line, text, chevron) | M3 values | every color it uses |
strings | TreeStrings | English | "expanded", "collapsed", "loading" |
Accessibility
- A tree with items announcing their level and expanded or collapsed ("Documents, collapsed, level 1, 2 of 5").
- One Tab stop; Down and Up move, Right expands (or enters the first child), Left collapses (or goes to the parent), Home and End jump, typing jumps to the next label, Enter or Space selects.
- A loading node announces "loading".
4. Build steps
CamoTreeNode, flattening,TreeTheme/Style, state, slots, renderer, theCamoSkinfield.CamoTree, the DebugSkin renderer.- Tests: flattening and levels, expand and collapse, lazy loading once, single and multi select, every key, semantics.
Done when
- The admin sample's file tree expands lazily and works by touch, keyboard and screen reader.
5. Edge cases
| Case | Decided behavior |
|---|---|
| Case | Decided behavior |
| --- | --- |
| Deep trees | Only the visible rows compose (lazy). |
| A node removed while expanded | Its key leaves the expanded set the next time the app sets it; stale keys are ignored. |
| Multi-select | Space toggles; Shift+arrows extend a range (desktop). |
| RTL | Indent and chevrons mirror; Left and Right swap. |
- The renderer has one part,
Node, withTreeNodeState(level, expanded, hasChildren, loading, selected, interaction) and slots for the icon, the label and the chevron's toggle behavior (Kotlin aModifier, Flutter awrapToggle).TreeThemehasrowHeight,indent,showLines,chevron,iconSize,textStyleandTreeColors(selected, hover, line, text, chevron); built-in strings are not built. - Keys: Right expands, then enters the first child; Left collapses, then goes to the parent; Home and End jump; Enter and Space select; Up and Down move. Left and Right are handled before the group's arrow keys (Flutter's
CamoRovingFocusItemgained anonKeyhook for this). Not built: type-ahead, Shift+arrow ranges. - A node whose children aren't loaded shows a spinner when it expands and asks for them once (
onLoadChildren). Nodes announce "Expanded" or "Collapsed" with their level, and offer expand and collapse actions to screen readers. The tree needs a height (lazy rows).
- Drag and drop to move nodes is v0.2.
- Checkbox trees (tri-state parents) are not in v0.1; say if you want them.
Implementation
1. Why
LazyColumn over the flattened visible nodes, key = node.key; the flatten is remembered on roots and expanded.
2. Shape
3. API
@Composable public fun <T> CamoTree(roots: List<CamoTreeNode<T>>, expanded: Set<Any>, onExpandedChange: (Set<Any>) -> Unit,
selected: Set<Any>, onSelectedChange: (Set<Any>) -> Unit, label: (T) -> String, caption: String,
modifier: Modifier = Modifier, multiSelect: Boolean = false, onLoadChildren: ((CamoTreeNode<T>) -> Unit)? = null,
icon: ((T, Boolean) -> (@Composable () -> Unit))? = null)
- Semantics:
collectionInfoon the list; per nodestateDescription(Expanded or Collapsed),selected, and acollectionItemInfowith the level as the row span;expand/collapsecustom actions for screen readers. - Keys through
onPreviewKeyEventon the list with aFocusRequesterper visible row.
4. Build steps
- Flatten, list, keys, selection, lazy load, the renderer.
- Tests: flatten, keys, semantics, lazy loading.
Done when
- Works by touch, mouse, keyboard and screen reader on all targets.
5. Edge cases
| Case | Decided behavior |
|---|---|
| Focus on a collapsed child | When the parent collapses, focus moves to the parent. |
- Rows are a
LazyColumnover the flattened visible nodes (remembered on the roots and the expanded set); the tree keys useonPreviewKeyEventbeforerovingFocusItem;Role.Buttonon the toggle.
- None specific.