# `EngineState` Defined in `crates/core/src/ui/interaction.rs`. ```rust 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`](interaction.md) | | 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](../ui-tree/feature_slots.md) | a [tree feature](../ui-tree/tree_features.md) | State is the engine's when any of these holds: 1. **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. 2. **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. 3. **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](../host/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 `NodeRef`s under the fixed holder `EngineHolder::Interaction`, made with `interaction::watch`, never as `NodeId`s: 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](../ui-tree/feature_slots.md#references-to-other-nodes). The state is kept valid at one point: - **`reconcile`** runs 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 returns `true` when 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](../ui-tree/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`](../todo.md) for the open question of where an `` 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`](interaction.md), 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`](interaction.md) | 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 `EngineState` directly, as a part of `Interaction`. - Feature state per node, like scroll offsets: implement [`FeatureSlot`](../ui-tree/feature_slots.md), never `EngineState`. The tree stores it, drops it with its node, and runs its `laid_out` after each layout. - Feature state for the whole tree: not a part of `Interaction` or a field of `UITree`. Tree features, which `docs/plan/feature-slot-events.md` designs, 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. ```rust // 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`: ```rust // 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.