Skip to content

Restore the window size and position on launch - #20

Merged
ggand0 merged 8 commits into
ggand0:mainfrom
hml-pip:feat/window-state
Oct 4, 2026
Merged

ggand0 merged 8 commits into
ggand0:mainfrom
hml-pip:feat/window-state

Conversation

@hml-pip

@hml-pip hml-pip commented May 11, 2026

Copy link
Copy Markdown
Contributor

This feature will create data\app.ron file in storage_dir.

圖片 1: 1920x1200

2: 1920x1080

It doesn't retain the previous window size when exiting in fullscreen mode, and when you exit application while in fullscreen on monitor 1, it restores to 2 at next startup on Windows, if there's other issues on other platform, I'll port the code from iced version.

hml-pip and others added 2 commits May 11, 2026 09:12
Skip the hardcoded with_inner_size and first-frame DPI resize when
eframe has persisted window state from a previous session. Both paths
now check for app.ron existence so the default 1280x720 only applies
on first launch.
@ggand0

ggand0 commented May 11, 2026

Copy link
Copy Markdown
Owner

I tested it and window size was indeed not restoring. Two things were overriding eframe's restore:

  1. The hardcoded with_inner_size([1280.0, 720.0]) in main.rs always sets the window size before loading the persisted state app.ron (points to a Rusty Object Notation file)
  2. There's existing code (src/app.rs:451) that corrects window size in physical pixels on the first frame for DPI-scaled displays, since egui only offers API for logical pixels. That part was firing unconditionally so I gated it to only do it for the initial app launch

The fix I pushed should now restore window size as well as positions, and I verified it working on macOS and Ubuntu. Can you verify it works on your Windows?

@hml-pip

hml-pip commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

