Skip to main content

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

CoreSkin (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.

FieldTypeMaterial defaultWhat it's for
rowHeightDp40node row height
indentDp24per level
showLinesBooleanfalseguide lines (Carbon, Fluent)
chevronIconSourcechevron rightrotates when expanded
iconSizeDp20node icons
colorsTreeColors(selected, hover, line, text, chevron)M3 valuesevery color it uses
stringsTreeStringsEnglish"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​

  1. CamoTreeNode, flattening, TreeTheme / Style, state, slots, renderer, the CamoSkin field.
  2. CamoTree, the DebugSkin renderer.
  3. 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​

CaseDecided behavior
CaseDecided behavior
------
Deep treesOnly the visible rows compose (lazy).
A node removed while expandedIts key leaves the expanded set the next time the app sets it; stale keys are ignored.
Multi-selectSpace toggles; Shift+arrows extend a range (desktop).
RTLIndent and chevrons mirror; Left and Right swap.
Changed in the M10 build (2026-11-02)
  • The renderer has one part, Node, with TreeNodeState (level, expanded, hasChildren, loading, selected, interaction) and slots for the icon, the label and the chevron's toggle behavior (Kotlin a Modifier, Flutter a wrapToggle). TreeTheme has rowHeight, indent, showLines, chevron, iconSize, textStyle and TreeColors(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 CamoRovingFocusItem gained an onKey hook 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: collectionInfo on the list; per node stateDescription (Expanded or Collapsed), selected, and a collectionItemInfo with the level as the row span; expand / collapse custom actions for screen readers.
  • Keys through onPreviewKeyEvent on the list with a FocusRequester per visible row.

4. Build steps​

  1. Flatten, list, keys, selection, lazy load, the renderer.
  2. Tests: flatten, keys, semantics, lazy loading.

Done when

  • Works by touch, mouse, keyboard and screen reader on all targets.

5. Edge cases​

CaseDecided behavior
Focus on a collapsed childWhen the parent collapses, focus moves to the parent.
Changed in the M10 build (2026-11-02)
  • Rows are a LazyColumn over the flattened visible nodes (remembered on the roots and the expanded set); the tree keys use onPreviewKeyEvent before rovingFocusItem; Role.Button on the toggle.
  • None specific.