Styling#

Every look in CEUI comes from data: style records in flash, a few palette entries, a font table and a set of metrics. There is no drawing code per theme or per look. This page explains each part; themes shows how to put them together.

The style record#

typedef struct {
  uint8_t bg, fg, hi, lo;
} ui_colors_t;

typedef struct {
  ui_colors_t c[UI_STATES];
  uint8_t border;
  uint8_t radius;
  uint8_t pad;
  uint8_t shadow;
  uint8_t shade;
  uint8_t text;
  const uint8_t *glyph;
} ui_style_t;

A style is 29 bytes in flash and nothing in RAM.

Field

Meaning

c[state]

Colors per state. bg fills, fg draws text and glyphs, hi draws the top and left border, lo the bottom and right border. Each is a palette slot

border

UI_BORDER(width, sides): width 0 to 3, sides any of UI_TOP, UI_BOTTOM, UI_LEFT, UI_RIGHT joined with |, or UI_ALL

radius

0 to 3. Corners are rounded by cutting corner pixels

pad

UI_PAD(x, y): space between the border and the content, 0 to 15 pixels each

shadow

UI_SHADOW(dx, dy): a drop shadow offset, 0 to 3 pixels each

shade

The palette slot of the shadow. No shadow is drawn when it is 0

text

Alignment (UI_ALIGN_LEFT, UI_ALIGN_CENTER, UI_ALIGN_RIGHT), UI_FONT(n), and UI_RING, joined with |

glyph

An 8 by 8 one bit image the widget draws in fg: the check mark, the radio dot, the select arrow

UI_RING draws a focus ring around the widget, outside its border, in the hi color of its UI_FOCUS state, while it has focus. Check boxes, radio buttons, toggles and fields use it in the default theme.

The same record covers every look:

Look

How

Flat outline

Equal hi and lo

Raised bevel

A lighter hi than lo, usually with a 2 pixel border

Inset field

A darker hi than lo

Underlined field

UI_BORDER(1, UI_BOTTOM)

Pill

radius = 3

Card

A shadow and a shade slot

Flat, outline, bevel and pill buttons

Four button styles, each one record drawn by the same code.#

States#

State

When

UI_NORMAL

The default

UI_FOCUS

The widget has focus

UI_ACTIVE

The widget is being pressed or edited: a button in the frame it is pressed, a select with its menu open, a number while digits are typed

UI_DISABLED

After ui_enabled(false)

UI_ON

Checked, selected or active: a checked box, the active tab, the selected row of a list without focus

Fallback#

Colors fall back field by field, so a style only states what changes:

  • A color left at 0 in UI_ACTIVE takes the UI_FOCUS one, then the UI_NORMAL one.

  • A color left at 0 in UI_FOCUS, UI_DISABLED or UI_ON takes the UI_NORMAL one.

  • A fg that is 0 in UI_NORMAL too comes from the container. Widgets in a container are drawn in the container’s state, so the text in a selected list row resolves its color from the row’s UI_FOCUS or UI_ON entry.

  • A bg that is 0 everywhere draws no fill, so the container shows through.

  • UI_NONE (0xFF) means transparent: nothing is drawn for that field.

Designated initializers make this short:

const ui_style_t style_primary = {UI_DEFAULT_BUTTON, .radius = 2, .text = UI_ALIGN_CENTER | UI_RING,
                                  .c[UI_NORMAL] = {UI_ACCENT, UI_ON_ACCENT, UI_ACCENT, UI_ACCENT},
                                  .c[UI_FOCUS] = {.hi = UI_TEXT, .lo = UI_TEXT}};

UI_DEFAULT_BUTTON and the other UI_DEFAULT_* macros come from themes/default.h. Starting from one and changing a few fields is the usual way to write a style. Later initializers override earlier ones, so add #pragma clang diagnostic ignored "-Winitializer-overrides" to the file, or include themes/default.c, which does it.

One widget in another style#

ui_next_style(&style) replaces the main style of the next widget only:

ui_next_style(&style_danger);
if (ui_button("Reset")) presses = 0;

That is how a program makes a primary button, a danger button, a large paragraph or a card panel without a function for each. The style must stay valid while the frame is built, so make it const at file scope.

Widget

Main style

ui_button, ui_icon_button

button

ui_check, ui_radio, ui_toggle

check, radio, toggle

ui_choice, ui_select

choice, select

ui_slider

thumb

ui_input, ui_textarea, ui_number

input

ui_heading, ui_text, ui_caption, ui_label

heading, text, caption, text

ui_separator, ui_header, ui_tabs

separator, header, tab

ui_panel_begin, ui_list_begin

panel, list

ui_modal_begin, ui_menu

modal, menu

ui_titlebar, ui_softkeys

titlebar, softkey

Parts that a widget uses besides its main style, like the knob of a toggle, the track and fill of a slider, list rows and menu items, come from the theme.

Palette slots#

Styles do not hold colors. They hold palette slots, and the colors live in the palette. CEUI owns UI_SLOTS entries (16 by default) at the top of the 256 entry palette, starting at UI_PALETTE_BASE (256 - UI_SLOTS). Changing a slot’s color changes it everywhere on the screen at once, with no redraw.

