Design
What CPUI is for, how it produces a frame, and the principles behind its API. To learn how to use it, start with the guide.
What it is for
Section titled “What it is for”CPUI is a Rust UI framework that renders on the CPU. It is immediate mode: your code describes the whole interface on every update, instead of creating widget objects once and modifying them later. It targets lightweight desktop tools: settings panels, list- and table-based tools, small editors and viewers.
Its goals, in order of priority:
- Low footprint. No GPU context is created, nothing is drawn while the window is idle, and memory use stays close to that of a bare system window.
- Speed. Only what changed is redrawn, and large redraws are spread over several threads.
- Ergonomics. You describe the interface every frame, a widget returns its interaction result on the line where you write it, and styles are set by chaining methods.
- Multiple platforms. Desktop first (macOS, Windows, Linux), then mobile (iOS, Android).
- A modern appearance. Transitions, light and dark themes, and one set of design tokens: the named colors, sizes and spacing that all widgets share.
Non-goals:
- A GPU rendering backend. Content that needs a GPU, such as video or a 3D view, is planned to be embedded as a child layer.
- State management, retained or reactive. Your application owns its state.
- The largest possible widget set. The common widgets are completed first.
- The browser as a supported platform. CPUI compiles to WebAssembly for the live examples on the website, but the browser is not a supported target.
How a frame works
Section titled “How a frame works”A frame is one update of the window. Your application implements one method, ui, which
describes the whole interface each time it is called:
ui.column().gap(8).p(16).children(|ui| { ui.text_input(&mut name).placeholder("Name"); if ui.button("Add").primary().disabled(name.is_empty()).clicked() { count += 1; } ui.text(format_args!("{count} added")).muted();});- Interaction results are immediate.
clicked()returns its result on the line where you write the button. The widget’s position comes from the previous frame’s layout; the layout of the current frame is computed after the whole interface has been described. - A frame is rebuilt until it settles. If input, an action or the layout changes the state, the interface is rebuilt within the same frame. The display always matches the current state: when a click changes what a later widget shows, the change does not appear a frame late.
- Nothing runs while idle. A frame is built only when input arrives, a transition is running or the application requests one. An idle window draws nothing and uses no CPU time.
- Only what changed is drawn. The output of each node is compared with the previous frame, and only the areas that differ are redrawn.
- A stable frame allocates no memory. Builders live on the stack, and text is written into a reused buffer. Once the interface is stable, building, laying out and drawing a frame allocate nothing.
- A widget is identified by its position in the structure. Its identity is derived from its
parent, its kind and its index among siblings of the same kind. Text that is shown only
conditionally therefore does not change the identity of the widgets after it. In a list whose
items are added or removed, give each item a key with
ui.key.
The API
Section titled “The API”Six principles shape the API:
- One way to write an element. Every element starts with
ui.something(…)and is configured by chaining. It ends when you request a result or pass a closure for its children. - One vocabulary for styles. Containers, controls and
Stylevalues share method names. A length is a number of logical pixels, not a step on a scale. - Application data belongs to you; interface state belongs to the framework. You bind text,
selections and numbers with
&mut. The framework tracks what is expanded, open, scrolled or focused; with a key, you can open and close elements from outside. - Events are return values, not callbacks.
- Defaults cover the common case. Rows center their items vertically and text inputs fill the available width, so the usual layout needs no configuration.
- The syntax costs no memory. Builders stay on the stack and add no per-frame allocation.
The layers
Section titled “The layers”| Crate | Role |
|---|---|
cpui |
The framework: the frame model, layout, widgets, input, themes, windows, accessibility |
cpui-render |
The renderer: it rasterizes glyphs and composes the image on the CPU. It presents through IOSurface on Apple platforms and softbuffer elsewhere |
The renderer is derived from Winc. Windows and events come
from winit; accessibility is provided through AccessKit.
See also
Section titled “See also”- Performance: measurements, budgets, and a comparison with other frameworks.
- Roadmap: platform support and planned work.
- Versioning: what counts as a breaking change.