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

ctx.input(|i| i.focused) always gets false for the first few frames #4468

Open
lopo12123 opened this issue May 6, 2024 · 0 comments
Open
Labels
bug Something is broken

Comments

@lopo12123
Copy link
Contributor

description

I want my window to automatically close when it loses focus, but in the first few frames when the app starts, ctx.input(|i| i.focused) always gets false (which makes my app close on the first frame)

reproduce

fn main() {
    use eframe::epaint::Color32;
    use egui::{Frame, Key, ViewportBuilder, ViewportCommand};

    let option = eframe::NativeOptions {
        viewport: ViewportBuilder::default()
            .with_active(true)  // initially focused
            .with_position([100.0, 100.0])
            .with_inner_size([800.0, 600.0]),
        ..Default::default()
    };

    let mut count = 0;
    eframe::run_simple_native(
        "my-app",
        option,
        move |ctx, _frame| {
            egui::CentralPanel::default()
                .frame(Frame::none().fill(Color32::WHITE))
                .show(ctx, |ui| {
                    if ui.button("Close").clicked() {
                        ctx.send_viewport_cmd(ViewportCommand::Close);
                    }

                    // Here I print the first 10 frames. On my computer, the first 2 or 3 frames always show false, and then true.
                    if count < 10 {
                        println!("focused: {}", ctx.input(|i| i.focused));
                        count += 1;
                    }
                });
        },
    ).unwrap();
}

env

KVWYI(8JO~51V( @FM%5`Y


Now my solution is as follows, but it doesn't seem elegant.

fn main() {
    use eframe::epaint::Color32;
    use egui::{Frame, ViewportBuilder, ViewportCommand};

    let option = eframe::NativeOptions {
        viewport: ViewportBuilder::default()
            .with_active(true)  // initially focused
            .with_position([100.0, 100.0])
            .with_inner_size([800.0, 600.0]),
        ..Default::default()
    };

    let mut ready = false;

    eframe::run_simple_native(
        "my-app",
        option,
        move |ctx, _frame| {
            egui::CentralPanel::default()
                .frame(Frame::none().fill(Color32::WHITE))
                .show(ctx, |ui| {
                    if ui.button("Close").clicked() {
                        ctx.send_viewport_cmd(ViewportCommand::Close);
                    }

                    // the first time 'focused' becomes true, the app is ready
                    if !ready && ctx.input(|i| i.focused) {
                        ready = true;
                    }
                    // automatically close window when focus is lost (ignore frames that are not ready)
                    if ready && ctx.input(|i| !i.focused) {
                        ctx.send_viewport_cmd(ViewportCommand::Close);
                    }
                });
        },
    ).unwrap();
}
@lopo12123 lopo12123 added the bug Something is broken label May 6, 2024
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