Don't lose keyboard focus in a pass that will be discarded (#8623)

* Follow-up to https://github.com/emilk/egui/pull/8621

`Ui::is_visible` is `false` for the rest of a pass once anything calls
`request_discard` (see `Context::will_discard`).
#8621 made invisible widgets not interested in focus, so a focused
widget drawn after any `request_discard` call would give up its focus,
and it stays lost because `Memory` persists across passes.

In Rerun this showed up as a focused list item losing focus whenever
some other part of the UI requested a discard.

This removes `w.visible` from `interested_in_focus`.
Invisible widgets are still hidden from accessibility, and their focus
is still not reported to AccessKit (the tree reports the root as focused
instead).

Added the regression test `keep_focus_in_discarded_pass`.

_This PR was written by Claude, an LLM, on behalf of @emilk._

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Emil ErnerfeldtandClaude Opus 5.5 authored and GitHub committed 2026-09-24 12:47:26 +02:00
1 parent a99dc715f1
commit 160090b20a
2 files changed
+32 -1

No files matched your search

-1
View File
@@ -1515,7 +1515,6 @@ impl Context {
debug_assert!(!w.rect.any_nan(), "widget rect is NaN: {:?}", w.rect);
let interested_in_focus = w.enabled
&& w.visible
&& w.sense.is_focusable()
&& self.memory(|mem| mem.allows_interaction(w.layer_id));
@@ -277,3 +277,35 @@ fn request_focus_during_sizing_pass() {
"The focus request should apply once the widget is visible"
);
}
/// Widgets are invisible in a pass that will be discarded (see `Context::will_discard`),
/// but that must not make them lose keyboard focus.
#[test]
fn keep_focus_in_discarded_pass() {
let ctx = egui::Context::default();
ctx.enable_accesskit();
ctx.options_mut(|options| options.max_passes = 2.try_into().unwrap());
let mut button_id = None;
let mut output = ctx.run_ui(Default::default(), |ui| {
let response = ui.button("button");
response.request_focus();
button_id = Some(response.id);
});
output.textures_delta.clear();
let button_id = button_id.unwrap();
let mut num_passes = 0;
let output = ctx.run_ui(Default::default(), |ui| {
num_passes += 1;
if ui.current_pass_index() == 0 {
ui.request_discard("test");
}
let _ = ui.button("button");
});
assert_eq!(num_passes, 2);
assert!(ctx.memory(|mem| mem.has_focus(button_id)));
let tree = validated_tree(output);
assert_eq!(tree.focus, button_id.accesskit_id());
}