# `Interaction` Defined in `crates/core/src/ui/interaction.rs`. `Interaction` holds all engine-owned interaction state for one UI tree. It is the interaction state of this design; there is no separate `InteractionState` type. It is a field of the tree, next to the presented and the last layout, so that a slot's [`TreeView`](../ui-tree/feature_slots.md#reading-the-tree) reads all of them in every event it hears: ```text UITree ├── nodes, revisions, feature slots, ... ├── presented_layout the layout of the frame last shown ├── last_layout the newest layout computed └── interaction: Interaction ├── pointer: PointerState hover path, capture, pressed target ├── focus: FocusState ├── selection: SelectionState the text selected, inside one user-select: contain element ├── grab: Option the feature slot holding the engine's drag └── revision: u64 bumped by every paint-level change ``` Code that changes it while reading the tree, such as `reconcile` or moving focus, runs inside `UITree::with_interaction`, which takes it out of the tree for the call and puts it back after. No slot hears an event inside that call: the slots hear events from `UITree` methods, which read `interaction` in place. JavaScript reads and writes this state through bindings such as `scrollTop`, `scrollTo()`, `focus()`, and `app.getSelection()`, but does not own it. Changing it does not bump the UI tree revision, so it never triggers layout. It implements [`EngineState`](engine_state.md) by forwarding to each part. Every part holds its nodes as weak `NodeRef`s under `EngineHolder::Interaction`, so a node reclamation frees reads as `None` and nothing has to forget it; see [lifecycle.md](../ui-tree/lifecycle.md). Scroll offsets are not in it: each scroll container keeps its offset in a [feature slot](../ui-tree/feature_slots.md), which the tree drops with the container, and which also paints its thumbs and handles its input. Reading an offset therefore takes the tree, and scrolling takes it mutably, so the [dispatch pipeline](#dispatch-pipeline) does too. A slot that scrolls requests paint, which advances the tree's paint revision. ## Revision `revision()` is bumped by every change the next frame must paint, through the private `invalidate_paint()`. `frame_revision(tree_revision)` returns `tree_revision + revision`, where `tree_revision` is `UITree::frame_revision`: the tree's layout revision plus its paint revision, which style changes that only repaint advance. All of these only ever advance by one per change, so the sum advances whenever any does. Frames therefore keep comparing a single `u64`, while layout stays keyed on the tree's layout revision alone. The host requests a frame when the interaction revision or the tree's paint revision changes (`paint_request_redraw_if_needed`), since neither moves the layout revision that window sync watches, and when an element asked for a frame with `request_frame`. Attributes of Burokku's own elements move neither tree revision: window sync also compares the window's specification, such as its `title`, with the one it last reconciled. An element with an instance may invalidate more as it hears `AttributeChanged`. ## Dispatch pipeline Everything that changes this state goes through one function, `js_bindings::events::pipeline::dispatch`, which takes an `Entry`: | Entry | What it carries | When it runs | |---|---|---| | `Input` | native mouse input, a pointer cancel, a key with what the platform made of it, or a command, text, or composition change of its own; see [The input queue](#the-input-queue) | on the host's turn | | `Script` | `focus()`, `blur()`, `dispatchEvent()`, `element::reveal`, which scroll's `scrollIntoView()` calls, and the `Selection` setters | inside the call, to the end before it returns | | `Handle` | an `ElementHandle`'s focus requests and the events it fires | on the host's turn | `DispatchStep` (`ui/dispatch_step.rs`) is the chain everything that reacts to an event steps through: `take` reads, `action` acts with nothing of the engine borrowed, and `give` writes, only while the stepper's guard holds. It has three steppers: `Slot`, a feature slot's; `Feature` (`ui/ui_tree/tree_feature/step.rs`), a [tree feature](../ui-tree/tree_features.md)'s, whose `give` may stop or consume input in the Capture stage; and `Pipeline`, the engine's own. The `take` of a slot and of a feature reads the tree through a read-only [`TreeView`](../ui-tree/feature_slots.md#reading-the-tree). Each input is one chain over the `Pipeline` stepper, whose `take` and `give` borrow the engine's state and whose `action` runs script with nothing borrowed: 1. **Capture**, in `take`. The tree features hear the input first, in registration order, until one stops it; one that consumes it ends the chain here, before hover changes. A consumed press clicks nothing, and once the primary button is up, a slot's pointer grab ends. See [Capture](../ui-tree/tree_features.md#capture). 2. **Route**, in `take`. The pointer grab hears raw input first; see [Pointer grab](#pointer-grab). Then hover, pointer capture, and click synthesis plan what script hears, and the focused element is the target of a key. A release that ends a press over nothing of the app, as outside the window, goes to the element the press went to, and clicks nothing. A move that hits nothing, as one outside the window does, goes to the element the primary press under way began on while its button is held, so that a drag keeps hearing `pointermove` and extending the selection. Route makes a `Route`: `Hover`, over a native overlay's view while the engine does not own the pointer, which only fires the boundary events of the hover change; or `Full`, with the target, the boxes whose slots hear the input, the grab holder, and what script hears. While script holds pointer capture, the hover path stays where the press began; the release that ends the capture fires `lostpointercapture`, then the boundary events to where the pointer is, as Pointer Events' implicit release of capture has them. 3. **Slots before script**, outermost first, in `take`. 4. **Listeners**, in `action`: the boundary events, then, unless a slot stopped the input before script, its own event and the `click` it makes. 5. **Defaults**, in `give`, unless a listener canceled the event, then the **slots after script**, nearest first. 6. **Follow-ups**, each a chain of its own, in turn: a release that leaves no button held ends the app's press when no `pointerup` did, as when a tree feature consumed it, and the app hears `lostpointercapture` for a capture it held; the slots whose pointer grab the input ended hear `PointerGrabLost`; focus moving brings a node into view, then `blur`, `focusout`, `focus`, and `focusin`. The defaults are the engine's own and fixed: | Event | Default | |---|---| | primary `pointerdown` | focus the nearest focusable element at or around the target, or clear focus; start a text selection at the pointer inside the `user-select: contain` element around the target, or clear the selection when there is none. The second press in quick succession at the same place selects the word there, the third the paragraph, timed by when the host heard each press, not when it is dispatched; with Shift, the press moves the selection's focus instead | | `pointermove` with the primary button held | extend the text selection a press started to the pointer | | `keydown` | none of its own: what a key does arrived as the command the platform bound it to, which follows the key unless a listener canceled it | | `FocusNext` command, Tab and Shift+Tab on every platform | move focus sequentially and bring it into view, selecting all the text of an editable element it moves into, as browsers do for a text field; a Tab the platform binds to nothing, as with Control, Alt, or Meta on macOS, sends no command and is the application's | | `SelectAll` command, Command+A on macOS | select all the text of the focused editable element, such as `input`, wherever the selection is; otherwise in the element the selection is in, or, with no selection, in the `user-select: contain` element around the focused element | | `Copy` command, Command+C on macOS | copy the selected text to the clipboard as plain text, each paragraph's part on a line of its own; the host writes it at the end of its turn | | `Move` command, the arrows, Home, End, and their Option and Command forms on macOS | move the selection's focus one unit that way, by character, word, line, paragraph, or to the document's end, where a character is a whole grapheme, such as a letter with its accents, a flag, or an emoji with its modifiers, and the anchor with it unless Shift extends; Left or Right without Shift collapses a selection to its start or end instead; a character or a word crosses into the paragraph beside; up and down keep the column the focus was last put at. A move to a paragraph's start or end, as Control+A and Control+E make, stays there, while a move by a paragraph, as Option+Shift+Up and Down make, goes on from there to the paragraph beside. Nothing with no selection | | text the platform committed, typed or pasted | at the focused editable element only, without its line breaks in a single-line one such as `input`: `beforeinput` with `inputType` `insertText` or `insertFromPaste` and the text as `data`, then, unless canceled, the text replaces the selection, or the range the platform named, the caret lands after it, and `input` fires with the same `detail` | | `DeleteBackward` and `DeleteForward` commands, Backspace, Delete, and their Option and Command forms | at the focused editable element only: `beforeinput` with the DOM's `deleteContent…`, `deleteWord…`, or `deleteSoftLine…` type, then the selection goes, or else one unit from the caret, a character being a whole grapheme as for `Move`, and `input` fires. Nothing at the text's edge | | `Cut` command | at the focused editable element only: `beforeinput` with `deleteByCut`, then the selection goes to the clipboard and from the text, and `input` fires | | `Paste` command | the clipboard's text, read when the paste is dispatched, goes in as a paste, so it is the text row above; it pastes what a copy or cut before it in the queue put there, though the host writes that only at the end of its turn | | a composition change from the IME | at the focused editable element only, and nothing cancels it, as Input Events Level 2 has it: script hears `compositionstart` the first time, then `beforeinput` with `insertCompositionText` and the text as `data`, which cannot be canceled, and `compositionupdate` with the text; the text replaces the marked text, or opens a composition at the selection, the selection follows the IME's caret inside it, and `input` fires with the same `detail`, in UI Events' order. Empty text clears the marked text and ends the composition with `compositionend` after `input`. Committed text in place of a composition ends it the same way and always lands: `beforeinput`, `compositionupdate`, and `input` carry it, then `compositionend`. A paste is no commit: it hears `insertFromPaste` as anywhere else, and unless canceled ends the composition with `compositionend` carrying the composed text, which stays, before it goes in at the selection and `input` fires. Focus leaving the element ends the composition with its text kept: `compositionend` with the marked text as `data` fires there before `blur` and `focusout` | | every other command | nothing yet: line breaks, undo, and redo wait | An editable element, which its type declares with `editable`, is the one `user-select: contain` container of its own raw text, by its style type's default, as `input`'s is, so that removing the property keeps it one. The one text selection serves as its caret: the scene paints a collapsed selection in it as a bar while it has focus. After every pipeline entry the engine tells the platform's text system what changed for it, through `HostRequest::SetTextInputContext`: the text around the selection, the selection in UTF-16 units, the caret's box, and, while a composition is open, its marked range and one box per UTF-16 unit of it, which the platform answers its text system from. The scene underlines the marked text while the element has focus. Focus leaving ends the composition, keeping its text as it is, and so does the native window closing or another replacing it, since the text system that held it goes with the window. The host also chooses the pointer's cursor at the end of its turn, from the hover path: the text cursor over a `text` element that a `user-select: contain` element makes selectable, the arrow elsewhere, through `Window::set_cursor`. ### The input queue Native input waits in `InputQueue` (`js_bindings/events/queue.rs`), in front of the JavaScript task queue, and each input runs as a task of its own, oldest first: the task runs one input and queues itself again while more wait, so a listener's microtasks run before the next input, as in browsers. Within an input, a microtask checkpoint also follows each listener the engine calls, as HTML's "clean up after running script" does after a callback made with no script on the stack: a `pointerup` listener's promise reactions run before the `click` listeners. A chain script enters, such as `dispatchEvent` or `focus()`, has no checkpoint between its listeners; their microtasks wait for the script that called it, as in browsers (`UITreeBindingState::listener_checkpoints`). Nothing is dropped: a full task queue is waited on. The rule for two inputs of a kind in one turn is on each `Input` variant: a move or hover replaces the move or hover before it with the same buttons held, and a move it replaces stays a move at the newer place, so that the hover the host queues after a frame never takes a move's `pointermove` away; a wheel adds its deltas to the wheel before it in the same gesture phase, and everything else keeps its place. A key owns the text, composition change, and command the platform made of it in the same native callback, as `Input::Key { follows }`: they run after the key, each a chain of its own, unless `keydown` was canceled, a tree feature consumed the key, or a slot stopped it before script, when the text and the command are dropped and only the IME goes on, a composition change and the text that commits an open composition, as an IME does in browsers. The host closes the key at the start of each turn. Everything else a feature does with input it does in its [feature slot](../ui-tree/feature_slots.md#events), through `FeatureSlot::event_dispatch`: the scroll slot scrolls on `wheel` and keys after script, and pages the track or starts a thumb drag on a primary press before script, which it stops. It holds the pointer grab until the release, and stops the moves and the release too, so that the app never hears a scrollbar press, as it never hears a native one. A key at an editable element skips the slots after script: its command, the text or the caret move, is all it does, so Space and the arrows typed into an input never scroll around it. A slot pass offers the event to the slots of its target, then of each box around it through layout parents, and each node's slots in registration order. A slot may stop the event, share state with the slots after it, or take the pointer grab. A slot that stops an event before script keeps it from the listeners, the defaults, and the pass after script. A press it stopped then clicks nothing on release, and the pointer never becomes active for the app: `setPointerCapture` throws `InvalidStateError` until the press ends. ### The guard `take` records what Route read to pick the target, and `give` runs only while all of it still holds once the listeners return: | Read | When | |---|---| | the presented layout the input was hit-tested against | always | | the target and the boxes whose slots hear the input, which must stay connected | for a `Full` route | | the slot holding the pointer grab, after the slots before script | always | | the element script captured the pointer to, or none after the release that ends the press, which ends the capture itself | when the capture chose the target | | the focused element | for a key, a command, or text that commits no composition, whose target it is | A listener that changes any of these cuts the defaults and the pass after script off. So a `keydown` listener that moves focus keeps Tab from moving it again, and a `pointerdown` listener that removes its target keeps the press from moving focus; a `pointerdown` listener that calls `focus()` does not cut the press's own focus default off, since focus did not pick a pointer's target. Nor does a listener that moves focus cut off a composition change or the text that commits one, which nothing cancels: the change lands where it was composed, and the move ends the composition only once it has. ### Focus Every focus change, from a press, Tab, `focus()`, `blur()`, or a handle, is one chain, `focus_chain`, with `FocusOptions` for what differs: whether the element that gains focus is brought into view, whether its instance is told, so that a native view of its own takes keyboard focus, and what an editable element that gains it does to the selection. Its `take` plans who loses focus and who gains it; its `give` moves focus and, when focus leaves a native overlay for an element that is not one, asks the window to take keyboard focus back from the overlay's view, which the host does at the end of its turn. The host also clears focus from an element that can no longer take it, as the HTML focus fixup rule does: one no longer connected, or no longer focusable in the presented layout, as one whose `tabindex` was removed. It asks for keyboard focus back when the element was an overlay. | From | Reveals | Tells the instance | An editable element's selection | |---|---|---|---| | a primary press | no | yes | kept: the press places the caret | | Tab | yes | yes | all its text | | `focus()` | unless `preventScroll` | yes | a caret at its text's end, unless the selection is in it already: where `selectionStart` and the text system read it | | `blur()` | no | no element gains focus | kept | | `HandleRequest::Focused`, a native view that took keyboard focus itself | no | no: it has focus already | kept | | `HandleRequest::FocusNext`, Tab out of a native view | yes | yes | all its text | ### Script calls `focus()`, `blur()`, `scrollIntoView()`, and `dispatchEvent()` run their chain nested, from inside a listener or any other script, and complete before they return, as browsers do: `focus()` fires `blur` and `focus` first, and `dispatchEvent(event)` runs the listeners of a `CustomEvent` script made with `new CustomEvent(type, { detail, bubbles, cancelable })`, which get the event itself, then returns whether none canceled it. Its path is the node, then, when it bubbles, its ancestors, connected or not. An event whose listeners are running throws `InvalidStateError`. An error of the engine's own, such as a failed borrow, ends the dispatch; a listener that throws does not. ## Pointer grab `UITree::route_pointer_grab` runs on raw mouse input in Route, *before* any dispatch. A grab is a drag a feature slot took with `grab_pointer` while hearing `SlotEvent::PointerDown`, in either pass, which the engine owns until the pointer is released or canceled. `PointerGrab` names the node, the slot type that holds it, and the pass it was taken in: - it follows the pointer anywhere, including over nothing or outside the window, where no pointer event is dispatched but a held press's moves: while the primary button is held, the slot hears `SlotEvent::PointerMove` before script, and it hears `SlotEvent::PointerUp` on release; - it ends on that release, on another press, on `pointercancel`, or once the node has no box, and the slot hears `SlotEvent::PointerGrabLost` once that input is done, unless it took the grab again in it; - while it lasts, the pointer's moves and releases go to the holder alone: the slot passes skip them, though listeners still hear them unless the holder stops them; - JavaScript pointer capture neither sees nor releases it, and the two coexist, each routing its own side; - a holder that took the grab after script and stops a move or release of a pointer script captured makes the engine release that capture first, so that the app hears `lostpointercapture`; - once it runs, listeners cannot stop it. ## Other state - **Revealing:** `reveal`, which the pipeline calls as a follow-up of `element::reveal`, `focus()`, and Tab, offers a node to the slots around it, which scroll it into view, and moves the target by each node's `content_offset` change. The scene's `content_offset` and `placement` read those offsets from the tree's slots. Scroll itself is the `burokku-scroll` extension, in `features/scroll.rs`, which fires its own `scroll` events. - **Focus:** `focused`, `set_focus`, and the lookups the focus chain plans with: `can_focus`, `pressed_focus`, and `next_in_tab_order`. The focused element receives keyboard events; with nothing focused, they go to the window. A native overlay hands keyboard focus to and from its own view through the focus chain; see [Focus](#focus) and [elements.md](../ui-tree/elements.md). - **Pointer:** the public `pointer` field plans hover, pointer, and mouse events from native input. Over a native overlay's view, input only hovers the element, unless a press that began in the engine or a capture still owns the pointer; see [elements.md](../ui-tree/elements.md). Input is hit-tested against the presented frame, which may still show a node that script removed since: such a hit goes to the nearest box around it in the presented layout that is still connected, so the pointer leaves only the removed subtree. A pointer capture whose element leaves the tree ends, and the next pointer input announces it with `lostpointercapture` at the app root, as the DOM does at the document. A `pointercancel` ends the press for the app: its release, and its moves while a button is held, fire no pointer events until every button is up. ## Adding new interaction state 1. Decide who owns it, by the three tests in [Who owns state](engine_state.md#who-owns-state). Only engine state goes on to the steps below. - A feature's state kept per node is a [`FeatureSlot`](../ui-tree/feature_slots.md), registered by the feature's extension, as scrolling's is. Its `event_dispatch` hears input in the slot passes, and requests paint through its `give`; nothing here changes. - A feature's state for the whole tree is never a field of `Interaction` or `UITree`, and never a default of the pipeline. It is a [tree feature](../ui-tree/tree_features.md), registered by the feature's extension, as the resize observer's is. 2. Implement [`EngineState`](engine_state.md) for it, and add it as a field of `Interaction`. 3. Forward to it from `Interaction`'s `reconcile`, and hold its nodes as `NodeRef`s made with `interaction::watch`, never as `NodeId`s. 4. Call `invalidate_paint()` whenever it changes outside `reconcile`. 5. If it reacts to input, it is a feature: give it a feature slot, or, for state of the whole tree, a tree feature, which hears input in Capture. The pipeline's defaults stay the engine's own focus.