CamoSegmentedControl
Concept
1. Why
A segmented control switches between 2–5 closely related options in a compact row: "Day / Week / Month", "List / Grid", or a filter set. Material calls it segmented buttons, and Apple segmented control. It supports single selection (like a compact radio group) and multi-selection (like a row of toggle buttons).
2. Shape
Material mapping preview: M3 segmented buttons.
- Height 40, with an
outlineborder andshapes.fullends. - The selected segment is
secondaryContainer/onSecondaryContainer, with a check icon (for single selection only when there's no icon).
3. API
Contract
fun <T> CamoSegmentedControl(
options: List<Segment<T>>,
selected: T, // single selection
onSelectionChange: (T) -> Unit,
enabled: Boolean = true,
)
data class Segment<T>(val value: T, val label: String? = null, val icon: Widget? = null,
val contentDescription: String? = null, val enabled: Boolean = true)
A segment needs a label or an icon. Icon-only segments need a contentDescription.
Building blocks (ADR-30): CamoSegmentedContainer(content: Slot) (the track, outer shape and radio-group semantics) and CamoSegmentButton(segment, selected, onClick, position) (one skin-styled segment with its radio semantics). They let a design put custom content in segments (an icon with a count, two-line segments) without losing the look or keyboard behavior.
State → renderer (M5 build): the renderer has two parts, matching the building blocks: the container (SegmentedContainerState(enabled, count), the track around the segments in a row) and each segment (SegmentState(selected, enabled, position: ItemPosition, interaction) with SegmentSlots(label?, icon?, content?)). Core gives the segments equal widths.
Tokens used
colors.outline,secondaryContainer,onSecondaryContainer,onSurfaceshapes.full,typography.labelLargemotion.durationShort(selection change; the Apple dialect slides its thumb)
Component theme
theme.components.segmentedControl: SegmentedControlTheme?. 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 |
|---|---|---|---|
height | Dp | 40 | control height |
shape | Shape | shapes.full | outer corners |
borderWidth | Dp | 1 | outline and dividers |
textStyle | CamoTextStyle | labelLarge | segment labels |
iconSize | Dp | 18 | segment icons |
showCheckIcon | Boolean | true | Material's check mark on selected segments |
colors | SegmentedColors(container, border, selectedContainer, selectedContent, content) | transparent, outline, secondaryContainer, onSecondaryContainer, onSurface | every color it uses; container is the track (Minimal's surfaceContainer, added in the M5 build) |
Accessibility
- Single selection: a radio group, with each segment a radio ("Week, 2 of 3, selected"). Arrow keys move and select.
- Each segment has a 48 minimum touch target, and the control grows vertically if needed.
4. Build steps
Segment,CamoSegmentedControl, and the selection logic.SegmentedControlRenderer, and the DebugSkin renderer.- Tests:
- a tap selects one;
- arrow keys move and select;
- the announcements;
- an icon-only segment without a description fails a debug assertion.
Done when
- "Day / Week / Month" works by touch, keyboard and screen reader.
5. Edge cases
| Case | Decided behavior |
|---|---|
| More than 5 segments, or long labels | Guidance: use CamoTabs or CamoSelect. Labels ellipsize after one line, and every segment takes an equal width. |
| Multi-selection | Not a segmented control (ADR-23): Apple's iOS segmented control and Carbon's content switcher are single-select only. Use a row of filter chips, CamoChip(selected = …), which every dialect draws. |
| Font scale 200% | The height grows. The labels still get one line with an ellipsis, and the full label is in semantics. |
| RTL | The segment order mirrors. |
| Custom segments | CamoSegmentedContainer + CamoSegmentButton (ADR-30). The container keeps arrow-key navigation across its buttons. |
Implementation
1. Why
A single-choice row of equal segments (ADR-23 removed multi-select). In Compose it's a Row of selectable segments with equal widths, the roving focus from CamoRadio, and public building blocks for custom segments (ADR-30).
2. Shape
3. API
@Composable
public fun <T> CamoSegmentedControl(
options: List<Segment<T>>,
selected: T,
onSelectionChange: (T) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
)
// building blocks (ADR-30)
@Composable public fun CamoSegmentedContainer(modifier: Modifier = Modifier, content: @Composable RowScope.() -> Unit)
@Composable public fun <T> RowScope.CamoSegmentButton(
segment: Segment<T>, selected: Boolean, onClick: () -> Unit, position: ItemPosition,
modifier: Modifier = Modifier, content: (@Composable () -> Unit)? = null, // null → the segment's label/icon
)
- Equal widths: each
CamoSegmentButtonappliesModifier.weight(1f); the container usesModifier.height(IntrinsicSize.Min)so dividers and the selected fill span the full height. - Selection animation: the renderer draws the selected background as one moving element (
animateDpAsStateover the measured segment offsets), so skins like Cupertino get the sliding thumb; Minimal fades instead. - Semantics: container
selectableGroup(); each buttonselectable(selected, role = Role.RadioButton)withItemPosition(index, count)exposed throughcollectionItemInfo. - Keyboard: the container installs the roving focus helper (one Tab stop, arrows move and select).
4. Build steps
Segment,SegmentedControlTheme/Style, the building blocks, thenCamoSegmentedControlbuilt from them.- The DebugSkin renderer (bordered boxes, the selected one filled).
commonTest: equal widths with uneven labels; tap selects; arrows select; icon-only segment withoutcontentDescriptionasserts; a custom-content segment keeps radio semantics.
Done when
- "Day / Week / Month" and a custom segment row (icon + count) work by touch, keyboard and screen reader.
5. Edge cases
| Case | Decided behavior |
|---|---|
| Labels too long for equal widths | One line with an ellipsis; the full label stays in semantics. |
| Font scale 200% | The row grows in height (intrinsic measurement), labels still single-line. |
selected not in options | Nothing selected, debug log. |