Themes

A theme is one JSON document that sets how AFUI pages and native renderers look: colors, fonts, text styles, spacing, shapes, control sizes, and the layout of collections and forms. Every renderer reads the same resolved result, so a theme changes every surface without touching a panel or a renderer.

The model

A theme is a partial assignment: every field may be left out.

{
  "density": "compact",
  "colors": {"inputs": {"accent": "#7a3cff"}},
  "font": {"ui": "Inter, sans-serif"},
  "control": {"regular": {"height_pt": 40}},
  "shape": {"control": "rounded", "radius": {"control_pt": 6}}
}

Themes combine in one way only: overlay(lower, upper) takes every field upper writes over the same field of lower. It is associative, so a host can stack a workspace theme on a profile theme on the baseline in any grouping.

Theme::baseline() is the AFUI baseline, a theme with every field written. The baseline stylesheet’s :root values are generated from it.

Resolving a theme is overlay(density(baseline), theme) for one color scheme, with each group then given one of two states.

Groups and the platform

The parameters fall into six groups: text, space, shape, control, collection and form. After resolution each group is in one of two states:

  • platform: the theme wrote no field of the group and no density touches it. A native renderer draws the group with the system’s own controls and text styles; a page uses the AFUI baseline.
  • resolved: the theme wrote at least one field of the group, or it wrote a density (which resolves space and control). Every value of the group is given; a field the theme left out takes the baseline’s value, after density, never the platform’s.

There is no state between the two. An empty theme looks native on every platform; a full theme looks the same on every platform.

Two kinds of value sit outside the groups. Colors always resolve to every role. font.ui and font.mono are independent font family lists; an omitted one is the platform font and does not change any group’s state.

Density

density is compact, regular or comfortable. It scales the _pt lengths of space and control in the baseline by 0.85, 1 or 1.15 and rounds them to the nearest half point, before the theme’s own values apply. A theme that writes every field resolves the same at every density.

Units

A parameter’s name carries its unit. _pt is a length in logical points: one CSS pixel at the default text size, one native point. _ratio is a multiplier, _percent a percentage, _lines a line count; enumerations have no suffix. The JSON holds numbers only: there are no references, aliases or spacing scales. The one name that points elsewhere is control.<size>.text, which names a text role.

Diagnostics

Resolution reports contradictions that hold for every renderer, and keeps going:

  • a length below zero (negative_length);
  • a spacing or a control height that is zero after density (not_positive);
  • a control lower than one line of the text role it uses, when both text and control are resolved (control_shorter_than_text);
  • a control’s minimum width below twice its horizontal padding (min_width_below_padding);
  • a line count below one (lines_below_one);
  • the color diagnostics: unmet contrast, surface order, neutral tint and unknown pins (color).

The minimum hit area under a coarse pointer is a rule of the renderer contract, not a diagnostic: a renderer extends the hit area beyond what it draws, whatever the theme says.

Parameters

Each row is one parameter, with the CSS variable a page reads it from and its AFUI baseline. A page gets lengths in rem (the value over 16, so the browser’s text size setting still applies), except border.*, focus.* and elevation.*, which are pixels. A control’s text becomes the CSS font of its role, and shape.control becomes the corner radius it implies.

