Surfaces — spec¶
Status: built. Every decision here was made on the map
Map: surfaces — card-style theming,
and each section links the ticket that holds its reasoning. The vocabulary is in
CONTEXT.md § Surfaces.
The model¶
A surface is how a component's edge and body are drawn. It is data that the pane interprets, written in colour roles. Any palette can be drawn with any surface.
The user picks two things independently: a palette (colours) and a surface (shape). The shell combines them, with the glyphs, into the Theme that components are built from.
Five surfaces ship:
| Surface | Look | Needs |
|---|---|---|
Outlined |
a line border, no fill. Today's look, byte-identical | nothing |
Filled |
a solid card whose half-block rows (▄/▀) are its own edge |
256 colours, block glyphs |
Banded |
a card under a header band carrying the title; focus colours the band | 256 colours |
Elevated |
a card with a half-block drop shadow (▌/▀) right and below; accent stripe on focus |
256 colours, block glyphs |
Accented |
unfilled, with a ▌ bar down the left edge, accent-coloured when focused |
block glyphs (else ┃) |
Ruled, Lipped (eighth-block lips) and Bare were considered and don't ship; they are what custom surfaces look like (Which built-in surfaces ship).
Build order¶
Each step ships on its own and leaves the library working.
ansi.Filland the selection fix. Today's selection holes are fixed under Outlined before any surface exists. See Selection.- Outlined goldens. Capture every launcher example's rendered frame from today's output. From here on, Outlined must match them byte for byte. See Enforcement.
pkg/surfaceandpkg/paletteas leaf packages, with derivation, quantisation and resolution unit-tested with no UI.- The pane takes a resolved surface: edges, text rows, dividers, the fill
pass, inset-aware hit-testing. The ~12 hard-coded
width-2/Y+1sites move toContentRect. The pane draws the header divider, so the 8 copies ofglyphs.Rulego. layout.ZStackcomposites by painted cells.- The app shell: two axes, the body padding, the screen pass, bars on the surface's role, fallback logging, config keys.
- Migration of the library's examples and the six downstream modules, with a "Migrating to surfaces" section in this doc's successor guide.
- The surface demo:
examples/appearance/look(wasshell/chrome), a live surface picker that defines two custom surfaces, Ruled and Lipped, as struct literals offered beside the built-ins.
Packages¶
Imports point one way: pane → surface, palette → surface, and
theme → palette, surface, pane, …. surface imports nothing from tuilib, and palette only surface.
pkg/surface(leaf):Surface,Role, the five built-ins,Derive,Resolve, and the resolved type. All colour maths lives here.pkg/palette(leaf, importssurface):Palette, the 19 named palettes (palette.Dark(),palette.FromBase16,palette.Terminal(),palette.All()),ByName. Picking the starting palette and surface from env and config lives in the app shell, which already readspkg/config, sopalettedepends on nothing butsurface.pkg/themekeepsTheme= palette + surface + glyphs, and theth.Component()builders, which need all three. It is not renamed.
(Where the surface choice lives, The surface seam)
A surface is data¶
A Surface is a struct literal, the way lipgloss.Border is. There is no
Draw interface, so a custom surface can't get the geometry wrong. It declares:
- Name, for config matching and pickers.
- Edges per side: glyphs, the cells each side takes, and where each glyph's
background comes from: the card (the
Fillrole), or the parent (whatever contains the surface; theParentrole). Textual's "reversed" placement needs no third value: a half-block lip is a glyph inFillonParent, which is what makes half-block edges expressible. - Text rows: every surface has a top and a bottom text row, so all five slots (title, top-right, bottom-left, bottom-middle, bottom-right) exist on every surface. The surface decides how each row looks: a line with text inlaid, a band, or a card-tone row inside a lip or shadow. Elevated's bottom edge is two rows: footer, then shadow. Slot brackets are text-row data.
- The divider under a pinned header: glyph, role, the role while the header has input, and which background it sits on.
- Focus effects for two states, focused and blurred: edge role, fill role, band role, whether the accent bar shows. Edge space never differs between states, so Accented reserves its bar column when blurred.
- Selection treatment (see Selection).
- Bar role: which role the breadcrumb and statusbar sit on.
- Overlay form: another surface value for modals (see Overlays).
- Field form: another surface value for single-line components — input,
filter, toggle — which screens place in three-row slots, so it takes at
most one row of edge above and below and one cell on the left; the right
may take a margin besides. Nil derives it: a lined surface is its own
field, and anything else keeps its inner rings, as many as fit one cell per
side counted from the innermost out. A card keeps its text rows and focus
effects and drops the lips and shadow, so a field reads as a small card in
the tone of the panes around it: Filled's field is its card rows, Banded's
its band and card (the band colours on focus), Elevated's its card with the
accent stripe. Accented's is its bar.
A margined surface's field keeps a margin of its own. One cell on the
right lines its card up with the cards around it, which end a cell short
of their rect. Below, the card's bottom text row moves onto the parent and
becomes the margin, still carrying the bottom slots (a filter's counts), so
the card is its title and content rows: two fields stacked in a column
keep a row of screen between them instead of running into one card.
A surface whose inner rings carry no text rows draws an outlined field. A
surface that falls back draws its fallback's field form. Theme builders
hand
Input(),Filter()andToggle()this form. A field drawn on an overlay takesResolved.OverlayField(): the field form filled withOverlayCard/OverlayCardRaised, a step lighter than the overlay, as a card is a step lighter than the screen. In the field form's ownCardtones it would sink below the overlay, and a focused field would vanish into it. A lined or unfilled field keeps its form. A form's wells take the same two tones (theme.Form()setsStyles.WellandWellFocused), drawn as a background behind the value rather than as a card per field. - Needs and fallback: minimum colour depth, whether it uses block glyphs, and
the
Fallbacksurface (see Degraded terminals).
Colours appear only as roles, never as literals.
Geometry¶
- A surface draws only inside its rect. The rect
layouthands out is where the surface draws, and content is the rect minus the edges. Elevated's shadow is edge, drawn on the parent. - Margins: a filled surface's edge includes one cell on its right and bottom,
drawn with no background, so whatever contains it paints it: an outer pane's
fill pass, or at the top level the shell's screen pass. Adjacent cards end up
exactly one cell apart. Elevated's shadow is its margin. Filled keeps a
right margin only: its lips are its vertical margin, since the lower half of
one card's
▀and the upper half of the next card's▄leave exactly one row of screen between them. Outlined has none. - The shell pads the body one column on the left when the current surface
has a right margin (
Surface.Margined), so the screen's left edge matches the gaps between cards; and one row under the breadcrumb when the surface's top edge starts with a solid row (Surface.TopGap: Banded, Elevated, Accented), which would otherwise sit jammed against it. A top edge that is already a lip or a line (Filled, Outlined) brings its own gap.layoutknows nothing about any of this. - Hit-testing:
pane.RowAt, scrollbar hits and slot clicks derive from the surface's per-side edge space. A click on a margin or shadow cell is in the rect but outside the card, so the component declines it (rule 28).
(The surface seam, Gutters between surfaces)
Colour roles¶
A palette gains two seeds: Background (the palette's canonical background:
base16 base00, Catppuccin base, nord0) and Foreground. Built-in palettes
set them from upstream. Unset, they fall back to BarBG and BarFG.
Every role is derived from the palette, and any role can be overridden
per palette with Palette.Surface map[surface.Role]lipgloss.TerminalColor, where
a missing key means derive.
"Lift" raises OKLab lightness toward the Foreground's while keeping the
background's own hue and chroma — tonal elevation, as in Material's dark
surfaces. Mixing toward the text colour instead dragged its tint in: Gruvbox's
neutral screen grew beige cards, and Everforest's tones each landed on a
different hue. On light palettes, cards lift toward white.
| Role | Used for | Derived (dark) | Derived (light) |
|---|---|---|---|
Screen |
behind and between surfaces | Background |
Background, a step darker (−0.05 lightness) so near-white cards stand off it |
Card |
surface fill | lift 3.5% | toward white 70% |
CardRaised |
focused fill | lift 6.5% | toward white 90% |
Band |
Banded's header, Filled's divider step | lift 10% | darken Card 6% |
Overlay |
overlay-form fill | lift 17% | white |
OverlayCard / OverlayCardRaised |
a field on an overlay, blurred / focused | lift 23% / 28% | Card / CardRaised — nothing is lighter than a white overlay, so a field steps back into the sheet |
Button |
a button drawn as a chip: a toggle's side, a modal's choice, a form's submit (the chosen one takes Accent) |
lift 38% | darken Card 10% — a grey chip |
Shadow |
Elevated's shadow | darken Screen 60% |
darken Screen 22% |
Selection |
focused selected row | Card mixed 22% toward EdgeActive |
45% |
SelectionDim |
blurred selected row | lift Card 12% |
same |
Bar |
bars under Outlined | BarBG |
same |
Text / TextMuted |
body text on filled surfaces | Foreground / Muted |
same |
Edge / EdgeActive |
lines, the active band | BorderInactive (kept clear of CardRaised) / BorderActive |
same |
Divider / Accent |
rules, separators, placeholders / emphasis | Subtle (kept clear of CardRaised) / Accent |
same |
The percentages are starting points to tune, not spec.
- Contrast floor: a derived tone too close in lightness to the role beneath
it is pushed further:
CardonScreen,CardRaisedonCard,BandonCard,SelectiononCard, andOverlayonCardRaised— wider, 0.05, on a dark palette, since a modal opens over a focused card and one tone step above it reads as the same surface.OverlayCardis kept clear ofOverlay, andOverlayCardRaisedofOverlayCard. - Elevation only rises. On a dark palette a card, a focused card, a band and a modal are each pushed up from what they sit on when they fall inside the floor, never back down — at truecolor and after rounding to xterm-256. Pushing whichever way a tone leaned once landed Solarized's focused card exactly on its screen colour: its card had been pushed above the focused card's lift, and the focused card was then pushed back past it.
- Minimum span. Dark lifts are fractions of the span from background to text, but at least 0.55 of it. A deliberately low-contrast palette — Solarized's text is a mid-grey, a span of 0.39 — would otherwise get cards that barely rise.
- Chrome floor:
DividerandEdgekeep at least 0.12 lightness from a focused card. The palette choseSubtleandBorderInactiveto sit on its own background, and a card lifts away from it: on a dark palette the rules, separators and placeholders inside a card would otherwise vanish into it (Nord's and Dracula's sat 0.01–0.02 from a focused card). On a filled surface the theme'sSubtleis theDividertone, so everything drawn with it inside a card follows. - Selection on dark palettes is a state layer, a 22% tint of the accent over the card, rather than the 45% mix light palettes use: on a dark card the stronger mix made a bright, saturated block that outshone everything around it.
- Vibrance: before the contrast floor, text colours' chroma is boosted in
their own hue —
Accent×1.3,TextMuted×1.2,Text×1.15 — as far as the RGB gamut allows without shifting hue. A card's tone sits between the palette's background and text, which mutes what is drawn on it; the boost gives the colour back. - Text contrast floor: on every card tone, body text (
Text, and the theme'sCurrentemphasis) keeps at least a 14:1 WCAG contrast ratio, secondary text (TextMuted, the theme'sMuted) at least 8:1, andAccentat least 7:1 (further would wash out its colour). A colour is moved in its own hue first; when the hue runs out — a tinted colour can't get as bright as white — it gives up tint, blending toward white or black only as far as the floor needs. Past pure white there is nowhere left: Nord's text tops out at 10.2:1 on a focused card, Dracula's at 11.8:1, Gruvbox's at 12.3:1. Each is moved in lightness, in its own hue, against whichever card is worst for it. Dark palettes need it because every lift moves a card toward its text: Nord's text went from 9.2:1 on the screen to 6.8:1 on a focused card, and Dracula's secondary text sat at 2.1:1. Light palettes need it because pale accents sink into near-white cards: Catppuccin Latte's accent was 2.5:1. On a filled surface the theme'sMuted,AccentandCurrentare the floored colours, since components draw labels, headers and titles with them directly.surface.Contrastandsurface.EnsureContrastare the helpers. - Text: filled surfaces paint body text in
Text, and the fill pass re-opens the foreground as well as the background. Outlined keeps the terminal's own colours. - Pipeline:
surface.Derive(seeds, overrides, profile)returns the full role-to-colour map;surface.Resolve(s, colours)returns a resolved surface with concrete colours. Theme builders do both. The pane only ever sees resolved surfaces. - Expected built-in override: Dark's
Selection(itsEdgeActiveis ANSI bright blue, so the mix comes out saturated).
(Surface colour tokens, Bars on filled surfaces)
The fill pass¶
The pane fills once, in pane.View, over its own rendered lines:
- open the surface background (and
Textforeground) at the start of each line; - re-open it after every SGR that clears it (
0, empty,49, and the foreground equivalents), found with a real parameter walk that skips the operands of38/48/58(a string replace corrupts38;2;R;0;B); - pad every line to the pane width.
It is ansi.Fill(s, open), the same primitive the row renderers use. It is a
no-op for Outlined. Nesting composes: an inner pane's background wins inside its
rect. The audit measured 0 unpainted cells across 32 renders at about 100 µs per
120×36 frame, before a no-escape fast path.
Bars, layout gaps and margins are outside any pane. The shell paints them with a
whole-frame pass in Screen.
(Audit where components rely on the terminal background;
findings in research/background-fill-audit.md)
Selection¶
ansi.Fillreplacestree.renderPreserving. All six row renderers use it with the selection background and pad the row to the inner width: inspector, eventlog, logview, textview, table, list. This is step 1 of the build and fixes today's holes under Outlined.- The surface declares the treatment. Outlined declares today's exactly: the
list's bold
Accentforeground with no background, aSubtlebackground in the others, and no difference on blur. Filled, Banded, Elevated and Accented declare aSelectionbackground withTextforeground in every component, andSelectionDimwhen the pane is blurred. Each row component carries the blurred style as its own option (SelectedBlurredStyleon list, table and eventlog;CurrentLineBlurredStyleon tree, inspector, logview and textview), set by the theme and used only when it sets a background, and redraws its rows on a focus change when it has one.
(Selection on filled surfaces)
Overlays¶
- Each surface carries an overlay form,
Surface.Overlay. Unset, it is the same shape with its fill role switched fromCardtoOverlay. Outlined sets Thick lines explicitly, so today's modals are byte-identical. It replacesTheme.BorderShapeOverlay. Confirm, alert, the action menu, the help overlay and a form's card are built with it, as is any later modal: anything drawn over a screen uses the overlay form. - A derived overlay form draws its edge on its own fill. A modal's parent is
whatever it covers, so an edge cell drawn there shows the terminal's own
background — a dark rim over the cards. Edge cells (Filled's lips, Accented's
title row) move onto the overlay fill. The margin ring stays as the surface
draws it: blank margins are transparent under
ZStack, and Elevated keeps its shadow, whose undrawn half takes the background of what the modal covers (below).internal/componenttestasserts it for every modal on every surface: the body is the overlay fill, the edge carries a background of its own, the margin shows the base or the shadow. layout.ZStackcomposites by painted cells: a cell that isn't a space, or that carries a background, covers the base. On each row it splices from the first painted cell to the last. Padding fromCenter/lipgloss.Placeand margin cells stay transparent. A block element (U+2580–U+259F) with no background of its own takes the background of the base cell under it, since the part of the cell it leaves undrawn would otherwise show the terminal's background — which is how a shadow sits on the card beneath a modal.- No scrim: the base isn't dimmed (out of scope, revisit only if modals don't stand out).
(Overlays on filled surfaces, modal forms)
Bars¶
The surface names the role the breadcrumb and statusbar sit on. Outlined uses
Bar (byte-identical). Filled, Banded, Elevated and Accented use Screen, so
the cards float in one field. Bar text keeps BarFG, falling back to Text
when it isn't legible there (surface.Legible: an OKLab lightness distance of
at least 0.25, well above the tone floor). Outlined keeps the palette's own
pair untouched. Info and Error keep their own bands. The ? help affordance
and the inline hints take the bar's role as their background.
Choosing a surface¶
app.Options.Palettes(wasThemes), cycled byThemeKey.app.Options.Surfaces, default[surface.Filled], cycled bySurfaceKey, unset by default (opt-in, likeThemeKey).- Config:
config.yamlgainspalette:(withtheme:read as an alias),surface:(matched by name from what the app offers, ignored when it offers one) andblock_glyphs:. - Env:
PaletteEnvVar(wasThemeEnvVar) andSurfaceEnvVar. - Live preview:
app.SetPalette(name)(wasSetTheme). - Screens need nothing new. A swap is a new
Themethrough the existingSetThemepath, and rule 4'sState/Restorecovers it. - Chrome moves:
BorderShapeActive/Inactivebecome Outlined's edge glyphs (surface.OutlinedWith(lipgloss.RoundedBorder())for a variant).SlotBracketsbecomeSurface.Brackets.Glyphsstay onTheme, exceptGlyphs.Rule, which isSurface.Divider.Glyphnow.
Per-component overrides¶
opts := t.Logview()
opts.Surface = t.Resolve(surface.Outlined) // this pane stays Outlined
Options.Surface holds only a resolved surface, so skipping Theme.Resolve
is a compile error. The override survives a surface cycle because SetTheme
re-applies it. A pinned surface brings its own margins or none.
(Where the surface choice lives, Per-component surface overrides)
Degraded terminals¶
- Quantise, then enforce the floor.
Deriverounds each role to the detected colour profile, then re-checks the contrast-floor pairs and pushes any that collapsed to the next distinct step. 256 colours, and so default tmux, keep filled surfaces. Palette overrides are rounded but never pushed. - Fallback chain. Resolution follows
Fallbackuntil the terminal meets the surface's needs: - Filled → Banded
- Elevated → Banded
- Banded → Outlined, below 256 colours
- Accented → Accented with a
┃bar, without block glyphs - Outlined ends every chain
- Block glyphs:
block_glyphs:in config. Unset, it is off for known font-drawn terminals (TERM_PROGRAM=Apple_Terminalfirst) and on elsewhere. - ANSI-index inputs: every derivation that mixes in a non-seed colour has a
seeds-only recipe, used when an input has no RGB (
Selectionbecomes "liftCard25%"). ANSI colours used directly pass through, soTerminal()works. - Visibility: when a chosen surface resolves to its fallback, the shell writes
one line to the output console ("surface elevated → banded (block glyphs
off)").
SurfaceKeyskips entries that resolve to a surface already offered.
(Surface fallback on degraded terminals)
Enforcement¶
The check works from geometry, never heuristics. Four assertions:
- Every cell inside the card (rect minus margins) carries a background.
- Margin cells carry none.
- The selected row has one background across the full inner width.
- Every cell of the frame carries a background, after any
ZStackcomposite.
The classifier is an SGR state machine on x/ansi.DecodeSequence in
internal/cells, shared with ansi.Fill, so the check and the fill agree on
what clears a background. It runs over three layers:
- Components in
internal/componenttest: every pane-owning component × the 5 surfaces × focused/blurred. States: empty, loading, filter focused, a selected row holding lipgloss,CellColorand hyperlink cells, a search highlight, row activity. Plus the four overlays throughZStack, and pinned surfaces against their own surface. - Examples: every launcher example through the full app shell, per surface.
- Outlined goldens, captured before the work starts (build step 2).
Downstream apps get the same assertions as a pkg/screentest rule,
SurfacesFill, once pkg/screentest exists.
(Enforcing the background-fill invariant)
Migration¶
This is a clean break in one minor version. There are no Go shims. The one
thing kept is the config key: users' config files are outside any app's
control, so theme: is still read when palette: is unset.
| Before | After |
|---|---|
theme.Dark() (and every named palette), as a Theme |
theme.New(palette.Dark()); as a palette, palette.Dark() |
theme.All() |
palette.All() |
theme.ByName(themes, name) |
palette.ByName(palettes, name) |
theme.Resolve(themes, envVar) |
gone: app.New picks the start from PaletteEnvVar and the config file |
theme.FromBase16, theme.Base16 |
palette.FromBase16, palette.Base16 |
theme.Terminal() |
palette.Terminal() |
a Theme literal |
a palette.Palette literal; Theme embeds it, so t.Accent still reads |
app.Options.Themes: []theme.Theme{…} |
app.Options.Palettes: []palette.Palette{…} |
app.Options.ThemeEnvVar |
app.Options.PaletteEnvVar |
app.SetTheme(name), app.SetThemeMsg |
app.SetPalette(name), app.SetPaletteMsg |
Theme.BorderShapeActive/Inactive |
a surface: t.Surface = t.Resolve(surface.OutlinedWith(shape)) |
Theme.BorderShapeOverlay |
the surface's Overlay form: s.Overlay = &overlay |
Theme.SlotBrackets |
Surface.Brackets (pane.SlotBracketStyle is now surface.Brackets) |
Glyphs.Rule |
Surface.Divider.Glyph |
config.yaml theme: |
palette: (theme: still read) |
Options.Surfaces defaults to [surface.Outlined], so an app that migrates
these calls and nothing else draws exactly what it drew before; the Outlined
goldens hold that.
The local modules that use tuilib — tui-builder (v0.35.0), tuiquery (v0.21.0) and tuiquery_old — pin released versions, so they migrate when a release carries this change. tuilib-skills and tuilib-detailed-progress are other checkouts of tuilib itself, not consumers.
Out of scope¶
- A scrim under modals.
- Ruled, Lipped and Bare as built-ins; they're custom surfaces.