Skip to content
  1. Docs
  2. enzUI framework
  3. Developers

Styling and tokens

How EnzTheme scales, semantic roles and per-component token snapshots decide what a widget looks like.

Widgets derive their appearance from the runtime iced::Theme on every render. A theme swap restyles everything without a restart. Appearance comes from three layers, broadest first:

  1. The user's theme file, shared by every enzUI app. See Theme file.
  2. EnzTheme in code: palette, spacing, radius, sizing, typography, stroke, elevation, opacity and motion scales.
  3. Component tokens: a per-widget .tokens(..) snapshot or builder override.

EnzTheme

EnzTheme holds the shared scales. Supply theme.iced_theme() through the application's theme callback; ThemeIntegration does this for you from the theme file. EnzTheme::dark() and EnzTheme::light() are the two modes of the built-in enzUI Nebula family. Stock iced themes still work: they provide colours and use the framework's default scales.

Choosing a token

MeaningConfiguration
Shared semantic colourEnzTheme::palette; use the role for the intended state and part
Padding or sibling gapSpacingScale / SpaceToken, resolved against the configured scale
Surface cornerRadiusScale and its rungs (xs, sm, md, lg), where the widget follows that scale
Separator or thin outlineStrokeScale::hairline / StrokeToken::Hairline
Keyboard focus ring widthStrokeScale::focus_ring / StrokeToken::FocusRing
Control or window extentThe matching SizingTokens role, where documented
Text hierarchyTypographyScale / TextSize
Component-specific paint or geometryThe component's tokens or an explicit builder override
Measurement, time, protocol or algorithm constantKeep it with its owning API; it is not an appearance token

Reuse a role because its meaning matches, not because its default number does. An equal default does not make a heading size right for a control label, or a surface corner right for a circular indicator.

rust
use enzui_theme::SpaceToken;

enz_vstack(vec![banner, status_row])
    .gap(SpaceToken::Sm)
    .into_element()

Component tokens

Every component with configurable paint has a *Tokens struct: 86 of them, with 859 public fields. The repository's docs/tokens.md lists each struct and field. Field names follow the component's anatomy:

text
component -> variant/part -> property -> interaction state -> exceptional unit
  • Parts: container / background, content / foreground, text, icon, border, track, thumb, focus_ring.
  • States: rest (implicit), hover, pressed, selected, disabled.
  • Geometry: padding inside an edge, gap between siblings, inset inside an edge, outset beyond one, and width, height, radius.
  • Tones: error (validation), danger (intrinsic risk) and destructive (an action) are not interchangeable.
  • Units: pixels are implicit; suffixes mark other units (_ms, _fraction, _alpha).

When to override

You want toTechniqueOn a theme switch
Use the framework defaultOmit .tokens()Follows the theme
Use a named preset.tokens(T::light())Snapshot: set it again
Derive from the current palette.tokens(T::from_palette(p))Snapshot: stale until re-derived
Change one fieldT { field: val, ..T::from_palette(p) }Snapshot, as above
Hard-code everythingA full struct literalNever changes

Prefer omitting .tokens(). A .tokens(..) call pins a snapshot, so rebuild it from the current theme when the theme changes. A from_palette constructor sees colours only; it cannot inherit spacing, rounding or typography from an EnzTheme.

rust
use enzui_theme::EnzTheme;
use enzui_widgets::checkbox::{CheckboxTokens, enz_checkbox};

let theme = EnzTheme::dark();
let tokens = CheckboxTokens {
    radius: Some(3.0),
    border_width: None,
    focus_ring_width: 2.5,
    disabled_alpha: 0.5,
    ..CheckboxTokens::from_palette(&theme.palette)
};
let checkbox = enz_checkbox(false, "Remember me", |_| ()).tokens(tokens);

Geometry helpers have their own setters, such as EnzBadge::size_tokens(BadgeSizeTokens::from_size(..)) or EnzColorPicker::size_tokens(..). The documented geometry tokens replace invalid values (NaN, infinities, negatives where a positive is required) with their defaults rather than breaking layout; each struct's documentation says which values it accepts.

Global scales and their limits

Global scales apply where a component documents a consumer; they do not rescale every preset. sizing.control_height is not the five-step button-height ladder, and sizing.row_height is read-only in theme files because built-in lists and tables use their own row metrics.

Disabled opacity

EnzTheme::opacity (OpacityScale) holds two 0–1 roles that multiply the alpha of what they fade:

RoleDefaultUsed by
disabled0.62Disabled controls: text, secure and search fields, combo box, autocomplete, stepper, and the default fade of checkbox, radio, toggle, switch and slider
disabled_surface0.55Disabled surfaces faded as a whole: clickable, pressable, list rows, swatches, a selected segment

Set them with EnzTheme::with_opacity(OpacityScale { .. }). A component token you pinned keeps its own value. Disabled placeholders use the contrast-audited disabled_text_ink, so they stay legible at any setting.

Meter tracks and switch knobs

Sliders, progress bars and gauges paint their unfilled track in Palette::meter_track, a soft groove with no outline. Switch knobs are light in both modes, and each track is raised to 3:1 contrast against the knob. Set track, thumb_border, track_off and the other component tokens to override these.

Drag feedback

DragFeedbackTokens sets how strongly a drag shows (item and area radius, highlight border width, fill alphas, the lifted row's elevation). Pass the same value to EnzDragDropHost::feedback_tokens(..) and each hosted EnzReorderList::feedback_tokens(..) to keep them consistent.

Following the desktop

An app on ThemeIntegration follows two desktop colour settings with no code:

  • Accent. The desktop's accent colour (the Settings portal's accent-color) replaces the built-in accent, unless the theme file sets palette.accent.
  • Contrast. When the desktop asks for more contrast, every palette becomes Palette::high_contrast(): surfaces take the page colour and are told apart by edges, and the accent, state colours and focus ring reach 7:1 against the page. Glass turns opaque and grain disappears. A palette built in code can call Palette::high_contrast() itself for an in-app setting.

Text size and reduced motion follow the desktop too; see Theme file and Motion.

Elevation

enz_elevation takes a semantic level from 0 to 5 and resolves the shadow from the theme, so two widgets at the same level match. Hand-written iced::Shadow values do not. Filled buttons are flat by default; the theme file's elevation.button or enz_button(..).elevation(level) lifts them.

Text grids on their own background

EnzTextGrid tones resolve from the application palette, which assumes the page behind them. A grid painted on another colour, such as a terminal's scheme background, declares it with .canvas(color), and each tone is raised to the 4.5:1 text floor against that background while keeping its hue.