Slot 0 means “inherit”. Slots 1 to 14 have roles shared by all themes:

Slot

Role

Default color

UI_BG

Screen background

244, 245, 247

UI_SURFACE

Panels, fields, lists, title bar

255, 255, 255

UI_RAISED

Buttons, headers, selected rows

230, 233, 238

UI_LINE

Borders and separators

200, 204, 212

UI_MUTED

Captions, headings, secondary text

100, 107, 120

UI_TEXT

Text

22, 25, 31

UI_ACCENT

Focus, selection, fills

37, 99, 235

UI_ON_ACCENT

Text on the accent color

255, 255, 255

UI_DANGER

Destructive actions

200, 40, 40

UI_OK

Success

22, 140, 70

UI_SHADE

Shadows

170, 175, 185

UI_DIM_BG

Dimmed background under a modal

140, 141, 145

UI_DIM_SURFACE

Dimmed surfaces

150, 151, 155

UI_DIM_TEXT

Dimmed text

85, 86, 90

UI_ROLES (15) is the first free slot. Slots from UI_ROLES to UI_SLOTS - 1 are yours. To get more, raise UI_SLOTS with -DUI_SLOTS=20 in CFLAGS. The showcase adds a gold slot this way:

enum { SLOT_GOLD = UI_ROLES, SLOT_COUNT };

const ui_style_t style_gold = {UI_DEFAULT_BUTTON, .c[UI_NORMAL] = {SLOT_GOLD, UI_TEXT, SLOT_GOLD, SLOT_GOLD},
                               .c[UI_FOCUS] = {.hi = UI_TEXT, .lo = UI_TEXT}};

Under a modal, your own slots are dimmed to UI_DIM_TEXT.

Changing colors at run time#

Call

Effect

ui_palette(colors, count)

Writes slots 1 to count - 1 from an array indexed by slot. colors[0] is not used

ui_set_color(slot, color)

Writes one slot

ui_color(slot)

Returns the palette index of a slot, for drawing with graphx in a canvas or a sprite

UI_RGB(r, g, b)

Turns 8 bit components into the 1555 color graphx uses, at compile time

ui_init loads the theme’s ui_palette_default. A light and a dark palette with an accent picker look like this:

static const uint16_t dark[SLOT_COUNT] = {
    [UI_BG] = UI_RGB(18, 18, 24), [UI_SURFACE] = UI_RGB(30, 31, 40), [UI_TEXT] = UI_RGB(232, 232, 240),
};

void theme_apply(void)
{
  ui_palette(theme_dark ? dark : light, SLOT_COUNT);
  ui_set_color(UI_ACCENT, accents[theme_accent]);
}

A palette array must give every slot from 1 to count - 1 a color; slots left out of the initializer are black.

The Looks screen in dark mode

Dark mode is one ui_palette call.#

The Looks screen with red, green and blue sliders for a custom accent

A custom accent set with ui_set_color from three sliders.#

Fonts#

The theme defines ui_fonts[UI_FONTS] (4 entries by default). A style picks one with UI_FONT(n) in its text byte.

typedef struct {
  const uint8_t *data;
  const uint8_t *spacing;
  uint8_t scale;
  uint8_t monospace;
} ui_font_t;

Field

Meaning

data

NULL for the graphx font, which is already proportional. Otherwise a graphx font, 8 pixels high, made with convfont, set with gfx_SetFontData

spacing

The width table for data, set with gfx_SetFontSpacing

scale

The integer text scale

monospace

A fixed advance in pixels, or 0 for proportional

Font

Default theme

0

The graphx font, for text

1

The graphx font at scale 2, for large text

2

The graphx font with a fixed advance of 8, for numbers in tables

3

The graphx font, free for your theme

Fonts are 8 pixels high and scaled by whole numbers, so text is 8, 16 or 24 pixels high.

Metrics#

ui_metrics holds the sizes used across the toolkit:

Field

Default

Meaning

margin

4

Space around the content area

gap

3

Space between widgets, rows and columns

row

14

The minimum height of a labeled control

label

92

The width of the label column of labeled controls

titlebar

18

Title bar height

softkeys

18

Softkey bar height

scrollbar

4

Scroll bar width

modal

260

Modal width

A theme changes them with UI_METRICS, see themes.

Parts#

Each part is a const ui_style_t named ui_style_<part>, defined by the theme. The linker keeps only the parts a program uses.

Part

Used by

screen

The background of the content area

titlebar, softkey

ui_titlebar, each key of ui_softkeys

panel, modal, menu, toast

Containers and overlays

heading, text, caption

ui_heading, ui_text and ui_label, ui_caption

separator

ui_separator

button

ui_button, ui_icon_button, the buttons of message and confirm

check, radio, toggle, knob

Check box, radio button, toggle track, toggle knob

choice, select

ui_choice with its left and right arrows, ui_select

track, fill, thumb

Slider, progress bar and spinner

input

ui_input, ui_textarea, ui_number

list, row, header

List frame, list row, table header

scrollbar, tab

Scroll bars, tabs

menu_item

Items of ui_menu and ui_select