Themes#

A theme is one C file that defines every style part, the palette, the dimmed palette, the fonts and the metrics. CEUI comes with four, and your program can bring its own.

Picking a built-in theme#

Set UI_THEME in your makefile before including ui.mk:

UI_THEME = dark
include vendor/ceui/ui.mk
include $(shell cedev-config --makefile)

Theme

Look

default

Light and flat, with a blue accent

dark

The default shapes with a dark palette

classic

Gray bevels in the look of old desktop toolkits, with a navy title bar

contrast

Black, white and yellow, with 2 pixel borders

none

No theme from CEUI; your program defines one

default
The gallery in the default theme
dark
The gallery in the dark theme
classic
The gallery in the classic theme
contrast
The gallery in the contrast theme

The value widgets in each theme:

default
Value widgets in the default theme
dark
Value widgets in the dark theme
classic
Value widgets in the classic theme
contrast
Value widgets in the contrast theme

And a modal over the dimmed screen:

default
A modal in the default theme
dark
A modal in the dark theme
classic
A modal in the classic theme
contrast
A modal in the contrast theme

The theme is chosen at build time. Shapes (borders, radius, padding) are fixed then; colors can still change at run time with ui_palette and ui_set_color, see palette slots.

How a theme file works#

themes/default.h holds a base initializer for each part (UI_DEFAULT_BUTTON, UI_DEFAULT_INPUT, and so on), the default palette (UI_DEFAULT_PALETTE), the dimmed table (UI_DEFAULT_DIMMED), the fonts (UI_DEFAULT_FONTS) and the metrics (UI_DEFAULT_METRICS).

themes/default.c is a template. For every part it uses an override macro when you defined one, and the base otherwise:

#ifndef UI_STYLE_BUTTON
#define UI_STYLE_BUTTON UI_DEFAULT_BUTTON
#endif
const ui_style_t ui_style_button = {UI_STYLE_BUTTON};

A theme defines only the macros it changes, then includes the template. Later designated initializers override earlier ones, so a theme states only its differences:

#define UI_STYLE_BUTTON UI_DEFAULT_BUTTON, .radius = 3, .c[UI_FOCUS] = {.bg = UI_ACCENT, .fg = UI_ON_ACCENT}
#define UI_STYLE_INPUT UI_DEFAULT_INPUT, .border = UI_BORDER(1, UI_BOTTOM)
#include <themes/default.c>

The template silences -Winitializer-overrides for you. The whole dark theme is a palette and the include:

#define UI_PALETTE                                                                     \
  [UI_BG] = UI_RGB(17, 18, 23), [UI_SURFACE] = UI_RGB(28, 30, 38),                     \
  [UI_RAISED] = UI_RGB(42, 45, 56), [UI_LINE] = UI_RGB(62, 66, 80),                    \
  [UI_MUTED] = UI_RGB(140, 146, 160), [UI_TEXT] = UI_RGB(232, 234, 240),               \
  [UI_ACCENT] = UI_RGB(96, 150, 255), [UI_ON_ACCENT] = UI_RGB(10, 12, 18),             \
  [UI_DANGER] = UI_RGB(240, 96, 96), [UI_OK] = UI_RGB(80, 200, 120),                   \
  [UI_SHADE] = UI_RGB(6, 6, 8), [UI_DIM_BG] = UI_RGB(8, 8, 11),                        \
  [UI_DIM_SURFACE] = UI_RGB(14, 15, 19), [UI_DIM_TEXT] = UI_RGB(100, 102, 112)

#include "default.c"

Override macros#

Macro

Defines

Base

UI_STYLE_<PART>

ui_style_<part>, for each of the 28 parts, such as UI_STYLE_BUTTON or UI_STYLE_MENU_ITEM

UI_DEFAULT_<PART>

UI_PALETTE

ui_palette_default, the colors ui_init loads

UI_DEFAULT_PALETTE

UI_DIMMED

ui_dimmed, the slot each role turns into under a modal

UI_DEFAULT_DIMMED

UI_FONTS_LIST

ui_fonts

UI_DEFAULT_FONTS

UI_METRICS

ui_metrics

UI_DEFAULT_METRICS

The parts are SCREEN, TITLEBAR, SOFTKEY, PANEL, MODAL, MENU, MENU_ITEM, TOAST, HEADING, TEXT, CAPTION, SEPARATOR, BUTTON, CHECK, RADIO, TOGGLE, KNOB, CHOICE, SELECT, TRACK, FILL, THUMB, INPUT, LIST, ROW, HEADER, SCROLLBAR and TAB. What each one draws is listed under parts.

Writing your own theme#

  1. Set UI_THEME = none in the makefile, so CEUI compiles no theme of its own.

  2. Add a C file to your src/, for example src/theme.c.

  3. Define the macros you want to change, then #include <themes/default.c>.

ui.mk puts CEUI’s root folder on the include path, so <themes/default.h> and <themes/default.c> are found from your sources. This is the theme of the showcase, which changes five parts:

#include <ui.h>
#include <themes/default.h>

#define UI_STYLE_TITLEBAR UI_DEFAULT_TITLEBAR, .c[UI_NORMAL] = {.bg = UI_ACCENT, .fg = UI_ON_ACCENT}
#define UI_STYLE_BUTTON UI_DEFAULT_BUTTON, .radius = 2, .shadow = UI_SHADOW(0, 1), .shade = UI_SHADE
#define UI_STYLE_INPUT UI_DEFAULT_INPUT, .border = UI_BORDER(1, UI_BOTTOM), .radius = 0
#define UI_STYLE_ROW                                                                   \
  UI_DEFAULT_ROW, .border = UI_BORDER(3, UI_LEFT),                                     \
                  .c[UI_FOCUS] = {.bg = UI_RAISED, .hi = UI_ACCENT, .lo = UI_ACCENT}
#define UI_STYLE_CAPTION UI_DEFAULT_CAPTION, .c[UI_FOCUS] = {0, UI_MUTED, 0, 0}
#include <themes/default.c>

The same file can hold the program’s extra styles for ui_next_style and its palettes for ui_palette, as the showcase’s src/theme.c does. Parts your program never uses are still dropped at link time.

The showcase home in its own theme, in dark mode

The showcase’s own theme: an accent title bar and list rows with a bar on the left, here in dark mode.#

Metrics and fonts#

#define UI_METRICS UI_DEFAULT_METRICS, .label = 80, .modal = 280
#define UI_FONTS_LIST UI_DEFAULT_FONTS, [3] = {my_font_data, my_font_spacing, 1, 0}
#include <themes/default.c>

The dimmed table#

ui_dimmed maps each role slot to the slot it is drawn in under a modal. The default maps backgrounds to UI_DIM_BG, surfaces to UI_DIM_SURFACE, and text, lines and accents to UI_DIM_TEXT. A theme that wants lines to stay visible under a modal changes one entry, like contrast does:

#define UI_DIMMED UI_DEFAULT_DIMMED, [UI_LINE] = UI_DIM_TEXT

Two looks in one program#

Shapes are fixed at build time, so a program cannot switch from default to classic while it runs. It can:

  • switch colors with ui_palette, such as light and dark;

  • change one slot with ui_set_color, such as an accent picker;

  • use its own styles with ui_next_style for the widgets that should look different.