Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gained_focus broken for parent Ui #5730

Open
oleflb opened this issue Feb 19, 2025 · 0 comments
Open

gained_focus broken for parent Ui #5730

oleflb opened this issue Feb 19, 2025 · 0 comments
Labels
bug Something is broken

Comments

@oleflb
Copy link

oleflb commented Feb 19, 2025

Describe the bug

gained_focus is broken in some cases. In the given example, has_focus of response becomes true in the same frame that had_focus_last_frame becomes true. Therefore gained_focus is always false.
In the provided example, the text is never printed when pressin the "Focus" button.

Screencast.From.2025-02-19.18-45-56.mp4

To Reproduce
Steps to reproduce the behavior:
Here is some code for reproduction

use eframe::{
    egui::{CentralPanel, Context, Response, Sense, Ui, UiBuilder, Widget},
    run_simple_native, Frame, NativeOptions,
};

fn main() -> Result<(), eframe::Error> {
    run_simple_native("Test", NativeOptions::default(), update)
}

fn update(ctx: &Context, _frame: &mut Frame) {
    CentralPanel::default().show(ctx, |ui| {
        let response = ui.add(SomeWidget);

        if ui.button("Focus").clicked() {
            response.request_focus();
        }
    });
}

pub struct SomeWidget;

impl Widget for SomeWidget {
    fn ui(self, ui: &mut Ui) -> Response {
        ui.scope_builder(UiBuilder::default().sense(Sense::FOCUSABLE), |ui| {
            let response = ui.response();
            let button = ui.button("Button");

            if response.has_focus() && response.gained_focus() {
                println!("Button gained focus");
            }

            if response.gained_focus() {
                button.request_focus();
            }
        })
        .response
    }
}

Expected behavior
I expect gained_focus to be true when has_focus becomes true.

Desktop (please complete the following information):

  • OS: Fedora Linux 41
  • eframe/egui version: tested on 0.31 and master (native)
@oleflb oleflb added the bug Something is broken label Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken
Projects
None yet
Development

No branches or pull requests

1 participant