Skip to content

fix: prevent stale Focused(false) events from clearing valid focus and freezing rendering#8231

Open
rustbasic wants to merge 7 commits into
emilk:mainfrom
rustbasic:p103
Open

fix: prevent stale Focused(false) events from clearing valid focus and freezing rendering#8231
rustbasic wants to merge 7 commits into
emilk:mainfrom
rustbasic:p103

Conversation

@rustbasic

Copy link
Copy Markdown
Contributor

fix: prevent stale Focused(false) events from clearing valid focus and freezing rendering

Description

This PR fixes a rare and elusive screen-freezing issue in egui (especially noticeable on Windows 10/11) where the UI rendering permanently stops after running the application for a prolonged period (e.g., once or twice a day during all-day usage).

Cause of the Issue

  1. Unconditional Focus Clearing:
    Previously, any WindowEvent::Focused(false) event would unconditionally reset focused_viewport to None. In multi-window environments or when Windows OS dispatches stale/out-of-order focus events, a stale Focused(false) event from another window/viewport would clear the currently active viewport's focus state.
  2. Missing Repaint Trigger:
    Once the focus state was incorrectly cleared or desynchronized, the application would stop repainting under certain conditions, causing the screen to freeze while the background logic continued running responsiveness-free.

Solution

  1. Guarded Focus Clearing:
    Only clear shared.focused_viewport if the viewport losing focus actually matches the one we currently believe to be focused:
    } else if shared.focused_viewport == Some(viewport_id) {
        shared.focused_viewport = None;
    }
  2. Forced Repaint on Focus Event:
    Explicitly set repaint_asap = true on any Focused event to ensure the UI updates immediately and stays responsive when focus transitions occur.

Since this bug occurs semi-randomly over long sessions and is highly dependent on OS-level window focus dispatching, it is extremely difficult to write a minimal reproducible example. However, guarding the state transition and forcing a repaint completely resolves the long-term freeze issue.

@github-actions

github-actions Bot commented Jun 10, 2026

Copy link
Copy Markdown

Preview available at https://egui-pr-preview.github.io/pr/8231-p103
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

View snapshot changes at kitdiff

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.

Unexpected Program UI Freeze While Using egui Program on Windows

1 participant