Windows was already working as "intended" (It restore size and position correctly in window mode, but doesn't have the features we implemented for the iced version) before your commit, it seems Windows wasn't affect by your forced .with_inner_size([1280.0, 720.0]), the issues I mentioned still presents.

I think my requirements are edge cases. I'll see what I can do after completing the compressed file support and slider preview with possible additional PR if you want to merge this one.

@hml-pip
hml-pip marked this pull request as ready for review May 12, 2026 01:17
@YelovSK

YelovSK commented May 23, 2026

Copy link
Copy Markdown
Contributor

I was also trying the window persistence, but it has some annoying bugs, at least on Windows.

The first one is that when restoring a maximized window, it briefly flashes white. I'm pretty sure that's an issue in winit, I made a PR here - rust-windowing/winit#4575. Basically, when the app is launched initially with the hidden and maximized state, winit first calls winapi's maximize API, which briefly shows the window, and then immediately hides it.

The second one is that the normal window size is not persisted when maximized in egui. So when you unmaximize the window, it doesn't get restored to the previous size. Winit doesn't have an API that would return the normal window size, so I could only fix it by calling winapi in egui, and even that has some potentially sketchy cross-coordinate stuff, so I ignored it.
However, related to this, I personally also have an issue where the maximized window gets opened on the wrong monitor, so I made a PR for that in emilk/egui#8191 so that it chooses the monitor by overlap instead.

Right now, the maximized window persistence on Windows is really annoying.

@hml-pip

hml-pip commented May 24, 2026

Copy link
Copy Markdown
Contributor Author

Does the iced version work for you?
I've tried to cover most situations.

@YelovSK

YelovSK commented May 24, 2026

Copy link
Copy Markdown
Contributor

Does the iced version work for you? I've tried to cover most situations.

The iced version is a bit less broken, but also acts weird when restoring the window. See video.

Recording.2026-05-24.122318_1.mp4.mp4

But I am using the egui version, since it feels a bit better to me.

@hml-pip

hml-pip commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

I'd say it's not perfect, but it gets the job done.

The reason I'm asking is that, at worst, I can port the code made for the Iced version, but I'm not sure the added code complexity is worth it. Even then, the outcome might not satisfy your expectations.

@YelovSK

YelovSK commented May 25, 2026

Copy link
Copy Markdown
Contributor

Oh, I don't have a strong opinion. Of course, ideally, the egui persistence would work perfectly, but that isn't the case.

I already tried doing it directly in viewskater, and it was mostly passable, including getting rid of the white flash (iirc I created the window hidden, and maximized it a bit later, but that showed the maximizing animation), but it was a bit too.. I wasn't comfortable with that being the final implementation. I only got it working well with the winit and egui patches, but I have no idea if those will get merged. Well, except for the unmaximized size. I imagine it's possible without using the winapi by persisting the last window size when not maximized/minimized.

@hml-pip

hml-pip commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

Check my new implementation and see if that works for you, I think this PR might not be needed if there's no upstream changes.

@hml-pip
hml-pip marked this pull request as draft May 25, 2026 09:09
@YelovSK

YelovSK commented May 25, 2026

Copy link
Copy Markdown
Contributor

Check my new implementation and see if that works for you, I think this PR might not be needed if there's no upstream changes.

Seems to work fine, except for the white flash and the unmaximize not remembering the size. This PR emilk/egui#8191 was just merged in egui, so in theory, the egui persistence should work almost the same as your implementation, with the only difference being that, in your PR, unmaximize restores a smaller window, and in egui's persistence, it restores the maximized size.

If the winit fix gets merged, I think it should fix the white flash in both your implementation and in the egui persistence.

@hml-pip

hml-pip commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! I found another problem: when you set the scaling to anything other than 100% for a high-resolution monitor on Windows, maximized or full-screen window does not recover properly on the next startup in my implementation, I'll keep digging.

@ggand0

ggand0 commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Ok I reproduced these issues on Windows 10 dual monitor setup:

  1. White flash on maximize restore
  2. Original window size lost when leaving maximized/fullscreen -
    eframe doesn't save the window size before maximize at all (ref: Incorrect maximize behaviour emilk/egui#3494)
  3. Wrong-monitor restore -
    maximized/fullscreen/windowed on secondary monitor, quit, relaunch -> comes back on primary

For 1, @YelovSK opened rust-windowing/winit#4575 but winit tends to be slow on PRs so probably not getting merged soon. For 3, @YelovSK's emilk/egui#8191 was merged, but we're pinned to egui 0.31 and it's not available yet.

Given these, I think implementing custom persistence (PR #29's direction) is the right approach.

@hml-pip

hml-pip commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

I think updating to egui 0.35 is a chore and a lot of works to even get it to compile. This needs a separate PR to do if that's the direction we're heading.

Incomplete summaries

Replace App::update with fn logic and fn ui

More ui less context

improved-popups-tooltips-and-menus

Some deprecations are easy, but others need additional testing.

eframe persistence snapshots the window at save time, so quitting
while maximized or fullscreen restored that state on the next launch.
On Windows that flashes white (rust-windowing/winit#4575) and the size
from before the maximize is lost (emilk/egui#3494).

Turn off persist_window and write the window entry from the app
instead: record the geometry every frame while the window is in its
normal state and store it under eframe's window key in App::save with
maximized and fullscreen set to false. eframe still does the restore
with its monitor and scale-factor handling, so a maximized or
fullscreen quit relaunches as a normal window at the last normal size
and position.

Building the entry needs a constructor on egui_winit::WindowSettings,
added to the egui-winit fork this project already patches in.
The normal-window geometry is captured every frame while the viewport
reports neither maximized, fullscreen nor minimized. On macOS egui-winit
never queries the maximized state at runtime, and NSWindow.isZoomed only
turns true once the zoom animation has landed, so the intermediate frames
of a green-button zoom were captured as normal geometry and a relaunch
came back nearly full screen.

Ask the NSWindow directly. AppKit runs the zoom and un-zoom animations
as a live resize, so isZoomed || inLiveResize is false exactly for the
frames that are safe to record.

Tested on macOS: green-button zoom, un-zoom, fullscreen and plain
resize/move all relaunch at the last normal geometry.
@ggand0

ggand0 commented Sep 8, 2026 •

Copy link
Copy Markdown
Owner

I decided to go with size and position only and skip restoring maximized/fullscreen for now.
Not having a basic window state restore feature feels annoying, so I'd like to ship this feature first before supporting extra stuff.

Now the app saves the window state itself as RON instead of eframe doing it, and it only records the size and position while the window is in the normal state. A maximized or fullscreen window never gets saved. eframe still restores it on startup like before, and to write eframe's format from the app I added a custom constructor to WindowSettings in my egui-winit fork (ggand0/egui-winit-custom@dd7862a). On macOS we use NSWindow's API to check isZoomed/inLiveResize because egui-winit doesn't report the zoomed state at runtime.

If you quit while maximized or fullscreen, the next launch gives you a normal window with the size and position you had before. This should get rid of the white flash issue. For the wrong monitor problem we'd still need egui 0.35.
I tested on Ubuntu (X11, dual monitor) and macOS (laptop monitor only):

  • Maximize/zoom/fullscreen and quit
  • Move the window to the second monitor and quit
  • Resize and move the window, quit
    all restore the last known state (before maximizing).

@hml-pip @YelovSK Can you check it on your machine to see if it works? We can look into restoring maximized state again in the future.

@ggand0
ggand0 marked this pull request as ready for review September 8, 2026 14:01
ggand0 added 3 commits October 3, 2026 02:07
Four sentences joined with semicolons are split in two, and the doc of `window_zoomed_or_resizing` says the zoom animation has finished.
`App::new` had eight arguments, one over clippy's `too_many_arguments` limit. `main` computed `needs_dpi_resize` from whether eframe's `app.ron` exists and passed it in. That check is now `window_state::has_persisted_state`, which `main` and `App::new` each call. eframe first writes `app.ron` when it saves, after `App::new` has returned, so both calls return the same value.
@ggand0

ggand0 commented Oct 4, 2026

Copy link
Copy Markdown
Owner

I merged main into the branch and tested it again on Windows, macOS and Ubuntu.
Basic restore feature and restoring the pre-maximize window state after quitting while maximized (my last comment) still work, so I'm merging this. Feel free to open another PR or issue if you run into any problems.

@ggand0 ggand0 changed the title Egui persistence test Restore the window size and position on launch Oct 4, 2026
@ggand0
ggand0 merged commit 4c92345 into ggand0:main Oct 4, 2026
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.

3 participants