GroupParameterCSS variableUnitBaselineUse
independentfont.ui--afui-font-uifont family list-apple-systemInterface font family; omitted means the platform font
independentfont.mono--afui-font-monofont family listui-monospaceMonospaced font family; omitted means the platform font
texttext.scale_ratio--afui-text-scaleratio1Multiplies every text size
texttext.title.size_pt--afui-text-title-sizepoints28Page and view titles
texttext.title.weight--afui-text-title-weightregular medium semibold boldboldPage and view titles
texttext.title.line_height_ratio--afui-text-title-line-heightratio1.25Page and view titles
texttext.title2.size_pt--afui-text-title2-sizepoints22Second-level headings
texttext.title2.weight--afui-text-title2-weightregular medium semibold boldboldSecond-level headings
texttext.title2.line_height_ratio--afui-text-title2-line-heightratio1.25Second-level headings
texttext.title3.size_pt--afui-text-title3-sizepoints18Third-level headings and dialog titles
texttext.title3.weight--afui-text-title3-weightregular medium semibold boldsemiboldThird-level headings and dialog titles
texttext.title3.line_height_ratio--afui-text-title3-line-heightratio1.3Third-level headings and dialog titles
texttext.headline.size_pt--afui-text-headline-sizepoints15Entry titles, section titles and emphasized labels
texttext.headline.weight--afui-text-headline-weightregular medium semibold boldsemiboldEntry titles, section titles and emphasized labels
texttext.headline.line_height_ratio--afui-text-headline-line-heightratio1.5Entry titles, section titles and emphasized labels
texttext.body.size_pt--afui-text-body-sizepoints15Running text
texttext.body.weight--afui-text-body-weightregular medium semibold boldregularRunning text
texttext.body.line_height_ratio--afui-text-body-line-heightratio1.5Running text
texttext.callout.size_pt--afui-text-callout-sizepoints14Descriptions, hints and secondary lines
texttext.callout.weight--afui-text-callout-weightregular medium semibold boldregularDescriptions, hints and secondary lines
texttext.callout.line_height_ratio--afui-text-callout-line-heightratio1.5Descriptions, hints and secondary lines
texttext.caption.size_pt--afui-text-caption-sizepoints12.8Small annotations
texttext.caption.weight--afui-text-caption-weightregular medium semibold boldregularSmall annotations
texttext.caption.line_height_ratio--afui-text-caption-line-heightratio1.4Small annotations
texttext.label.size_pt--afui-text-label-sizepoints14.4Form field labels
texttext.label.weight--afui-text-label-weightregular medium semibold boldsemiboldForm field labels
texttext.label.line_height_ratio--afui-text-label-line-heightratio1.5Form field labels
texttext.mono.size_pt--afui-text-mono-sizepoints15Code and fixed-width values
texttext.mono.weight--afui-text-mono-weightregular medium semibold boldregularCode and fixed-width values
texttext.mono.line_height_ratio--afui-text-mono-line-heightratio1.5Code and fixed-width values
spacespace.page_pt--afui-space-pagepoints16Inset of a panel’s content from its edges
spacespace.segment_pt--afui-space-segmentpoints24Gap between the segments of a segmented panel
spacespace.section_pt--afui-space-sectionpoints16Gap between sections, and a section’s own inset
spacespace.field_pt--afui-space-fieldpoints12Gap between the fields of a form
spacespace.grid_pt--afui-space-gridpoints12Gap between and around the tiles of a grid
spacespace.row_pt--afui-space-rowpoints12Horizontal inset of a row’s content
spacespace.inline_pt--afui-space-inlinepoints8Gap between items on one line: an icon and its label, buttons in a group
spacespace.stack_pt--afui-space-stackpoints4Gap between closely related lines, such as a title and its description
spacespace.label_pt--afui-space-labelpoints6.4Gap between a field’s label and its control
spacespace.inset_pt--afui-space-insetpoints12Inset inside a small framed box: a callout, a code block, a menu
spacespace.page_max_width_pt--afui-space-page-max-widthpoints1056Widest a panel’s content column is drawn
shaperadius.control_pt--afui-radius-controlpoints8Corner radius of buttons and menu items when controls are rounded
shaperadius.field_pt--afui-radius-fieldpoints12Corner radius of text fields, pickers and choice cards
shaperadius.tile_pt--afui-radius-tilepoints12Corner radius of tiles and form sections
shaperadius.group_pt--afui-radius-grouppoints16Corner radius of cards, dialogs and the panel surface
shaperadius.icon_pt--afui-radius-iconpoints8Corner radius of icon tiles and media thumbnails
shapeshape.control--afui-shape-controlrounded capsulecapsuleOutline of buttons, chips and pickers: rounded or capsule
shapeborder.width_pt--afui-border-widthpoints1Width of borders and dividers
shapeborder.strong_width_pt--afui-border-strong-widthpoints3Width of emphasized borders and the selection indicator
shapefocus.width_pt--afui-focus-widthpoints3Width of the keyboard focus ring
shapefocus.offset_pt--afui-focus-offsetpoints2Gap between an element and its focus ring
shapeelevation.1.y_pt--afui-elevation-1-ypoints8Raised surfaces
shapeelevation.1.blur_pt--afui-elevation-1-blurpoints24Raised surfaces
shapeelevation.1.opacity_percent--afui-elevation-1-opacitypercent8Shadow opacity of ink; a dark scheme draws no shadow
shapeelevation.2.y_pt--afui-elevation-2-ypoints10Floating menus and popovers
shapeelevation.2.blur_pt--afui-elevation-2-blurpoints32Floating menus and popovers
shapeelevation.2.opacity_percent--afui-elevation-2-opacitypercent22Shadow opacity of ink; a dark scheme draws no shadow
controlcontrol.small.height_pt--afui-control-small-heightpoints36Height of a control of this size
controlcontrol.small.padding_x_pt--afui-control-small-padding-xpoints12.8Space between a control’s edge and its content
controlcontrol.small.min_width_pt--afui-control-small-min-widthpoints36Narrowest a control of this size is drawn
controlcontrol.small.icon_pt--afui-control-small-iconpoints16Icon size inside a control of this size
controlcontrol.small.text--afui-control-small-texttext roleheadlineText role of a control’s label
controlcontrol.regular.height_pt--afui-control-regular-heightpoints44Height of a control of this size
controlcontrol.regular.padding_x_pt--afui-control-regular-padding-xpoints16Space between a control’s edge and its content
controlcontrol.regular.min_width_pt--afui-control-regular-min-widthpoints44Narrowest a control of this size is drawn
controlcontrol.regular.icon_pt--afui-control-regular-iconpoints20Icon size inside a control of this size
controlcontrol.regular.text--afui-control-regular-texttext roleheadlineText role of a control’s label
controlcontrol.large.height_pt--afui-control-large-heightpoints52Height of a control of this size
controlcontrol.large.padding_x_pt--afui-control-large-padding-xpoints24Space between a control’s edge and its content
controlcontrol.large.min_width_pt--afui-control-large-min-widthpoints52Narrowest a control of this size is drawn
controlcontrol.large.icon_pt--afui-control-large-iconpoints24Icon size inside a control of this size
controlcontrol.large.text--afui-control-large-texttext roleheadlineText role of a control’s label
controlcontrol.number_width_pt--afui-control-number-widthpoints96Width of a number input
controlcontrol.disabled_opacity_percent--afui-control-disabled-opacitypercent55How opaque a control is drawn while it is out of service
controlcontrol.menu_min_width_pt--afui-control-menu-min-widthpoints176Narrowest a menu is drawn
controlcontrol.menu_max_width_pt--afui-control-menu-max-widthpoints352Widest a menu is drawn
collectioncollection.row_min_height_pt--afui-collection-row-min-heightpoints52Shortest a list row is drawn
collectioncollection.media_column_pt--afui-collection-media-columnpoints48Size of a row’s media
collectioncollection.icon_tile_pt--afui-collection-icon-tilepoints32Size of the icon of an icon tile
collectioncollection.tile_min_width_pt--afui-collection-tile-min-widthpoints192Narrowest a grid tile is drawn
collectioncollection.rail_pt--afui-collection-railpoints40Width of the column that holds an entry’s more button
collectioncollection.title_lines--afui-collection-titlelines2Lines a tile’s title takes before it is cut
collectioncollection.description_lines--afui-collection-descriptionlines2Lines a tile’s description takes before it is cut
collectioncollection.media_aspect_ratio--afui-collection-media-aspectratio1.3333Width over height of a tile’s picture
collectioncollection.avatar_pt--afui-collection-avatarpoints40Size of a standalone icon and an avatar picture
collectioncollection.media_min_height_pt--afui-collection-media-min-heightpoints144Shortest a video is drawn
collectioncollection.media_max_height_pt--afui-collection-media-max-heightpoints192Tallest a banner picture is drawn
collectioncollection.trailing_max_width_pt--afui-collection-trailing-max-widthpoints160Widest a row’s trailing status is drawn
collectioncollection.cell_max_width_pt--afui-collection-cell-max-widthpoints336Widest a table cell is drawn before its text wraps
collectioncollection.bar_height_pt--afui-collection-bar-heightpoints48Height of a panel’s toolbar
collectioncollection.bar_threshold_pt--afui-collection-bar-thresholdpoints544Toolbar width below which a button with an icon shows only the icon
collectioncollection.narrow_threshold_pt--afui-collection-narrow-thresholdpoints640Panel width below which tables, fact lists and buttons take their narrow layout
formform.label_min_width_pt--afui-form-label-min-widthpoints128Narrowest the label column of a wide form is drawn
formform.label_max_width_pt--afui-form-label-max-widthpoints224Widest the label column of a wide form is drawn
formform.label_width_percent--afui-form-label-widthpercent30Share of a wide form’s width the label column takes between its bounds
formform.wide_threshold_pt--afui-form-wide-thresholdpoints544Form width from which labels sit beside their controls
formform.dialog_width_pt--afui-form-dialog-widthpoints576Widest a dialog is drawn

