Trame

designer: live @(...) GUI designer

trame:designer (#import "trame:designer") is a live, note-driven GUI designer built on Formula's compile-time typed @(...) notes, base:compiler introspection, std:lang/lexer, and std:lang/parser. Annotate any plain Formula struct with @(designer.Editable) and field notes, extract its static schema at compile time with #run designer.schema_of(T), and edit values live through an inspector, direct canvas handles, a branching undo tree, and an async identity-anchored .fm source patcher.

The design, constraints, and refused alternatives live in src/md/features/designer.md. A complete runnable example is in examples/designer/main.fm (./build/trame build designer).

1. Annotate a struct and extract its schema

Formula
#import "trame:designer"
#import "trame:ui"

@(designer.Editable{source_symbol = "DEFAULT_CARD", label = "Hero Card"})
Card_Style :: struct {
	@(designer.Section{title = "Layout"})
	@(designer.Range{min = 280, max = 560, step = 4, unit = "px"})
	@(designer.Rect_Handle{box_id = "card", axis = .Width})
	width: f32,

	@(designer.Range{min = 10, max = 44, step = 2, unit = "px"})
	@(designer.Padding{box_id = "card"})
	padding: f32,

	@(designer.Section{title = "Surface"})
	@(designer.Color)
	accent: ui.Color,

	@(designer.Label{text = "Action label"})
	cta_text: string,
}

DEFAULT_CARD :: Card_Style{width = 400, padding = 20, accent = {0.26, 0.56, 0.96, 1.0}, cta_text = "Launch"}
CARD_SCHEMA :: #run designer.schema_of(Card_Style)
NoteTargetPurpose
@(designer.Editable{source_symbol, label})StructMarks the struct editable and names the constant or global compound literal to patch (empty patches struct field defaults).
@(designer.Section{title})FieldStarts a collapsible inspector section from this field forward.
@(designer.Label{text})FieldOverrides the display label derived from the field name.
@(designer.Range{min, max, step, unit})FieldSets bounds, step, and unit suffix for numeric and vector controls.
@(designer.Color)FieldRenders a color swatch and inline RGBA picker for a ui.Color ([4]f32).
@(designer.Rect_Handle{box_id, axis})FieldBinds a scalar field to direct width, height, or corner-radius handles over the ui.Box matching box_id (Handle_Axis: .Width, .Height, .Radius).
@(designer.Padding{box_id})FieldBinds a scalar or [4]f32 padding field to inset handles and guides on the named ui.Box.
@(designer.Hidden)FieldOmits the field from the inspector and patcher.
@(designer.Read_Only)FieldDisplays the field without editing or patching.

designer.schema_of($T) executes at compile time via #run and produces a plain value designer.Schema containing the originating source path (compiler.type_source_span(T)), field byte offsets, types, enum variant names, and parsed note metadata. It performs zero runtime reflection setup.

2. Run a session

Initialize a designer.Session bound to a live struct instance, poll worker results each frame, draw the target UI, then render the canvas overlay, inspector, code preview, and branching history panel.

Formula
// Startup
designer.session_init(&app.sess, &CARD_SCHEMA, &app.card)
defer designer.session_destroy(&app.sess)

// Each frame
designer.session_poll(&app.sess)
build_card_preview(&app.card)                    // builds the box with id "card"
designer.canvas_overlay(&app.sess, &h.state)     // direct-manipulation handles and guides
designer.inspector(&app.sess)                    // dense property inspector + Save/Reload/Undo/Redo
designer.code_preview(&app.sess)                 // live unified AST diff preview
designer.history_panel(&app.sess, &app.history_view) // branching undo tree (ui.history_tree)
ProcedureWhat it does
session_init(sess, schema, target)Binds sess to a typed struct pointer, snapshots baseline values, and initializes the branching undo.Tree.
session_destroy(sess)Frees session state, preview buffers, and undo history.
session_poll(sess)Drains completed background sync or reload jobs on the interface thread without blocking.
session_save(sess, ignore_conflict, force_overwrite_expr)Dispatches an async AST patch and atomic write to a trame:tasks worker.
session_reload(sess)Reloads the file on disk from a worker to resolve .Conflict or refresh the source snapshot.
session_commit_edit(sess, label, detail)Commits the current struct values as a new node in the branching undo tree.
session_undo(sess), session_redo(sess)Steps backward or forward along the active branch and restores struct values.
session_jump(sess, node_id), session_prune(sess, node_id)Jumps to any live history node or prunes an inactive branch.
session_reset(sess)Restores all fields to their initial baseline values.
session_refresh_preview(sess)Recomputes the in-memory AST patch and unified diff preview against the cached source snapshot.
session_set_source_snapshot(sess, source)Supplies an in-memory .fm source snapshot for disk-free previews and demos.
canvas_overlay(sess, state)Draws selection frames, resize/radius/padding handles, and pixel rulers over the settled rects of matched boxes.
inspector(sess)Builds the collapsible property inspector, dirty indicators, sync pill, and action bar.
code_preview(sess, show_header)Renders the live unified diff of what session_save will splice into the .fm file.
history_panel(sess, view)Renders the session's branching undo tree with ui.history_tree.

3. Identity-anchored AST patching and async sync

designer.patch_source (src/designer/patch.fm) is a pure transformation over source bytes:

  1. Lexes and parses the .fm source with std:lang/lexer and std:lang/parser.
  2. Finds the declaration matching Editable.source_symbol (or the struct declaration for field defaults) on every save instead of trusting compile-time byte offsets.
  3. Formats dirty fields only and splices non-overlapping byte spans in descending offset order, leaving comments, blank lines, trailing commas, and untouched fields byte-for-byte intact.
  4. Inserts missing keyed fields cleanly before the closing } of a keyed compound literal.
  5. Refuses non-literal initializers (such as BASE_WIDTH * 2 or a call) with Patch_Status.Expression_Locked unless force_overwrite_expr is true.
  6. Re-parses the spliced output before writing; any parse error aborts the write.

All disk I/O runs on a trame:tasks worker. Before writing via fsx.write_atomic, the worker compares the file's BLAKE3 digest against the last known digest. If an external editor changed the file on disk, Session.sync_state transitions to Sync_State.Conflict without overwriting.

Sync_StateMeaning
CleanLive values match the last synced source snapshot.
DirtyLive values differ from the baseline and await save.
WritingOne background worker job is in flight; further drags coalesce in pending_resync.
ConflictThe file on disk changed externally; choose Reload or Force Save.
ErrorI/O, parse, or anchor lookup failed; live edits remain intact in memory.

Tests

  • tests/designer/: compile-time schema extraction, identity-anchored AST patching, comment preservation, expression locking, conflict detection, and branching history (./build/run_tests designer).
  • ./build/trame build designer && ./build/designer --headless --frames=3: headless smoke test of the full example (--demo-morphed and --demo-branches open interactive preview states without writing to disk).

Edit this page on GitHub