Skip to content

winit-win32: Fix Window focus after showing hidden maximized window - #4587

Open
rhysd wants to merge 3 commits into
rust-windowing:masterfrom
rhysd:issue-4586
Open

rhysd wants to merge 3 commits into
rust-windowing:masterfrom
rhysd:issue-4586

Conversation

@rhysd

@rhysd rhysd commented Jun 4, 2026

Copy link
Copy Markdown
Contributor
  • Tested on all platforms changed
  • Added an entry to the changelog module if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality

Fixes #4586

Previously winit could call ShowWindow(SW_MAXIMIZE) while the window was still hidden. It could disturb Win32’s focus state. The fix delays the call until the window is actually shown avoiding calling it twice.

@rhysd

rhysd commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor Author

CI failed inside winit-x11, which should not be related to this PR's change.

error: iterating on a map's values
    --> winit-x11/src/event_processor.rs:1760:31
     |
1760 |                 for window in self.target.windows.borrow().iter().filter_map(|(_, w)| w.upgrade()) {
     |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `self.target.windows.borrow().values().filter_map(|w| w.upgrade())`
     |
     = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.96.0/index.html#iter_kv_map
     = note: `-D clippy::iter-kv-map` implied by `-D warnings`
     = help: to override `-D warnings` add `#[allow(clippy::iter_kv_map)]`

@rhysd

rhysd commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

I will rebase onto the latest master branch and try to resolve the conflict within today.

@rhysd

rhysd commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

I rebased this branch onto the latest master branch resolving the conflict.

echelon added a commit to storytold/pdfcraft that referenced this pull request Oct 10, 2026
…it#4587) (#674)

eframe creates PdfCraft's window hidden, restores the saved state
(app.ron, persist_window) and shows the window after the first frame is
drawn. For a window closed maximized, winit 0.30.13 applies the maximized
state with ShowWindow(SW_MAXIMIZE), which shows a hidden window: PdfCraft
appeared unpainted (white), was hidden again, then shown with SW_SHOW and
SW_MAXIMIZE once more. rust-windowing/winit#4586 also shows the window's
focus state going wrong on this path.

vendor/winit now skips the maximize while the window is hidden and shows a
hidden window that is maximized with one SW_MAXIMIZE, as
rust-windowing/winit#4587 (approved upstream) does on winit master. That is
the later, focus-safe form of #4575; #4575's other change (re-sending
SW_MINIMIZE on every flag change while minimized) is not taken. Transitions
between two visible states make the same ShowWindow calls as before. A test
fails if a re-vendored winit loses the patch.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Brandon Thomas <echelon@gmail.com>
krichelj pushed a commit to krichelj/pdfcraft that referenced this pull request Oct 11, 2026
…it#4587) (storytold#674)

eframe creates PdfCraft's window hidden, restores the saved state
(app.ron, persist_window) and shows the window after the first frame is
drawn. For a window closed maximized, winit 0.30.13 applies the maximized
state with ShowWindow(SW_MAXIMIZE), which shows a hidden window: PdfCraft
appeared unpainted (white), was hidden again, then shown with SW_SHOW and
SW_MAXIMIZE once more. rust-windowing/winit#4586 also shows the window's
focus state going wrong on this path.

vendor/winit now skips the maximize while the window is hidden and shows a
hidden window that is maximized with one SW_MAXIMIZE, as
rust-windowing/winit#4587 (approved upstream) does on winit master. That is
the later, focus-safe form of storytold#4575; storytold#4575's other change (re-sending
SW_MINIMIZE on every flag change while minimized) is not taken. Transitions
between two visible states make the same ShowWindow calls as before. A test
fails if a re-vendored winit loses the patch.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Brandon Thomas <echelon@gmail.com>
lucasmerlin added a commit to emilk/egui that referenced this pull request Oct 13, 2026
eframe now remembers the window's last *normal* (not maximized, not
fullscreen) size and position. After a relaunch, un-maximizing or
leaving fullscreen goes back to that size instead of a window that is
"maximized but not maximized".

Other fixes:
* A maximized or fullscreen window no longer paints its first frame
small.
* Leaving a restored fullscreen goes straight back to the normal size,
in one step.
* The window reopens on the correct monitor, also with monitors of
different scale factors on macOS. The position is saved in pixels
together with the scale factor, and converted on restore.
* On macOS and Windows, the window is placed exactly after creation,
then maximized or made fullscreen.
* On Windows, a window restored maximized no longer flashes on screen
(shown, hidden, then shown again) during startup. winit maximizes with
`ShowWindow(SW_MAXIMIZE)`, which shows the still-hidden window before
hiding it again. The new `egui_winit::set_maximized` cloaks a hidden
window (`DWMWA_CLOAK`) while maximizing it, so it stays invisible but is
already maximized for the first frame. This is a workaround until winit
has rust-windowing/winit#4575 or rust-windowing/winit#4587.
* On X11, a window saved maximized or fullscreen now restores as such.
winit sends these as `_NET_WM_STATE` client messages, which window
managers ignore while the window is still hidden (eframe keeps it hidden
until the first frame is painted). The new
`WindowSettings::after_first_show` re-applies them right after the
window is shown. `before_first_show` replaces the macOS-only
`enter_fullscreen_after_first_frame` / `enter_deferred_fullscreen`.

The save format changed, so old saved window settings are ignored once
(the window opens at its default size).

Known limitations:
* The normal geometry is sampled on save. If you resize, maximize before
the next autosave, and quit while maximized, un-maximizing after a
relaunch goes back to the size from before the resize.
* On X11, a restored maximized window may show un-maximized for a frame
before it maximizes.
* On X11 with GNOME (mutter), un-maximizing a window that was restored
maximized puts it in the top-left corner of its monitor, not at its
saved position. The size is right. mutter places windows that get
maximized right after mapping at the work area origin.
* On Wayland, the position (and so the monitor) can't be restored,
because Wayland doesn't let clients read or set window positions. Size
and maximized/fullscreen state are restored.
* On GNOME, a restored window that is close to the screen size gets
auto-maximized by mutter (`org.gnome.mutter auto-maximize`).

Tested on macOS, wayland, x11 (XWayland on GNOME, including two
monitors) and Windows (wgpu and glow).

* [x] I have followed the instructions in the PR template

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_019yMtdim1FPCsviY1FWfdTk

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
richerfu pushed a commit to richerfu/winit that referenced this pull request Nov 2, 2026
Port of rust-windowing#4587 (focus-safe form of rust-windowing#4575).

Originally "M15.1: keep hidden maximized windows hidden" in storytold/wordcraft#787 (same change as storytold/pdfcraft vendor/winit).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Windows: Focus is broken when an invisible window is created with maximized

2 participants