emilk / emilk/egui

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

Open
#4,468 1 comment 1 reaction 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
30.6k
Forks
2.1k
Avg merge
1d 9h
Merged PRs (30d)
72

Description

## 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

```rust
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](https://github.com/emilk/egui/assets/59609929/dcef299b-3acd-4c27-8676-fdb521be59f5)

---

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

```rust
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();
}
```

Contributor guide

Open the contributing guide

Research direction

Run the supplied eframe::run_simple_native reproduction and observe the first frames from ctx.input(|i| i.focused). Trace how ViewportBuilder::with_active(true) and native viewport events reach the input state. Done means the initial focus state is handled consistently, or the startup behavior is clearly documented with a reliable way to distinguish initialization from focus loss.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.