Fusuma

Docs

Architecture

The layers from your app to the glass, what a frame does, and why it is shaped this way.

Fusuma is a retained widget toolkit with its own layout engine and a renderer you can swap. This page is the map: what the layers are, what a frame does, and why the code is arranged this way.

The layers

The layers of FusumaYour app, the fusuma crate with its view layer, widget tree, layout, theme and motion, the Renderer trait, three renderers, and the platform crates for desktop, web, ESP32, e-paper, terminal and phones.Your appState · Msg · update(state, msg) · view(state, env)fusuma (the one crate you add)The view layerProgram · Runner · View · Task · Sub · EnvThe widget treefusuma-ui: one node per widget, damage, focus, scrollLayouttsubo: integer flex and grid, no heapTheme and designpalettes, tokens, corner shapesMotion, text, iconsanim · text · fonts · iconsRendererfill · stroke · paths · clips · text · images · gradients · layers (one trait, one conformance suite)IntegerRGB565 · no allocator · no FPUVello CPUanti-aliased · SIMD · threadsVello GPUwgpu · desktop and webplatform crates: they own the window or the panel, and draw nothingDesktopwinitWebwasm + canvasESP32ESP-IDFE-papercontrollersTerminalANSI cellsPhoneAndroid · iOSA renderer crate never depends on the toolkit. A platform crate draws nothing itself. CI checks both.
From your app down to the glass. Everything above the Renderer trait is the same on every device.

Your app is a [Program](/docs/program/): a state, the messages that change it, update and view.

The view layer (fusuma) runs the program. It turns a view into widget-tree nodes, compares as it goes so only what differs changes, keeps timers and subscriptions, and, with the async feature, polls futures on the app's own clock. No async runtime is linked.

The widget tree (fusuma-ui) is where the toolkit lives: one node per widget, hit-testing, focus, scrolling and gestures, dialogs, text input, the semantic tree for assistive technology, and painting with damage tracking. It is no_std and needs no allocator.

Layout is [tsubo](/docs/crates/): integer flexbox and grid with no heap and no dependencies, checked against Taffy as an oracle. The name is the Japanese floor measure of two tatami mats, a whole unit and never a fraction.

The theme is data. Palettes, spacing, radii, type sizes, motion and corner shapes live in TOML and token files and are generated into the code; a test fails when they drift. See Design.

The renderer is one trait, Renderer. Everything above it is the same on every device; everything below it is chosen for the hardware. See Rendering.

The platform crates own the window, the web canvas, the panel or the terminal, feed input in and present frames out. They draw nothing themselves.

One frame

One frameAn input becomes a message, update changes the state, view describes the screen, the tree is synchronised in place, layout looks only at what changed, damage rectangles are collected, paint_frame draws inside them through the Renderer, and only the dirty rectangle is presented.Inputtouch · key · timerupdatestate, msg → taskviewstate, env → viewsSync the treesetters compare firstLayoutonly what changedDamagerectangles that differpaint_framedraws inside the damageRendererfill · text · pathsPresentthe dirty recta message changes the stateWith nothing changing, nothing is laid out, drawn or sent: the app sleeps until the next input or timer.
The tree is the previous state: a view is a cheap value made on the stack and compared with it, so nothing is kept between frames but the tree.
  1. An input arrives: a pointer, a key, a resize, a timer. The tree handles it and queues an action.
  2. update(state, msg) changes the state in the one place that is allowed to, and returns a Task: nothing, or up to a few effects in order.
  3. view(state, env) describes the screen. The description is made on the stack, so the tree is the previous state and nothing else is kept.
  4. The description is synced into the tree. Every setter compares first, so a label with the same text, a style with the same numbers, changes nothing and asks for nothing.
  5. Layout walks only the subtrees that changed. A box that only moved carries its children. A tree of 900 nodes with one changed cell lays out in a few microseconds.
  6. Changes record damage: the rectangles whose pixels differ. Several separate changes are drawn apart, and a scroll can move pixels instead of redrawing them when the renderer retains its picture.
  7. paint_frame draws inside the damage through the Renderer, and the platform presents the dirty rectangle.

With nothing changing, nothing is laid out, drawn or sent, and the app sleeps until the next input or timer. That is what lets a battery-powered board keep its screen still, and what lets an e-paper panel refresh only a region.

Why it is shaped like this

  • Integer layout, no allocation. A chip has no FPU worth using and often no heap to spare. tsubo does its arithmetic in integers and a pass allocates nothing, so the same engine runs on an ESP32-C6 and on a desktop.
  • Constants for sizes. A tree of N nodes and K message slots is a type. A running session does not allocate, and a test guards it.
  • A retained tree with damage. Redrawing everything each frame is fine on a GPU and ruinous on a panel behind an SPI bus. Keeping the tree and recording damage makes both cases cheap.
  • Messages are values. Msg is Copy, so a session can be logged and replayed, a timer can hold one, and a test can send one and look at the tree.
  • Data, not code, for what varies. Fonts, icons, colours, corner shapes and boards are files. A new Waveshare board is a file when its controllers are already known.

Measured

Layout of the same dashboard, 36 cards in a four-column grid, 902 nodes, on one Apple M-series core in a release build (crates/fusuma-ui/tests/taffy_bench.rs, run by hand):

FusumaTaffy 0.15
from scratch (the window gets another width)63 µs96 µs1.5x
one cell changes height inside its row2.4 µs21 µs9x
a cell grows, so the cards below move3.8 µs29 µs7.6x
a tree 60 levels deep, the leaf changes width9 µs138 µs15x
allocations in a from-scratch pass01,551

A wrapping flow of 600 boxes is about even (42 µs against 47 µs): it is the shape where Fusuma is not ahead, because every child moves and each move records damage.

Against iced 0.14 on the same dashboard, with a layout-only renderer:

Fusumaiced
layout from scratch88 µs17 µsiced is 5x faster
a frame after one cell changed4.6 µs49 µsFusuma is 10x faster

iced remembers nothing between frames, so a layout of everything is a plain walk and very quick, while Fusuma keeps the tree and lays out only what changed. The case where everything changes at once, a window being dragged, is Fusuma's weak one, and 88 µs is well inside a frame. The tables are the latest runs; the machine is noisy, so read the ratios.

On a board: the gallery on an ESP32-S3 watch at 240 MHz takes 92 ms for a first frame, 12 ms for a tap and 36 ms for a scroll step with the integer renderer.

Next: the program model, or the renderers.