emilk / egui

egui: an easy-to-use immediate mode GUI in Rust that runs on both web and native
https://www.egui.rs/
Apache License 2.0
21.88k stars 1.58k forks source link

`Context::interact_bg` breaks focus #5053

Closed lucasmerlin closed 1 week ago

lucasmerlin commented 4 weeks ago

Describe the bug

Calling ui.interact_bg(Sense::click()) somewhere in an egui applicaiton makes it impossible to focus any elements added after the call.

https://github.com/user-attachments/assets/588c2aef-005b-491a-acb1-5578deb1aa44

The label in the video is in a child ui where interact_bg is called. I just keep pressing tab. I'd expect the focus to jump between the first button, the child scope and the second button with each tab press. Instead the scope seems to receive focus for a single frame and then loose it, causing the first button to be focused again on the next tab press.

To Reproduce Example code:

use eframe::egui;
use eframe::NativeOptions;
use egui::{CentralPanel, Sense};

pub fn main() -> eframe::Result {
    eframe::run_simple_native("focus test", NativeOptions::default(), |ctx, _frame| {
        CentralPanel::default().show(ctx, |ui| {
            ui.button("I can focus");

            ui.push_id("focus me", |ui| {
                ui.label("I can focus for a single frame");
                let response = ui.interact_bg(Sense::click());
                let t = if response.has_focus() {
                    "has focus"
                } else {
                    "doesn't have focus"
                };
                ui.label(t);
            });

            ui.button("I can't focus :(");
        });
    })
}

Expected behavior Focus jumps between all focusable elements.

Desktop (please complete the following information):