Architecture

Goals

Frame loop

input event ──► dispatch ──► state updates ──┐
task wake ────► poll tasks ──► results ───────┴──► App::view ──► new Element tree
                                                                        │
RedrawRequested ◄── request_redraw ◄── measure (taffy) + place ◄───────┘
      │                       ▲
      │                       └── place only: redraw without a rebuild
      │                           (hover, press, scroll, caret)
      │
      ▼
 paint every node ──► Scene (flat instance list) ──► Renderer (one pass) ──► surface

Runtime (src/runtime/mod.rs) owns the app, the current element tree, per-widget state, the text system and the renderer. On desktop and the web it is a winit ApplicationHandler. On Android, runtime/activity.rs drives it from android-activity's poll_events instead, so the loop can return on onDestroy and start again for a recreated activity; winit can't do either. There, the renderer is dropped when the native window goes away (backgrounded) and rebuilt when a new one arrives. The renderer only sees the window through the Host trait.

Modules

FileRole
lib.rsPublic API: App, run, run_android, re-exports.
widget.rsElement, Widget trait, Event, State, PaintCx, EventCx.
widgets/mod.rsWidget infrastructure: the Styled flex modifiers, re-exports.
widgets/*.rsOne file per built-in: container (row/column/container/spacer), label (text), button, icon (icon/icon_button), scroll, stateful, provide, builder (future_builder/stream_builder), text_input, toggle, slider, progress (progress/stacked_progress/wait_bar/spinner), radio, segmented (segmented/tabs), overlay (overlay/popup and the dialog, drawer, bottom_sheet, snackbar, tooltip, dropdown built on them).
anim.rsAnimated<T> transitions, Lerp, ease curves; driven by PaintCx::now and request_frame. Morph between two Paths (e.g. glyphs): contours with as many corners pair corner to corner, and segments of the shorter side are cut into as many pieces as needed.
kinetic.rsDrag: drag slop, release velocity and fling decay for widgets that scroll themselves (scroll, app widgets).
path.rsPath: contours of quadratic segments, filled by PaintCx::path through the glyph renderer (per-frame shapes use a reserved region of the outline buffers). Shape: a Path stored once like a glyph and drawn into any rect by PaintCx::shape, for icons.
layout.rsFlattens the tree to pre-order nodes; measure runs taffy, place produces absolute rects, clips and scroll offsets.
runtime/mod.rsRuntime: input → Event translation, dispatch, pointer capture, focus, frame pacing and stats.
runtime/winit_app.rsDesktop and web event loop (winit ApplicationHandler).
runtime/activity.rsAndroid event loop on android-activity: lifecycle, touch, keys, soft keyboard, system bar icon color.
runtime/android.rsdfui::android: with_activity JNI helper, system bar insets and appearance.
runtime/task.rsWidget-owned futures/streams, polling, sleep timers, the event-loop waker.
runtime/clipboard.rsSystem clipboard on desktop, in-app elsewhere.
runtime/bench.rsBench, GpuBench and snapshot, through the real runtime without a window.
render/primitives (Color/Rect/Shadow/Bitmap), scene (Scene, damage), gpu (wgpu setup, the single render pass, GPU timestamp timing, offscreen rendering).
shader.wgslVertex expands instances to quads; fragment does SDF shapes (kind 0) and outline glyphs (kind 1).
text.rsFont handles (bundled DejaVu Sans as default and fallback), per-word shaping with harfrust, greedy word wrap, lazy glyph outline building.
outline.rsConverts glyph contours to monotonic quadratics and per-glyph band lists for the shader.

Widgets and state

Async

Web

Layout

flatten produces a pre-order Vec<Node>, once per rebuilt tree; other passes re-walk it from cached ids (tree.md). measure builds a taffy tree from each widget's style()/measure() and keeps each node's parent-relative box; place turns those into absolute rects, applying scroll() offsets and intersecting clip rects. The runtime keeps measure's result until the tree is rebuilt or the viewport changes, so redraws without a rebuild only rerun place. The taffy tree itself is kept across rebuilds, matched by node id: only nodes whose style, children or measure_key() changed are marked dirty, and taffy's cache recomputes just their path to the root. Widgets whose measure depends on their own data must override measure_key. The root fills the viewport, with insets added to its padding so backgrounds bleed under system bars. Text layouts are cached in Text by (string, size, width) and evicted after a frame that doesn't use them, so repeated measure calls and per-frame rebuilds of the same strings are cheap.

Input

Events are offered in reverse paint order (deepest / topmost first). A widget can stop() propagation or capture() the pointer; on capture every other widget receives Event::Cancel and subsequent pointer events go only to the captor until pointer up. Touch takes up to two fingers: a second finger moves the one pointer with the fingers' midpoint (shifted so it doesn't jump when a finger lands or lifts) and sends Event::Pinch with the spread's change. On desktop, ctrl+wheel and touchpad pinch gestures send Event::Pinch too.

scroll captures the pointer once a drag passes a 6px slop. On release it flings: the offset speed (from the last 100ms of pointer samples) decays as e^(-t/0.35s) until it drops below 10 px/s or hits an edge. The offset is computed in Widget::scroll from the frame time, and the scroll view's paint calls request_placement so placement reruns every frame while the fling lasts, without a relayout.

One widget at a time holds keyboard focus, claimed with cx.focus() on pointer down; a tap no widget claims blurs it. Event::Text and Event::Key go only to the focused widget, which sees cx.focused in event and paint, and cx.shift for extending selections. Ctrl/Cmd+A, C and X arrive as Key::SelectAll, Key::Copy and Key::Cut; a widget hands text to the clipboard with cx.copy. Paste arrives as Event::Text with the clipboard's contents. On Android, focus shows the soft keyboard through runtime/editor.rs: a Java view with an InputConnection, whose edits arrive as Event::Text and Key::DeleteBefore. A field's Keyboard picks the keyboard type. Hardware keys still arrive as key events.

Layers

A widget whose layer() returns Some floats over the tree instead of taking part in its flow: measure makes it position: absolute over its parent (Layer::Parent, which is made relative) or position: fixed over the window inside the system bars (Layer::Window), using taffy's containing blocks. place gives its subtree the viewport as clip, so it escapes scroll views, and leaves it out of the parent's scrollable content. Paint order is pre-order, except that each layer's subtree comes after everything it covers (sorted by layer depth, computed once per tree in Runtime::order); dispatch walks that order in reverse, so layers get input first. Pointer events only reach the nodes under the pointer, the ones it just left or pressed, and layers (docs/tree.md, "Pointer targets").

Overlay is the built-in layer. Its content sits in a Sink that stops pointer events hitting it, so any press that reaches the overlay landed outside: that is on_dismiss, and modal also stops everything else and paints a scrim. Tooltips and snackbars hide themselves (hover, timeout) by scrolling their content out of the overlay's clip, so showing and hiding needs no rebuild. Dialogs, drawers, sheets and snackbars slide the same way: scroll eases an Animated between closed and open and offsets the content by that fraction of the overlay's rect, and the scrim fades with it.

A removed node can't animate out, so overlays(|ov| ..) hosts the ones opened from handlers. It is a stateful holding a builder per entry; ov.show(cx, build) adds one and Shown::close marks it closing, which the overlay animates out. Each entry sits in a future_builder whose future sleeps until the entry's removal time (dfui::sleep) and then builds nothing, so removal is an ordinary task result and rebuild: the tree's shape still only changes in set_root. The deps are (key, closing), so closing restarts the timer while the overlay below keeps its state. Removed entries are pruned from the list on the next show.

Rendering

Details and design decisions, including partial redraws: rendering.md.

Testing

Known limits

Tracked in TODO.md and as ponytail: comments in the code: no IME composition on desktop, no bidi, fallback only to the default font, single pointer, full view rebuild and paint per change, linear hit-testing.