Skip to content

Improve window restoration logic - #8687

Merged
lucasmerlin merged 8 commits into
mainfrom
lucas/fix-window-restoration
Oct 13, 2026
Merged

lucasmerlin merged 8 commits into
mainfrom
lucas/fix-window-restoration

Conversation

@lucasmerlin

@lucasmerlin lucasmerlin commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

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 win32: avoid showing hidden windows when applying window state rust-windowing/winit#4575 or winit-win32: Fix Window focus after showing hidden maximized window 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).

  • I have followed the instructions in the PR template

🤖 Generated with Claude Code

https://claude.ai/code/session_019yMtdim1FPCsviY1FWfdTk

lucasmerlin and others added 2 commits October 9, 2026 15:03
- Create the window normal, move it, then maximize or enter fullscreen
  (Mac and Windows), so moving no longer resets either state.
- Clamp the saved position once and use the clamped settings for the
  exact move too.
- Drop a saved Mac position that is on no connected monitor; zooming an
  off-screen window gave it zero height and crashed wgpu.
- Fall back to defaults for old save files instead of a 64x64 window.
- Remember the startup geometry when nothing was saved yet.
- Keep X11/Wayland on the builder-only path.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019yMtdim1FPCsviY1FWfdTk
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Preview available at https://egui-pr-preview.github.io/pr/8687-lucasfix-window-restoration
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

lucasmerlin and others added 3 commits October 9, 2026 21:19
…indows

eframe creates its window hidden until the first frame is painted. winit
implements maximize with `ShowWindow(SW_MAXIMIZE)`, which shows the hidden
window, and then hides it again, so the window flashed on screen before
the real show.

On Windows, a window created hidden is no longer maximized via its window
attributes. `apply_viewport_builder_to_window` maximizes it instead, with
the window cloaked (`DWMWA_CLOAK`) so the brief show is never composited.
The window is already maximized when first shown, so the first frame is
rendered at the right size.

Can be removed once winit has rust-windowing/winit#4575 or #4587.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`WindowSettings::initialize_window` maximizes the still-hidden window after
moving it, which goes through winit's `ShowWindow(SW_MAXIMIZE)` and briefly
shows it. Use the new `egui_winit::set_maximized`, which cloaks a hidden
window while maximizing it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lucasmerlin lucasmerlin added bug Something is broken eframe Relates to epi and eframe labels Oct 12, 2026
@lucasmerlin lucasmerlin changed the title Remember the normal window size and position when maximized or fullscreen Improve window restoration logic Oct 12, 2026
@lucasmerlin
lucasmerlin marked this pull request as ready for review October 12, 2026 14:17

@emilk emilk left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this is complicated 😓

Thanks for testing on all the platforms.

Did you also test with multiple monitors with different DPIs?

Comment on lines +1034 to +1035
// The event can be stale: winit buffers events emitted while we create and set up
// the window (e.g. maximize it), so it can arrive after the window has changed size again.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well that's annoying 😆

@lucasmerlin

Copy link
Copy Markdown
Collaborator Author

Did you also test with multiple monitors with different DPIs?

Yes! It works perfectly everywhere now for me, on my hdpi laptop screen and my lowdpi monitor (except wayland, where you can't programmatically set window positions, so the restored position is up to the window manager)

@lucasmerlin
lucasmerlin enabled auto-merge (squash) October 13, 2026 08:34
@lucasmerlin
lucasmerlin merged commit b25d2d4 into main Oct 13, 2026
26 checks passed
@lucasmerlin
lucasmerlin deleted the lucas/fix-window-restoration branch October 13, 2026 08:37
dfrnoch added a commit to ViceVerse-cz/Serein that referenced this pull request Oct 15, 2026
Picks up window restoration fixes (emilk/egui#8687), including stale
Resized handling, Windows system-font CJK fallback (emilk/egui#8688) and
premultiplied clipboard image copies (emilk/egui#8715). All apply without
code changes; eframe window persistence stays disabled. Sync license
provenance with the pinned revision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lucasmerlin pushed a commit that referenced this pull request Nov 9, 2026
* Follow-up to #5211 and #8687

#5211 made `egui_winit::screen_size_in_pixels` use `outer_size()` on
iOS, so egui lays out over the whole screen, including the area behind
the Dynamic Island and home indicator. But eframe's wgpu backend still
creates and resizes the surface from `inner_size()`, which on iOS is
only the safe area.

The OS then stretches the smaller surface over the screen. The UI is
drawn taller than its layout, about 11% on an iPhone 16 Pro Max. Bottom
panels end up off screen and touches land away from what they hit.

This PR adds `egui_wgpu::winit::surface_size(window)`, which returns
`outer_size()` on iOS and `inner_size()` everywhere else, and uses it:

- in `Painter::set_window` and `Painter::set_window_unsafe`, where the
surface is created
- in eframe's `WindowEvent::Resized` handler

Other platforms are unchanged. Found while running an eframe app
(LightCraft) on an iPhone 16 Pro Max. The fix has been in use there
since; with it, the layout and the drawn frame match exactly.

* [x] I have followed the instructions in the PR template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something is broken eframe Relates to epi and eframe

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants