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 |
|---|---|
|
Colors per state. |
|
|
|
0 to 3. Corners are rounded by cutting corner pixels |
|
|
|
|
|
The palette slot of the shadow. No shadow is drawn when it is 0 |
|
Alignment ( |
|
An 8 by 8 one bit image the widget draws in |
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 |
Raised bevel |
A lighter |
Inset field |
A darker |
Underlined field |
|
Pill |
|
Card |
A shadow and a |
Four button styles, each one record drawn by the same code.#
States#
State |
When |
|---|---|
|
The default |
|
The widget has focus |
|
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 |
|
After |
|
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_ACTIVEtakes theUI_FOCUSone, then theUI_NORMALone.A color left at 0 in
UI_FOCUS,UI_DISABLEDorUI_ONtakes theUI_NORMALone.A
fgthat is 0 inUI_NORMALtoo 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’sUI_FOCUSorUI_ONentry.A
bgthat 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 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|---|---|---|
|
Screen background |
244, 245, 247 |
|
Panels, fields, lists, title bar |
255, 255, 255 |
|
Buttons, headers, selected rows |
230, 233, 238 |
|
Borders and separators |
200, 204, 212 |
|
Captions, headings, secondary text |
100, 107, 120 |
|
Text |
22, 25, 31 |
|
Focus, selection, fills |
37, 99, 235 |
|
Text on the accent color |
255, 255, 255 |
|
Destructive actions |
200, 40, 40 |
|
Success |
22, 140, 70 |
|
Shadows |
170, 175, 185 |
|
Dimmed background under a modal |
140, 141, 145 |
|
Dimmed surfaces |
150, 151, 155 |
|
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 |
|---|---|
|
Writes slots 1 to |
|
Writes one slot |
|
Returns the palette index of a slot, for drawing with graphx in a canvas or a sprite |
|
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.
Dark mode is one ui_palette call.#
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 |
|---|---|
|
|
|
The width table for |
|
The integer text scale |
|
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 |
|---|---|---|
|
4 |
Space around the content area |
|
3 |
Space between widgets, rows and columns |
|
14 |
The minimum height of a labeled control |
|
92 |
The width of the label column of labeled controls |
|
18 |
Title bar height |
|
18 |
Softkey bar height |
|
4 |
Scroll bar width |
|
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 |
|---|---|
|
The background of the content area |
|
|
|
Containers and overlays |
|
|
|
|
|
|
|
Check box, radio button, toggle track, toggle knob |
|
|
|
Slider, progress bar and spinner |
|
|
|
List frame, list row, table header |
|
Scroll bars, tabs |
|
Items of |