--- topic: component-library-in-the-client triggers: [mithril_bevy, lib_keyboard.rs, systems::lib_keyboard, KeyEdits, data-keep-focus, KeyboardInset, LibCamera, follow_world_target, ellipsis.rs, images.rs, app_tokens, lib_chats.rs, systems::lib_chats, lib_search.rs, systems::lib_search, SearchView, bridge_input, overlay_owns_keyboard, crates/mithril_bevy, lib_settings.rs, systems::lib_settings, ui_probe.rs, CARTO_UI_LIB, CARTO_UI_PROBE, LibScreens, SettingsState::lib, "ui-probe", UiPointer, overlay_owns_pointer, FontMap, drive_ui, DriveUi, with_host, "replace_children", UiTransform] updated: 2026-11-28 --- # The component library is the client's interface `docs/mockups/component-library` is the study; this is what holds in the code. The library is `mithril` (the Rust edition in `jeroen/mithril`), drawn by `crates/mithril_bevy` on `bevy_ui` natively (feature `ui-probe`, a default and named in the APK's) and as DOM on the web (`ui-dom`). Every screen is a `systems::lib_*` module; `LibScreens` turns them all on whenever the build has a backend. **egui left the client on 2026-11-26** (screens moved over from 2026-11-24, the bridge deleted last); the sections below keep, dated, what the move taught. Input and size questions are `ui-input.md`'s. Two traps every port met: a node is not laid out in the frame that makes it, so content rebuilt while it ticks is blank on every frame it is rebuilt — a ticking value goes into a signal that rewrites its existing text, and only a change of the rows themselves rebuilds them (`lib_dev_panel`'s readouts, `lib_call`'s timer: a capture of either was blank or empty while they rebuilt whole, and a throttle like `READOUT_PERIOD_S` only made it rarer); and the host's dark switch is one for every screen, so a screen dark in both themes draws in `theme::DARK`'s colours rather than mounting dark (`lib_camera`). ## One frame of the library `mithril_bevy::drive_ui` runs in `PostUpdate` (set `DriveUi`): it hands the frame's pointer and keyboard events to the tree's handlers, drains the operations the tree queued (a signal write anywhere → an effect → operations), applies them to `bevy_ui` entities, restyles what changed through the library's cascade (`mithril::style::Stylist`, the same `css!` rules `gen-css` writes for the browser) and syncs text inputs. The host (renderer, mounted trees, handlers) is **thread-local** — signals and handlers are `!Send`, and Bevy 0.20 has no non-send `World` resources — so a system that mounts takes `NonSendMarker` and reaches it with `mithril_bevy::with_host`. **A node is marked one of two ways** (`tree.rs`, 2026-11-26): `dirty` when the view changed it (an op), which always writes its components, and `restyle` when only something its selectors read changed (a hover, the focus, an ancestor's class, the window), which writes only if its style, opacity or states come out different. A focus change reaches every node through `:focus-within`; when every restyled node was written, a focus change on a 200-field form rewrote 801 of 802 nodes in 9.4 ms (and Bevy then laid out and shaped every text); now it writes 3, in 1.1 ms, and an idle frame touches nothing (`tree::tests`, Mac node). Something new that `write_components` reads must either come through an op (→ `dirty`) or be part of that comparison, or a restyle will skip writing it. Tokens: the plugin takes `theme::tokens()`, cartopolis' own `Tokens` (`theme/tokens.rs`) — the one table `web_theme.rs` writes the web's `theme.css` from, so the app's library screens and the site share a palette by construction. Change a colour there and both move; the web theme test fails until `theme.css` is regenerated. Fonts: the cascade's families map onto `FontMap` (`regular`, `semibold` from weight ≥ 550, `mono`, and `icons` for a family naming "Symbols"), so an icon is a glyph of `icons::FONT` in a span with `font-family: 'Material Symbols Rounded'`, on the web the same with an `@font-face`. ## Two silent ways to a 0×0 tree (Bevy 0.20) Both left every node under the root at size zero at (0, 0), with every style computed correctly, and nothing in any log. `MITHRIL_BEVY_DUMP=1` logs the tree with each node's size; that is how they were found. - **Never `remove::` from a node.** `ui_layout_system`'s node query requires it; a node without it ends the layout walk, and its whole subtree stays unlaid out. Write `UiTransform::IDENTITY` instead. - **Never `remove::()` + `add_children()` in one frame.** The layout syncs changed child lists and then clears every parent whose `Children` was *removed* that frame (`RemovedComponents`), so the parent ends the frame childless in the layout tree, and nothing re-syncs it until its children change again. `replace_children(&all)` edits in place. ## The shape of a screen Settings was the first (`systems::lib_settings`, 2026-11-24); every screen since has the same shape: - **The screen's system owns its life.** `PostUpdate` before `DriveUi`: `RightPanel::Settings` appears → mount; disappears → clear; its ✕ sets a `Cell` the system reads and clears `RightPanel`. Each screen mounts under its own key (`lib_host::mount_keyed`), so a card over the map and a panel stand side by side and clear independently. - **State is bridged by a frame-by-frame diff**, not by callbacks: a signal whose value moved since the last frame writes the preference; a preference that moved writes the signal; `AppPrefs` is assigned only when something differs, because its `is_changed()` gates re-run work. `DataLayers` (`lib_layers`) goes further: written through `bypass_change_detection` and `set_changed` only when a field moved, because its `is_changed` restarts streaming work. - **The logic lives on the state, not in the view.** What Search's field and rows do is `GeoNavState`'s (`query_edited`, `submit_query`, `search_list`, …); a screen whose actions need the camera or the store publishes a view (`lib_view`) and runs the action the library hands back. - **A panel that takes room from the map marks its root `data-reserves-map`** (`ui-input.md`), and the pointer and keyboard verdicts reach the world through `lib_host::bridge_input` → `UiInput`. A field takes the keyboard on mount with `autofocus` (the renderer action `@focus`), only when asked (Ctrl+L, `--ui search`), so a tap on the phone does not throw the keyboard over the recents. **A tap focuses a field in two places, and both are needed** (fixed 2026-11-28, when no tap could focus any field and the phone's keys never rose): the library's own press observer (`events.rs`) sets the focus on a text input as on any control, and the field carries a `TabIndex` (`tree.rs`) — because Bevy 0.20's press then asks for the focus with a bubbling `AcquireFocus` that only a `TabIndex` answers, and one nobody answers reaches the window, which clears the focus. That request needs a `PrimaryWindow`, so a headless run never sends it; `@focus` and the MCP's `set_text` set the focus directly and never showed the fault. The MCP's `press` taps once, on the topmost thing under the point, as a finger does. Captures: `--shots docs/phone-ui-shots.toml --only settings` (`CARTO_UI_LIB_PAGE=general|graphics|world` for a page, `&page=` on the web), and `CARTO_UI_LIB_HTML=` writes the panel as HTML for the browser beside it. ## The same screen as DOM, in the web build On the web the client is only the world inside the site's frame (`web-client-is-the-world.md`), and its chrome should look like the site. `systems::lib_host` is the one seam: a screen's system calls `mount` and `clear`, and the lane decides — `ui-probe` draws through `mithril_bevy`, `ui-dom` (wasm) through `mithril::dom` into a full-window `
` over the canvas, `pointer-events: none` except where a screen is, with the library's stylesheets written into a `