Skip to content

Dismiss on window blur only after the window had focus (part of #326) - #365

Merged
samitbasu merged 2 commits into
mainfrom
blur-326
Nov 18, 2026
Merged

samitbasu merged 2 commits into
mainfrom
blur-326

Conversation

@samitbasu

Copy link
Copy Markdown
Owner

Part of #326: the blur bug, which needs no API decision. DismissOn::Outside, on_dismiss_with and focus_on_open wait for the user's answers on the issue.

The bug

A window that starts without the keyboard focus, as a screenshot run's often does, can still be sent WindowFocused(false). The WindowBlur rule in dismiss::dismissals then dismissed any overlay already drawn, so a popover opened in the first frames closed at once.

Proven first: a_window_that_never_had_focus_while_it_was_open_does_not_dismiss_it failed on main. It takes the window's focus away (as egui-winit does: RawInput::focused = false plus the event), opens the popover, and sends WindowFocused(false). On main the popover was dismissed.

The fix

  • dismiss::WindowFocus { Had, NotYet }: a crate-internal enum, kept on each open overlay's stack entry (stack::Open::window_focus) and carried into its Layer.
  • Setting it: judge sets Had at the end of a frame in which the overlay is drawn while egui says the window has the focus (InputState::focused).
  • The rule: a window blur dismisses only a layer that has Had. A window that never had the focus while the overlay was open can't lose it.
  • What's unchanged: the other rules (outside press, Esc, focus out, a missing anchor, exclusive menus), and blur for any overlay shown while the window had the focus. In kittest, focused defaults to true, so the existing blur test is unaffected.
  • A small cleanup: layers_of's closure now returns a named Remembered { focus_before, focus_now, window_focus } instead of a tuple.

Behaviour change

An overlay opened while its window doesn't have the keyboard focus is no longer dismissed by a window blur until the window has gained the focus while the overlay is open. Gaining the focus doesn't dismiss anything.

Tests

  • tests/it/overlay_input.rs, popover module, through the real draw path:
    • a_window_that_never_had_focus_while_it_was_open_does_not_dismiss_it (failed on main);
    • a_window_that_gains_focus_while_it_is_open_dismisses_it_on_losing_it: the window is unfocused when the popover opens, gains the focus (nothing is dismissed), then loses it (dismissed);
    • both assert the window's focus as a precondition.
  • Unit test (dismiss.rs): a_window_blur_dismisses_only_an_overlay_shown_while_the_window_had_focus, which also checks that an outside press still dismisses a NotYet layer.
  • Mutations, each caught:
    • the blur filter removed: the new tests fail;
    • Had set whether or not the window has the focus: the never-had-focus test fails;
    • Had never set: the existing the_window_losing_focus_dismisses_it and the gains-focus test fail.

No UI change and no public API change. No TODO(prose).

git diff origin/main HEAD --stat

 src/dismiss.rs            | 31 +++++++++++++++++++++++++++++++
 src/overlay_input.rs      | 42 ++++++++++++++++++++++++++++--------------
 src/stack.rs              |  4 +++-
 tests/it/overlay_input.rs | 40 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 102 insertions(+), 15 deletions(-)

cargo xtask ci exits 0 on 7f485ab (ef87169 merged with main a8e1ec7).

🤖 Generated with Claude Code

https://claude.ai/code/session_01PWiKKf74MNWrhkW1CvKVUP

samitbasu and others added 2 commits November 18, 2026 07:20
A window that starts without the keyboard focus, as a screenshot run's
often does, can still be sent WindowFocused(false). The WindowBlur rule
then dismissed any overlay already drawn, so a popover opened on the
first frames closed at once.

An open overlay now remembers, on its stack entry, whether the window
had the focus (egui's InputState::focused) in a frame it was drawn. A
window blur dismisses only an overlay that has; a window that never had
the focus can't lose it. The other rules are unchanged.

Behaviour change: an overlay opened while its window has no keyboard
focus is no longer dismissed by a blur until the window has gained the
focus while it is open.

Part of #326.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKf74MNWrhkW1CvKVUP
@samitbasu

Copy link
Copy Markdown
Owner Author

Approved by the user (2026-11-18). Gate (my run with current main): cargo xtask ci exits 0. Merging.

@samitbasu
samitbasu merged commit 9c42574 into main Nov 18, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant