EngineState #
Defined in crates/core/src/ui/interaction.rs.
pub(crate) trait EngineState {
fn reconcile(&mut self, ui_tree: &UITree, layout: &ComputedLayout) -> bool;
}
EngineState is implemented by state the engine owns for the whole tree that
refers to UI tree nodes, such as focus and pointer bookkeeping. Only engine
state implements it. A feature's state, whether kept per node or for the
whole tree, is the feature's own; see Who owns state.
Who owns state #
Every piece of state has one owner, and the owner decides where it is stored and who may change it. How many nodes it is kept for is a second question, asked only once the owner is known.
| Owner | Owns | Per node | Per tree |
|---|---|---|---|
| Engine | what its pipeline needs to run, whatever is registered, and the arbitration of resources features share | Node: element, attributes, style, children, revisions |
UITree's structure and revisions, ComputedLayout, the scene plan, and Interaction |
| Element type | what its elements are | ElementInstance |
nothing: a definition keeps no state |
| Feature | what a feature does to nodes of any type | a feature slot | a tree feature |
State is the engine's when any of these holds:
- A pipeline stage reads it to do its own job. Layout reads style, the scene reads layout, hit testing reads the scene, and dispatch reads hover, capture, and focus.
- Features compete for it, and one holds it at a time. Focus, pointer capture, and the pointer grab: the engine decides who holds them, and routes events by them.
- A standard the engine implements defines it. DOM focus and pointer capture.
Otherwise it is the feature's: it exists because the feature does, and without the feature registered, the engine works the same for everything else.
Derived state is stored by no one. What can be computed again from other
state, such as a scroll container's thumbs or a node's position as shown, is
computed when needed by whoever owns its inputs. A pipeline stage's output,
such as ComputedLayout, is that stage's.
Access #
| Engine state | Feature state | |
|---|---|---|
| Stored in | engine types: Node, UITree, ComputedLayout, Interaction |
the feature's own types, such as its slot type or its tree feature type |
| The engine reads it | directly | only through the feature's hooks, never by naming its type |
| Changed by | the engine; a feature asks through its give, as a slot does with request_layout or SlotGive::grab_pointer, and a tree feature with request_measure |
the feature, in its hooks or through with_slot and with_tree_feature |
| Registered | never: it is the engine | by the feature's extension, through Registry |
A feature keeps no copy of engine state to read later instead of asking: a slot uses the layout its hook is given rather than keeping the last one.
A resource features share splits along the boundary. The engine holds who
has it; the feature holds what it means. For the pointer grab,
Interaction's PointerGrab names the node and slot type holding the
pointer, by rule 2, and the scroll slot's ThumbDrag holds which thumb is
dragged and where the press landed on it.
Checks #
- No engine module names a feature's type, field, or module.
- A feature uses only public API.
- Engine state changes only in engine code. A feature's request goes through its hook's context and applies once the hook returns.
- What the engine asks the host to do goes through one queue,
HostRequest; see host_requests.md. No new field, shared cell, or returned tuple carries a request to the host.
docs/plan/feature-slot-events.md designs the API that lets every feature
follow these: tree features, read-only access to the tree, and references to
other nodes. Scroll and the resize observer follow them: each is a built-in
extension on the public API alone.
Known debt #
Code that breaks the rule today. Each entry is paid by the step of
docs/plan/feature-slot-events.md's Order named after it; new code adds no
entries without a reason recorded here.
| Where | What breaks the rule | Paid by |
|---|---|---|
| none | Step 6 paid the last entries: the resize observer's registry in UITree and ApplicationHost, and its slot type's crate-private registration. |
Why this state is outside the tree #
The UITree revision drives layout: every bump forces a reconcile and layout
pass. State that changes only what is painted must not bump it. Otherwise, for
example, every wheel tick would re-run layout for nothing.
Rule of thumb, for engine state: state that changes layout belongs in the
UITree's nodes; state that changes at most what is painted implements
EngineState. A feature slot sits outside both: changing one does not bump
the revision either, unless the slot requests layout, as one whose style
changes what it sets does.
Contract #
The state holds its nodes as weak NodeRefs under the fixed holder
EngineHolder::Interaction, made with interaction::watch, never as
NodeIds: whoever stores a node across events holds a NodeRef. A freed
node's reference reads as None through UITree::get, which is the right
answer for focus, hover, the pressed target, capture, and the grab, so
freeing needs no cleanup and the engine hears nothing when a node goes; see
feature_slots.md.
The state is kept valid at one point:
reconcileruns after every successful layout. It re-validates the state against the new layout, for example by clearing focus when the focused element is no longer rendered. It returnstruewhen the state changed and the next frame must repaint.
State that only a connected node can hold, such as pointer capture, is released when script detaches the node; see lifecycle.md.
reconcile cannot request another layout. It runs after layout, so doing so
would mean a second layout pass per frame, and possibly a loop. This is why
the trait is paint-only today. See todo.md for the open
question of where an <input> value will live; that value is the first planned
state that changes layout.
Callers #
| Method | Called from |
|---|---|
reconcile |
UITreeBindingState::reconcile_interaction (ui/js_bindings.rs), after layout in ui/host/render.rs |
It reaches the implementors through Interaction, which
implements EngineState by forwarding to each of its parts.
Implementors #
| Implementor | reconcile |
A freed node |
|---|---|---|
PointerState |
no-op; nothing about the pointer is painted yet | its pressed target, capture, and hover entries read as None; after_mutation releases capture held by a node script detached |
FocusState |
clears focus when the element is no longer focusable, laid out or not, without blur events (like HTML's focus fixup rule), asking for keyboard focus back when it was a native overlay; the host turn does the same for an element no longer connected, or no longer focusable in the presented layout |
reads as no focus |
Interaction |
forwards to all of the above; bumps its revision on change | a pointer grab on it ends at the next pointer move, telling no slot |
Implementing it #
First decide the owner, by the three tests in Who owns state. Then:
- Engine state for the whole tree, like focus: implement
EngineStatedirectly, as a part ofInteraction. - Feature state per node, like scroll offsets: implement
FeatureSlot, neverEngineState. The tree stores it, drops it with its node, and runs itslaid_outafter each layout. - Feature state for the whole tree: not a part of
Interactionor a field ofUITree. Tree features, whichdocs/plan/feature-slot-events.mddesigns, will hold it; until they exist, it waits for them or is added to Known debt with its reason.
Combining several states #
When one state owns others, as Interaction does, its reconcile must call
every part's reconcile and combine the results with |, not ||.
|| short-circuits: once the left side is true, the right side is never
evaluated.
// Wrong: if `pointer` reports a change, `focus` never runs.
let changed = self.pointer.reconcile(ui_tree, layout) || self.focus.reconcile(ui_tree, layout);
On bool, | always evaluates both sides, and the result is still true if
any part returned true:
// Right: every part sees the new layout.
let changed = self.pointer.reconcile(ui_tree, layout) | self.focus.reconcile(ui_tree, layout);
reconcile is not just a question ("did anything change?"). It does work: it
clears and drops state. With ||, a skipped part would keep stale state, such
as focus on an element that is no longer rendered.