git clone https://garage44.eu/jeroen/cartopolis.gitcartopolis-agent · 2026-11-28T20:49:43Z
fix(mithril_bevy): a tap on a text field focuses it, so the phone's keys rise No tap could focus any library text field, so the on-screen keyboard, which rises with a focused field, never appeared: tapping the search field on the phone did nothing. The library's press observer skipped text inputs, trusting Bevy to focus them. Bevy 0.20 does that only through PointerFocusPlugin: a press sends a bubbling AcquireFocus that only an entity with a TabIndex answers. The fields had none, so the request reached the window, which clears the focus. The observer now focuses a text input as it does any control, and every text input gets a TabIndex(0) so Bevy's own request keeps that focus instead of clearing it. Why tests never caught it: AcquireFocus needs a PrimaryWindow, so a headless run never sends it; and the MCP's set_text and the @focus action set the focus directly. The MCP's press also fired the tap separately on every element under the point, so each ancestor's press took the focus off the field again. It now taps once, on the topmost hit, as a finger does. Checked headless on mac-2 with CARTO_OSK=1: tapping the search field raises the keys, and typing g-r-o lists Groningen. Not yet tried on a handset.
crates/mithril_bevy/src/access.rs | 95 +++++++++++++++++++--------
crates/mithril_bevy/src/events.rs | 14 ++--
crates/mithril_bevy/src/tree.rs | 10 ++-
docs/notes/component-library-in-the-client.md | 14 +++-
4 files changed, 93 insertions(+), 40 deletions(-)
diff --git a/crates/mithril_bevy/src/access.rs b/crates/mithril_bevy/src/access.rs
index 2b9c516f..bc31b83f 100644
--- a/crates/mithril_bevy/src/access.rs
+++ b/crates/mithril_bevy/src/access.rs
@@ -665,9 +665,11 @@ pub enum PressError {
const PRESS_POINTER: PointerId = PointerId::Touch(u64::MAX - 7);
/// Presses `node` at `at` (logical px) as a finger would: down, up, and the
-/// click, triggered on whatever the point hits, so the events bubble to the
-/// node's observers as a real pointer's do. The handlers run in the next
-/// [`crate::drive_ui`].
+/// click, triggered once on the topmost thing the point hits within the
+/// node, so the events bubble to the node's observers as a real pointer's
+/// do. Triggered on every hit, each ancestor's own press asked for the
+/// focus again and cleared what a field had just taken. The handlers run
+/// in the next [`crate::drive_ui`].
pub fn press(world: &mut World, node: NodeId, at: Vec2) -> Result<(), PressError> {
let entity = world
.resource::<UiTree>()
@@ -685,9 +687,9 @@ pub fn press(world: &mut World, node: NodeId, at: Vec2) -> Result<(), PressError
} else {
vec![entity]
};
- if !hits.iter().any(|&h| within(world, h, entity)) {
+ let Some(top) = hits.into_iter().find(|&h| within(world, h, entity)) else {
return Err(PressError::Covered);
- }
+ };
let camera = world.get_resource::<UiCamera>().map(|c| c.0);
let window = world
.query_filtered::<Entity, With<bevy::window::PrimaryWindow>>()
@@ -707,30 +709,28 @@ pub fn press(world: &mut World, node: NodeId, at: Vec2) -> Result<(), PressError
position: at,
},
);
- for e in hits {
- let hit = HitData::new(camera.unwrap_or(Entity::PLACEHOLDER), 0.0, None, None);
- world.trigger(PointerPress {
- entity: e,
- pointer: pointer.clone(),
- button: PointerButton::Primary,
- hit: hit.clone(),
- count: 1,
- });
- world.trigger(PointerRelease {
- entity: e,
- pointer: pointer.clone(),
- button: PointerButton::Primary,
- hit: hit.clone(),
- });
- world.trigger(PointerClick {
- entity: e,
- pointer: pointer.clone(),
- button: PointerButton::Primary,
- hit,
- duration: Duration::from_millis(80),
- count: 1,
- });
- }
+ let hit = HitData::new(camera.unwrap_or(Entity::PLACEHOLDER), 0.0, None, None);
+ world.trigger(PointerPress {
+ entity: top,
+ pointer: pointer.clone(),
+ button: PointerButton::Primary,
+ hit: hit.clone(),
+ count: 1,
+ });
+ world.trigger(PointerRelease {
+ entity: top,
+ pointer: pointer.clone(),
+ button: PointerButton::Primary,
+ hit: hit.clone(),
+ });
+ world.trigger(PointerClick {
+ entity: top,
+ pointer,
+ button: PointerButton::Primary,
+ hit,
+ duration: Duration::from_millis(80),
+ count: 1,
+ });
Ok(())
}
@@ -983,4 +983,41 @@ mod tests {
let save = find(&nodes, "Save").unwrap();
assert!(!set_text(&mut world, save.node, "x"));
}
+
+ /// A press asks for the focus as `PointerFocusPlugin` does, with an
+ /// `AcquireFocus` on what it hit: a field answers it, anything else lets
+ /// it reach the window, which clears the focus. Fields that did not
+ /// answer it could be focused by no tap, and the phone's keyboard never
+ /// rose (2026-11-28).
+ #[test]
+ fn a_tap_on_a_field_focuses_it() {
+ use bevy::input_focus::tab_navigation::acquire_focus_tab_index;
+ use bevy::input_focus::{on_window_acquire_focus_clear, AcquireFocus};
+ use bevy::window::{PrimaryWindow, Window};
+
+ let (mut world, _) = mounted();
+ world.add_observer(acquire_focus_tab_index);
+ world.add_observer(on_window_acquire_focus_clear);
+ let window = world.spawn((Window::default(), PrimaryWindow)).id();
+ lay_out_rows(&mut world, 1.0);
+ let nodes = access_tree(&world);
+ let acquire = |world: &mut World, e: Entity| {
+ world.trigger(AcquireFocus {
+ focused_entity: e,
+ window,
+ });
+ world.flush();
+ };
+ let field = find(&nodes, "Your name").unwrap().entity;
+ acquire(&mut world, field);
+ assert_eq!(world.resource::<InputFocus>().get(), Some(field));
+ let save = find(&nodes, "Save").unwrap().entity;
+ acquire(&mut world, save);
+ assert_eq!(world.resource::<InputFocus>().get(), None);
+ // And the press itself focuses it, window or not (a headless run
+ // has none, and Bevy's request needs one).
+ let field = find(&nodes, "Your name").unwrap().clone();
+ press(&mut world, field.node, field.tap.unwrap()).unwrap();
+ assert_eq!(world.resource::<InputFocus>().get(), Some(field.entity));
+ }
}
diff --git a/crates/mithril_bevy/src/events.rs b/crates/mithril_bevy/src/events.rs
index c702fca7..2d5333fd 100644
--- a/crates/mithril_bevy/src/events.rs
+++ b/crates/mithril_bevy/src/events.rs
@@ -272,14 +272,14 @@ fn ensure_state_observers(world: &mut World, entity: Entity, id: NodeId) {
},
);
world.entity_mut(entity).observe(
- move |ev: On<PointerPress>,
- mut tree: ResMut<UiTree>,
- mut focus: ResMut<InputFocus>,
- inputs: Query<&bevy::ui_widgets::TextInput>| {
+ move |ev: On<PointerPress>, mut tree: ResMut<UiTree>, mut focus: ResMut<InputFocus>| {
set_state(&mut tree, id, |s| s.active = true);
- // A press on a control takes the focus; a text input does this
- // itself; an on-screen keyboard's key leaves it where it is.
- if inputs.get(ev.entity).is_err() && !keeps_focus(&tree, id) {
+ // A press on a control takes the focus, a text input's too;
+ // an on-screen keyboard's key leaves it where it is. Bevy's
+ // own request (`PointerFocusPlugin`) needs a window to run at
+ // all, and a field keeps what it is given only through its
+ // `TabIndex` (`tree.rs`).
+ if !keeps_focus(&tree, id) && focus.get() != Some(ev.entity) {
focus.set(ev.entity, bevy::input_focus::FocusCause::Pressed);
}
},
diff --git a/crates/mithril_bevy/src/tree.rs b/crates/mithril_bevy/src/tree.rs
index 25ffc0ae..06143564 100644
--- a/crates/mithril_bevy/src/tree.rs
+++ b/crates/mithril_bevy/src/tree.rs
@@ -5,6 +5,7 @@
use std::collections::{HashMap, HashSet};
+use bevy::input_focus::tab_navigation::TabIndex;
use bevy::input_focus::InputFocus;
use bevy::picking::events::PointerDrag;
use bevy::prelude::*;
@@ -1241,13 +1242,18 @@ fn write_components(
));
}
let mut em = world.entity_mut(e);
- // The text controls: a `TextInput` over an `EditableText`.
+ // The text controls: a `TextInput` over an `EditableText`. The
+ // `TabIndex` is what lets a tap focus it: a press asks for the focus
+ // with a bubbling `AcquireFocus`, which in Bevy 0.20 only a `TabIndex`
+ // answers (`acquire_focus_tab_index`); without one the request reaches
+ // the window, which clears the focus, and no field could be typed in
+ // by a finger or a mouse — nor raise the on-screen keyboard.
if text_input {
let value = props.get("value").cloned().unwrap_or_default();
if em.get::<TextInput>().is_none() {
let mut editable = EditableText::new(&value);
editable.allow_newlines = tag == "textarea";
- em.insert((TextInput, editable, TextLayout::no_wrap()));
+ em.insert((TextInput, editable, TextLayout::no_wrap(), TabIndex(0)));
}
em.insert(TextColor(colour(c.color, opacity)));
let tf = match fonts {
diff --git a/docs/notes/component-library-in-the-client.md b/docs/notes/component-library-in-the-client.md
index 2fdb53f8..ede868be 100644
--- a/docs/notes/component-library-in-the-client.md
+++ b/docs/notes/component-library-in-the-client.md
@@ -1,7 +1,7 @@
---
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-26
+updated: 2026-11-28
---
# The component library is the client's interface
@@ -101,7 +101,17 @@ since has the same shape:
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. Captures: `--shots
+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=<path>` writes the
panel as HTML for the browser beside it.