From 160090b20a3acfc554580eb866e1a0bb91ac44ea Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 24 Sep 2026 12:47:26 +0200 Subject: [PATCH] Don't lose keyboard focus in a pass that will be discarded (#8623) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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) --- crates/egui/src/context.rs | 1 - .../tests/discard_invisible_accessibility.rs | 32 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 6ccbf430..1653c250 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -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)); diff --git a/crates/egui_kittest/tests/discard_invisible_accessibility.rs b/crates/egui_kittest/tests/discard_invisible_accessibility.rs index fd273270..dc0cf2b7 100644 --- a/crates/egui_kittest/tests/discard_invisible_accessibility.rs +++ b/crates/egui_kittest/tests/discard_invisible_accessibility.rs @@ -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()); +}