Desktop: a maximized window no longer flashes on and off at start-up on Windows (#23) (#1194)

* Desktop: a maximized window no longer flashes on and off at start-up on Windows (#23)

winit shows a window the moment it creates it maximized, even though eframe starts it hidden
until the first frame. A window saved as maximized therefore appeared and disappeared about four
times in a third of a second before settling. On Windows it now opens at its normal size, still
hidden, and the first frame maximizes it (WindowGeometry::maximize_after_open). Other platforms
are unchanged. Measured by polling the window's visibility every 15 ms: before, show/hide four
times; after, hidden until the first frame, shown once.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Contributors: add my display name

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Brandon Thomas <echelon@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-10-14 17:59:04 -04:00
1 parent 6655ba675d
commit 8ae23cee29
3 files changed
+41 -2

No files matched your search

+6
View File
@@ -49,9 +49,15 @@ impl eframe::App for App {
self.3.show(frame);
let prev = self.0.ui.window;
self.0.ui.window = ctx.input(|i| WindowGeometry::track(prev, i.viewport(), i.viewport_rect().size()));
// The first frame, once the monitor is known (as `take_rescue` waits for): maximize a
// window that opened at its normal size so it never flashed on screen half-opened.
let maximize = self.1.and_then(|g| g.maximize_after_open()).filter(|_| ctx.input(|i| i.viewport().monitor_size.is_some()));
if let Some(pos) = ctx.input(|i| take_rescue(&mut self.1, i.viewport())) {
ctx.send_viewport_cmd(egui::ViewportCommand::OuterPosition(pos));
}
if let Some(command) = maximize {
ctx.send_viewport_cmd(command);
}
// The window's close button: with unsaved changes, ask first (`WordApp::close_requested`).
let closing = ctx.input(|i| i.viewport().close_requested());
if closing && !self.0.close_requested() {
+3 -1
View File
@@ -91,7 +91,6 @@ sync_github_name = true
[people.benandez]
real_name = "Martin Ball"
display_name = "Martin Ball"
sync_github_name = false
[people.Nghiathan13]
@@ -101,3 +100,6 @@ real_name = "Sazid Al Bayazid"
[people.switch87]
real_name = "Gert Pellin"
display_name = "Gert Pellin"
[people.tlomts-dev]
display_name = "The Legend Of MTS Dev"
+32 -1
View File
@@ -63,9 +63,23 @@ impl WindowGeometry {
Some(Self { position, width: size.x, height: size.y, maximized: self.maximized, monitor })
}
/// Whether the window is maximized after it has opened rather than as it opens. On Windows,
/// winit shows a window the moment it creates it maximized, even one that is meant to stay
/// hidden until the first frame (eframe's white-flash guard), so a maximized window flashed
/// on and off about four times at start-up. There it opens at its normal size, hidden, and the
/// first frame maximizes it ([`Self::maximize_after_open`]).
pub fn maximizes_after_open(self) -> bool {
self.maximized && cfg!(windows)
}
/// The command that maximizes the window after it opened, if [`Self::maximizes_after_open`].
pub fn maximize_after_open(self) -> Option<egui::ViewportCommand> {
self.maximizes_after_open().then_some(egui::ViewportCommand::Maximized(true))
}
/// Open the window at this size, position and maximized state.
pub fn apply(self, builder: egui::ViewportBuilder) -> egui::ViewportBuilder {
let builder = builder.with_inner_size([self.width, self.height]).with_maximized(self.maximized);
let builder = builder.with_inner_size([self.width, self.height]).with_maximized(self.maximized && !self.maximizes_after_open());
match self.position {
Some(p) => builder.with_position(p),
None => builder,
@@ -122,6 +136,23 @@ mod tests {
assert_eq!(b.maximized, Some(false));
}
#[test]
fn a_maximized_window_opens_normal_on_windows_and_is_maximized_by_the_first_frame() {
let g = WindowGeometry { maximized: true, ..saved() };
let b = g.apply(egui::ViewportBuilder::default());
// Normal size and position either way, so un-maximizing returns to them.
assert_eq!(b.inner_size, Some(egui::vec2(1200.0, 800.0)));
assert_eq!(b.position, Some(egui::pos2(100.0, 50.0)));
if cfg!(windows) {
assert_eq!(b.maximized, Some(false), "winit would show the hidden window while creating it maximized");
assert_eq!(g.maximize_after_open(), Some(egui::ViewportCommand::Maximized(true)));
} else {
assert_eq!(b.maximized, Some(true));
assert_eq!(g.maximize_after_open(), None);
}
assert_eq!(saved().maximize_after_open(), None, "a normal window needs no command");
}
#[test]
fn maximized_keeps_the_normal_size_and_position() {
let full = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1920.0, 1040.0));