diff --git a/crates/ui-egui/src/resources_updater.rs b/crates/ui-egui/src/resources_updater.rs index 95840c26..21cf648b 100644 --- a/crates/ui-egui/src/resources_updater.rs +++ b/crates/ui-egui/src/resources_updater.rs @@ -254,7 +254,10 @@ pub(crate) fn show(app: &mut TextureCraftApp, ctx: &egui::Context) { ui.label(tr("No resources match the filters")); } for (index, row) in visible.into_iter().enumerate() { - if let Some(asked) = row_ui(app, ui, &tokens, index + 1, row) { + // Ids from the name alone, so the keyboard focus stays with its resource when a check reorders the rows + let row_scope = egui::UiBuilder::new().scope_id(ui.scope_id().with(("resources_updater_row", &row.name))); + let asked = ui.scope_builder(row_scope, |ui| row_ui(app, ui, &tokens, index + 1, row)).inner; + if let Some(asked) = asked { request = Some(asked); } } diff --git a/crates/ui-egui/tests/it/resources_updater.rs b/crates/ui-egui/tests/it/resources_updater.rs index 980102a9..20609aec 100644 --- a/crates/ui-egui/tests/it/resources_updater.rs +++ b/crates/ui-egui/tests/it/resources_updater.rs @@ -186,3 +186,27 @@ fn a_file_changed_while_the_window_stays_open_shows_without_refresh() { assert!(shows(&h, "(1 resources, 1 outdated)"), "no document change, no focus change and no Refresh"); } + +#[test] +fn the_keyboard_focus_stays_with_its_resource_when_the_rows_reorder() { + /// Steps of the harness's quarter second: over the window's half-second check + const FRAMES: usize = 4; + const SELECT_LATER: &str = "Select new resource zz_later.png"; + + let dir = TempDir::new("resources-updater-focus"); + let (earlier, later) = (png(&dir, "aa_earlier.png", 4), png(&dir, "zz_later.png", 4)); + let mut h = app(); + import(&mut h, &earlier); + import(&mut h, &later); + activate(&mut h, "Resources Updater"); + h.get_by_role_and_label(Role::Button, SELECT_LATER).focus(); + h.run_steps(2); + + // The changed file moves to the top of the list + png(&dir, "zz_later.png", 8); + h.run_steps(FRAMES); + + let rows = &h.state().ui.windows.resources_updater.as_ref().unwrap().rows; + assert_eq!(rows[0].name, "zz_later.png", "the outdated resource moved up"); + assert!(h.get_by_role_and_label(Role::Button, SELECT_LATER).accesskit_node().is_focused(), "the focus moved with it"); +}