The resolved theme

resolve_theme(&theme, scheme) returns a ResolvedTheme. Its JSON is the canonical exit, read by renderers that do not link this crate:

{
  "scheme": "light",
  "colors": {"opaque": {"page": "#f3f7fc"}, "overlays": {"fill": "#484b4f12"}},
  "font": {"ui": null, "mono": null},
  "groups": {
    "text": {"state": "platform"},
    "control": {"state": "resolved", "values": {"regular": {"height_pt": 44}}}
  },
  "diagnostics": []
}

Colors are #rrggbb and #rrggbbaa; overlays keep their alpha. A dark scheme resolves every elevation’s opacity to zero: dark surfaces draw no shadow. The vocabulary in tests/fixtures/view/theme-conformance/params.json lists every parameter with its JSON path, CSS variable and baseline.

ResolvedTheme::css_tokens() is the CSS projection: every color role, each font family that is set, and every parameter of every resolved group. A group left to the platform writes no variable, so a page falls back to its baseline. A host hands the projection to a page with afui.setTheme, which replaces the whole host theme each time it is called.

On the command line

afui theme css theme.json --output json | jq -r '.result.css' > theme.css
afui theme resolve theme.json --scheme dark --output json

theme css writes the light and dark variables of every resolved group with the diagnostics of both schemes; load its CSS after the baseline stylesheet. theme resolve writes the resolved theme. A file that is not JSON fails with theme_invalid_json, a field of the wrong structure or type with theme_invalid and its path, and an unreadable file with theme_unreadable. Diagnostics are not failures.

Choosing color roles

UseRoles
Page, raised panel, and distinct input or header surface--afui-page, --afui-panel, --afui-panel-strong
Body and secondary text--afui-ink, --afui-muted
Decorative separator, control border, and keyboard focus--afui-line, --afui-line-strong, --afui-focus
A solid fill with text or icons over it--afui-X with --afui-on-X
A tinted surface with semantic text, icons, or borders--afui-X-bg with --afui-X-ink
Hover and press--afui-fill, --afui-fill-strong
Selection and the current item within a selection--afui-accent-fill, --afui-accent-fill-strong

X is one of accent, info, warn, danger, or ok. Use --afui-X-ink for semantic foregrounds on neutral surfaces as well. State overlays stay translucent so the surface beneath remains visible. Use focus rings, labels, and other visible cues when interaction or status must remain identifiable without color.

Set a color identity in colors.inputs to generate coordinated roles for both schemes. A pin in colors.pins.light or colors.pins.dark fixes one role in one scheme; the other roles are solved with it in place. Use role_kind(name) to classify a pin name as RoleKind::Opaque or RoleKind::Overlay. Rust hosts can call theme::derive_color_family for a color supplied by application data.