Layout#
Widgets stack from top to bottom inside their container, each one as wide as the container, with
ui_metrics.gap pixels between them (3 in the default theme). There are no coordinates to compute.
The screen is the outer container: from below the title bar to above the softkey bar, inset by ui_metrics.margin
(4 pixels). Call ui_titlebar and ui_softkeys first in the frame so the content area is known before anything is
placed in it.
Columns#
ui_columns(count, widths) puts the next widgets side by side in count columns, left to right. After the last
column the next widget starts a new row, so a loop fills a grid. The columns stay until the next ui_columns call or
the end of the container. ui_columns(1, NULL) goes back to one column.
ui_columns(3, NULL);
if (ui_button("Plain")) presses++;
if (ui_button("Primary")) presses++;
if (ui_button("Reset")) presses = 0;
ui_columns(1, NULL);
widths is an array of count ints, copied when you call ui_columns, so it can be a local. NULL makes equal
columns. Each width is one of:
Width |
Meaning |
|---|---|
A number above 0 |
That many pixels |
|
As wide as the widget’s content: the label of a button, the text of a label |
|
An equal share of what the other columns and the gaps leave |
static const int sidebar[] = {104, UI_FILL};
static const int mixed[] = {UI_AUTO, UI_FILL, 48};
An auto column after a fill column gets what the fill column left, which may be nothing, so put auto columns before
fill columns. A row is as high as its tallest widget. At most UI_COLUMNS (8) columns fit in a row.
A 104 pixel column next to a fill column, and inside it a row of auto, fill and 48 pixel columns.#
Heights#
Each widget has a natural height: one text line plus padding for most of them, ui_metrics.row (14 pixels) at least
for labeled controls. ui_height(h) makes the next widget h pixels high instead. It applies to one widget.
ui_columns(4, NULL);
for (int i = 0; i < 16; i++) {
ui_height(22);
if (ui_button(keys[i])) press(keys[i][0]);
}
ui_columns(1, NULL);
ui_space(h) leaves h pixels empty. ui_separator() draws a horizontal line in ui_style_separator, with 2
pixels of space above and below it in the default theme.
Labeled controls#
Check boxes, toggles, choices, selects, sliders, numbers and text fields are one row: the label on the left in a
column ui_metrics.label pixels wide (92 in the default theme, at most half the row), the control on the right. Pass
NULL as the label to give the control the full width. Check boxes and radio buttons draw their label right after
the box instead.
Panels#
ui_panel_begin(title) and ui_panel_end() make a framed container in ui_style_panel. Its width is the column it
is in, and it is as high as its content. The title is optional; pass NULL for none. Panels nest.
ui_columns(2, sidebar);
ui_panel_begin("Battery");
ui_progress(battery, 4);
ui_caption(ui_fmt("Level %u of %u", battery, 4));
ui_panel_end();
ui_next_style(&style_card);
ui_panel_begin("Panels");
ui_text("Panels size themselves to their content, take any style and nest.");
ui_panel_end();
ui_columns(1, NULL);
Columns set inside a panel end with the panel, and the columns of the parent are back after ui_panel_end. Every
ui_panel_begin needs its ui_panel_end in the same frame.
Scroll areas#
ui_scroll_begin(&scroll, h) and ui_scroll_end() make an area h pixels high that clips its content and shows a
scroll bar when the content is taller. UI_FILL as the height takes the rest of the container, which is what most
screens want.
static ui_scroll_t scroll;
ui_scroll_begin(&scroll, UI_FILL);
buttons();
switches();
choices();
ui_scroll_end();
The ui_scroll_t holds the state: y is how far the area is scrolled, and content is the height of the content,
set by ui_scroll_end. Keep it in a static or global variable. You can read both, for example to show a position,
and set y to jump.
Scrolling follows focus: an arrow that finds no widget in its direction scrolls the area by one row, and a focused
widget that is partly hidden is scrolled into view. An area with no focusable widget inside, like a page of text,
becomes a focus stop itself, and [left] and [right] page through it.
Commands outside the visible part of a scroll area are not recorded, so a long page costs little to draw. At most
UI_SCROLLS (8) scroll areas are tracked per frame.
A scroll area that holds only text is a focus stop of its own.#
Nesting#
Containers (panels, scroll areas, lists, modals, menus) nest up to UI_DEPTH (8) levels, the screen included.
Each container restores the columns of its parent when it ends.