How a Rust and QML App Fits
Together
Understand how Ljós runs your QML and Rust code on one Engine,
talks to background threads through bounded messages, uses 0 %
CPU when idle, and isolates faults.
After reading this page, you will understand how a Ljós application is structured around the
Engine, how background threads communicate with the UI thread without locks, why an idle application uses 0 % CPU, and how faults and boundaries keep the interface running when code fails.Ljós is a toolkit for malleable software: apps that people and their agents can reshape while they run. Your Rust code holds the application's logic, data and services, and the interface on top of it is declarative QML scripted in TypeScript. Four things follow for a Rust application:
Malleable. Your app can let its users replace its interface. A
ShellLoader shows their own shell.qml in place of your shell, your Rust code says where that file lives, and your shell comes back, with what broke and where, when theirs does not work (Let users replace the shell). Compiling the user's file on the device takes the compiler, which the user-qml feature links (App::user_qml); an app that does not let its users change it embeds components compiled ahead of time and links no parser.TypeScript first. Handlers and bindings are typed TypeScript, checked before anything runs, and your native types describe their properties, signals and methods to the compiler, so a misspelt member is a check error rather than a surprise at run time.
Observable.
App::inspector (the mcp feature) serves the same MCP inspector as ljos run --mcp, so ljos observe or a coding agent can read and drive your running app.Stable. Background threads talk to the UI thread only through bounded messages, a fault stays inside the nearest
ErrorBoundary, and an app at rest uses 0 % CPU.You write the Rust side of a Ljós application against the
ljos facade crate alone. Import ljos::prelude::* for the traits, types and macros, and use ljos::App to configure and run the application. What the prelude does not re-export is internal to the engine crates and may change without notice.Add
ljos as your only UI dependency in Cargo.toml:In
src/main.rs, register your native modules, pass the compiled QML pack, name the root component, and start the event loop:A shipped application compiles its QML ahead of time in
build.rs into a binary pack in OUT_DIR and embeds it with ljos::include_qml!. Because the QML is already lowered to intermediate representation (ljos_ir), the shipped binary links no QML parser or compiler. An application its users change enables user-qml and names their directory with App::user_qml: it links the compiler, keeps the sources in the pack, and compiles only once the user has a file. During development, you can enable the non-default dev-qml feature and use App::qml_dir (or App::qml_dirs for multiple import trees) to compile QML from disk at start-up.An application started with
App::run, App::run_headless or their _with forms writes what it logs (console.log, console.warn, console.error and the runtime's own warnings) to stderr, one line per entry after its level, exactly as ljos run does:A thread of its own writes these lines, so a terminal that scrolls slowly, a pipe nobody reads or a closed stderr never holds up the UI thread. That thread starts with the first line, so an application that logs nothing starts no thread. If stderr does not keep up for long enough to fill a 1 MiB queue, the lines that do not fit are dropped, and one line in their place says how many were lost. When the application ends, it waits up to two seconds for the remaining lines to be written.
To handle the log yourself, pass a handler to
App::on_log. Each LogEntry then goes to your handler, and nothing goes to stderr. LogEntry::line formats an entry as the stderr line above:App::log_to_stderr(false) writes the log nowhere and keeps it in the Engine instead, where Engine::take_log reads the latest 1024 entries. Whichever of on_log and log_to_stderr you call last applies. A handler set with Engine::set_log_handler while the application starts, from a module's register or from the setup closure of run_with, replaces either one. App::build and App::build_engine keep the log in the Engine unless you call one of these methods, since you run that Engine yourself.An application normally has one
Engine. It lives on the main UI thread and is !Send, so it can never be moved across threads or shared behind a mutex.Inside the
Engine live:The object store, which keeps every QML and native object in a generational
SlotMap<ObjectId, ObjectHeader>. Because ObjectId keys carry a generation counter, destroying an object never causes a use-after-free; any remaining reference in JavaScript reads as null.The
MetaObject registry, which stores property, signal and method tables for both Rust types and QML components. Derived types share inherited member entries behind Arc pointers rather than copying tables.The reactive dependency graph (
ljos_reactive), which tracks property dependencies and runs bindings and effects.A single JavaScript runtime and context (
rquickjs), shared across all components and windows in the Engine. Wrapper objects are created lazily when a script first touches a QML object, so a UI that runs no JavaScript never starts the JS host.The time-sliced
Incubator, which creates asynchronous components (Loader.asynchronous, Component.incubateObject, ListView cache delegates and StackView pushes) in slices of at most 3 ms per frame at 60 Hz (scaled down at higher refresh rates, never less than 250 µs).The frame scheduler and render tree, which serve work in priority order (
Input, Critical, Visible, Prefetch, Deferred, Idle) and hand shapes and texture updates to the host shell.Reading a property can evaluate a binding, which can call into JavaScript, which can call back into a Rust method. To keep re-entrant calls safe, the
Engine never holds a single RefCell borrow across a JavaScript call; instead, its internal state is split into separately borrowed parts (graph, objects, JS) and every Engine API takes short borrows.Ljós follows a strict ownership rule: one owner holds each piece of state, and threads talk to each other only through bounded messages (
UiMsg). No locks are taken on the property binding path.When a background thread sends the first message into an empty inbox through a
UiHandle, it sets an AtomicBool wake flag and calls request_repaint on the window host. At the next frame, the UI thread drains queued messages within the frame budget. If a burst of messages exceeds the budget, the remaining messages stay queued in the bounded inbox, which applies back-pressure to background senders without spinning or blocking the UI thread.Nothing in Ljós polls. The event loop sleeps until input arrives, a timer or animation fires, a state write marks the scene dirty, or a background thread posts a
UiMsg.The reactive graph is push-pull with three node states (
Clean, Check, Dirty) and allocates nodes lazily only when a property is observed or bound:1.
Writes push dirty marks and flush effects synchronously. Writing a new value to a property marks its direct observers
Dirty and its transitive observers Check (writing an equal value does not propagate). Eager effect roots—signal handlers, Connections, Behavior interceptors, Rust subscribers, layout polish effects, and accessibility observers when a screen reader is attached—then flush synchronously in height order before control returns to the writer.2.
Render bindings are pulled lazily on demand. Every binding that is not an effect root stays dirty until something reads it. Because the paint pass only visits visible items near the viewport, bindings on hidden or off-screen items do no work at all.
3.
Off-screen content costs close to nothing. A
ListView with 10,000 items creates only the delegates visible in the viewport (plus cacheBuffer). Items that are not painted do no layout, text shaping or GPU work.4.
Subsystems rest when unused. JavaScript garbage collection runs in the
Idle lane only after 100 ms of rest following work that grew the JS heap. A MediaPlayer or SoundEffect that is not playing, a VideoOutput that is not visible, and an inactive sensor schedule no timers or frames. Even TextInput.canPaste only checks the system clipboard when the window becomes active or an editor receives keyboard focus.A runaway script, a failed binding or a Rust panic in a handler should not freeze the frame or crash the process. Ljós takes its fault-isolation model from Erlang/BEAM:
One fault pipeline. Every failure (
Fault::{JsException, JsInterrupted, JsOutOfMemory, Panic, Startup}) goes through Engine::report_fault. It is logged with its QML file and line (plus the QML call stack for JavaScript exceptions), recorded in a 32-entry fault history, and queued for delivery at the start of the next frame (Engine::deliver_faults). Delivering faults between turns ensures the Engine never destroys an object while its own handler is still on the call stack.Rust panics are caught at the turn boundary. Panics inside native invokable methods, signal handlers, completion hooks,
on_message callbacks and background task completions are caught and turned into Fault::Panic. Other handlers connected to the same signal still run.Runaway JavaScript is interrupted. The JavaScript host checks an interrupt deadline roughly every 10,000 opcodes (about 266 µs). A handler that enters an infinite loop (
while (true) {}) is stopped with Fault::JsInterrupted and the application keeps working. Time spent inside native work requested by a script (such as Qt.createComponent or createObject) is credited back so slow native setup does not trip the script's interrupt limit.UI boundaries isolate and restart subtrees.
Loader, StackView pages, Window and ErrorBoundary (in the Ljos QML module) act as supervision boundaries, each with its own logical owner ID (ProcId).You can wrap any part of your QML tree in an
ErrorBoundary to catch faults in its children and show a fallback component:By default, a JavaScript exception in a handler or binding is logged as a warning and stops only that handler or binding. Setting
catchExceptions: true on an ErrorBoundary treats JavaScript exceptions inside it the same way as panics, script interrupts, out-of-memory faults and startup failures (such as a missing required property): the boundary destroys only its child content and instantiates fallback. Calling boundary.retry() destroys the fallback and recreates the content from its component definition. An ErrorBoundary without a fallback restarts its content immediately until it exceeds maxRestarts within within milliseconds, at which point it gives up and escalates the fault to the boundary around it (or to the application's fault handler if no outer boundary exists).To inspect supervision boundaries, recent faults, mailbox depths, wake causes, hot bindings and memory owners in a running application, run: