Expand description
compose-rust’s own runtime: positional memoisation on a slot table, with Compose’s shape and Compose’s names.
use compose_rust::runtime::*;
use compose_rust::ui::*;
#[composable]
fn Counter() {
let count = remember(|| mutable_state_of(0));
Column().content(|| {
Text(format!("Count: {}", count.get()));
let more = count.clone();
Button("Increment").on_click(move || more.update(|count| *count += 1));
});
}
fn main() {
compose_rust::ui::application(Counter);
}Nothing is compared to find out what changed. A state remembers which scopes read it; writing it marks those scopes invalid, and the next frame runs exactly them again. Inside a scope that runs, a composable whose parameters equal the ones it last had is skipped, its slots and nodes carried over untouched. What a widget writes is compared with what it wrote last time, one attribute at a time, and only an attribute that changed becomes a record.
Positional memoisation. remember belongs to its call site: the group path the
#[composable] macro builds around every function and every branch, and the order of
calls inside each group. A branch that stops being taken takes what it remembered with
it; a loop that gets shorter drops the iterations it lost. Where order is not identity,
because items move, key says which item is which.
One thread. Composition, effects and event handlers run on the Renderer’s UI
thread, inside a Host call. Other threads write MutableStates and wake futures; both
queue an id and ask for a frame, and the UI thread does the rest.
Structs§
- Batch
- One batch, written by the runtime and by the Host into the same arena.
- Compose
Host - The Host of a composable application: compose-rust’s
Hostdriving aRecomposer. - Coroutine
Scope - A place to start futures from an event handler, whose futures are cancelled when the
call site that remembered it leaves the composition. Compose’s
CoroutineScope. - Derived
State - A value computed from other states, which tells its readers only when the result
changes. Compose’s
derivedStateOf. - Disposable
Effect Result - What a
disposable_effectleaves behind: the cleanup to run when it leaves. - Group
Guard - Holds a group open for the rest of a block. The group closes on every way out of the
block,
return,?,breakand an unwind included, because it closes inDrop. - Mutable
State - A value that composition observes. Compose’s
MutableState. - Recompose
Scope - The scope composing now, to be told to run again by hand. Compose’s
currentRecomposeScope. - Recomposer
- The slot table runtime for one root: what composes the tree and runs its handlers.
- Snapshot
Mutation Policy - When a write counts as a change. Compose calls this a
SnapshotMutationPolicy.
Traits§
- Runtime
- What builds and changes the tree, seen from the boundary.
- State
- Something composition can read. Compose’s
State.
Functions§
- application
- The same as
launch, under Compose Desktop’s name for it. - current_
design_ system - The design system the Renderer resolved the theme to, recorded as read.
- current_
notification_ permission - Whether notifications may be shown, recorded as read.
- current_
recompose_ scope - The innermost scope composing now.
- current_
window_ size - The window’s size, recorded as read: the composable runs again when the Renderer reports a different size class.
- delay
- Resolves after
duration. Compose’sdelay. - derived_
state_ of - A state computed from the states
calculationreads. - disposable_
effect - Runs
effectafter this call site enters the composition and again wheneverkeychanges, and runs the cleanup it returned before the next run and when the call site leaves. Compose’sDisposableEffect. - key
- Gives
contentan identity of its own, made fromkeyrather than from its position. Compose’skey. - launch
- Runs a composable as the application, with every launch choice left to its default.
- launched_
effect - Runs
block’s future when this call site enters the composition, and again, after cancelling the last one, wheneverkeychanges. Cancelled when the call site leaves. Compose’sLaunchedEffect. - mutable_
state_ of - A state whose writes of an equal value change nothing. Compose’s
mutableStateOf. - mutable_
state_ of_ with_ policy - A state with a policy of its own for what counts as a change.
- never_
equal_ policy - Every write is a change, whatever it wrote.
- on_
dispose - The cleanup a
disposable_effectreturns. Compose’sonDispose. - on_
notification_ activated - Calls
handlerwhen the user presses a notification this application posted, for as long as this call site is in the composition. - recomposer_
for - The runtime factory for one composable root: what
crate::LaunchBuilder::launch_runtimeand the browser’s start take. - remember
- The value this call site remembered, or
init()’s, kept for next time. Compose’sremember. - remember_
coroutine_ scope - A coroutine scope bound to this call site. Compose’s
rememberCoroutineScope. - remember_
node_ size - Follows one node’s measured size, recorded as read. Compose has no single equivalent;
it is
onSizeChangedwith the class judgement made for you. - remember_
with_ key - Like
remember, but computed again wheneverkeychanges. Compose’sremember(key) { }. - side_
effect - Runs
effectafter every composition of the scope it is in that is applied. Compose’sSideEffect: how a composable publishes something to an object the composition does not manage. - structural_
equality_ policy - A write of an equal value changes nothing and runs nothing again.
- with_
frame_ nanos - Resolves on the next frame with that frame’s time, in nanoseconds. Compose’s
withFrameNanos, the clock animations run on. - with_
worker - Runs
workon a worker thread and resolves with its result on the UI thread. Compose’swithContext(Dispatchers.IO): the way domain work leaves the UI thread.
Attribute Macros§
- composable
- Turns a function into a composable.