From 3910304bd94bddc64d9694bdfd76ebaf5398dca2 Mon Sep 17 00:00:00 2001 From: LoganDark Date: Thu, 10 Nov 2022 15:56:11 -0800 Subject: [PATCH 1/9] Don't show window until after initialization Shortens #1802, but does not completely solve it --- crates/eframe/src/native/run.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 5af243477160..7245aab02410 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -331,7 +331,7 @@ mod glow_integration { let window_settings = epi_integration::load_window_settings(storage); let window_builder = - epi_integration::window_builder(native_options, &window_settings).with_title(title); + epi_integration::window_builder(native_options, &window_settings).with_title(title).with_visible(false); let gl_window = unsafe { glutin::ContextBuilder::new() @@ -393,6 +393,8 @@ mod glow_integration { }); } + gl_window.window().set_visible(true); + let app_creator = std::mem::take(&mut self.app_creator) .expect("Single-use AppCreator has unexpectedly already been taken"); let mut app = app_creator(&epi::CreationContext { @@ -692,6 +694,7 @@ mod wgpu_integration { let window_settings = epi_integration::load_window_settings(storage); epi_integration::window_builder(native_options, &window_settings) .with_title(title) + .with_visible(false) .build(event_loop) .unwrap() } @@ -763,6 +766,8 @@ mod wgpu_integration { }); } + window.set_visible(true); + let app_creator = std::mem::take(&mut self.app_creator) .expect("Single-use AppCreator has unexpectedly already been taken"); let mut app = app_creator(&epi::CreationContext { From f68d0d1d21e01571b2ecaff61b308e93af8a7776 Mon Sep 17 00:00:00 2001 From: LoganDark Date: Fri, 11 Nov 2022 10:28:23 -0800 Subject: [PATCH 2/9] format code --- crates/eframe/src/native/run.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 7245aab02410..2d50045f70ab 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -330,8 +330,9 @@ mod glow_integration { }; let window_settings = epi_integration::load_window_settings(storage); - let window_builder = - epi_integration::window_builder(native_options, &window_settings).with_title(title).with_visible(false); + let window_builder = epi_integration::window_builder(native_options, &window_settings) + .with_title(title) + .with_visible(false); let gl_window = unsafe { glutin::ContextBuilder::new() From df8f97fb9858fd9999f46bb6c5ccc76753860e91 Mon Sep 17 00:00:00 2001 From: LoganDark Date: Fri, 11 Nov 2022 11:32:22 -0800 Subject: [PATCH 3/9] Present first frame immediately before showing window This resolves the white flash almost completely, but is a hack. Window visibility should be derived from the AppOutput, and the first frame should not be painted before the event loop has processed initial events. Working on a better implementation. --- crates/eframe/src/native/run.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 2d50045f70ab..f8b824c00c81 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -394,8 +394,6 @@ mod glow_integration { }); } - gl_window.window().set_visible(true); - let app_creator = std::mem::take(&mut self.app_creator) .expect("Single-use AppCreator has unexpectedly already been taken"); let mut app = app_creator(&epi::CreationContext { @@ -418,6 +416,10 @@ mod glow_integration { integration, app, }); + + // TODO(logandark): This is a hack. we should put window visibility into Frame + self.paint(); + self.running.as_ref().unwrap().gl_window.window().set_visible(true); } } @@ -540,8 +542,10 @@ mod glow_integration { winit::event::Event::Resumed => { if self.running.is_none() { self.init_run_state(event_loop); + EventResult::Wait + } else { + EventResult::RepaintNow } - EventResult::RepaintAsap } winit::event::Event::Suspended => { #[cfg(target_os = "android")] @@ -767,8 +771,6 @@ mod wgpu_integration { }); } - window.set_visible(true); - let app_creator = std::mem::take(&mut self.app_creator) .expect("Single-use AppCreator has unexpectedly already been taken"); let mut app = app_creator(&epi::CreationContext { @@ -790,6 +792,10 @@ mod wgpu_integration { app, }); self.window = Some(window); + + // TODO(logandark): This is a hack. we should put window visibility into Frame + self.paint(); + self.window.as_ref().unwrap().set_visible(true); } } @@ -909,6 +915,7 @@ mod wgpu_integration { ); self.set_window(window); } + EventResult::RepaintNow } else { let storage = epi_integration::create_storage(&self.app_name); let window = Self::create_window( @@ -918,8 +925,8 @@ mod wgpu_integration { &self.native_options, ); self.init_run_state(event_loop, storage, window); + EventResult::Wait } - EventResult::RepaintAsap } winit::event::Event::Suspended => { #[cfg(target_os = "android")] From 672e6c7140a2a4b8ac35aba029047c9cf4c36a47 Mon Sep 17 00:00:00 2001 From: LoganDark Date: Fri, 11 Nov 2022 11:58:58 -0800 Subject: [PATCH 4/9] Integrate window showing with AppOutput This allows an app to keep the window hidden (never shown) by calling Frame.set_visible(false) on the first update. This includes a slightly less nasty hack than the last commit did. Also fixes an accidental cross-contamination of pull requests. --- crates/eframe/src/native/epi_integration.rs | 20 ++++++++++++++------ crates/eframe/src/native/run.rs | 18 +++++------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 9d9d51e0311e..0b044e14280f 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -146,7 +146,7 @@ pub fn handle_app_output( fullscreen, drag_window, window_pos, - visible, + visible: _, always_on_top, } = app_output; @@ -183,10 +183,6 @@ pub fn handle_app_output( let _ = window.drag_window(); } - if let Some(visible) = visible { - window.set_visible(visible); - } - if let Some(always_on_top) = always_on_top { window.set_always_on_top(always_on_top); } @@ -240,7 +236,10 @@ impl EpiIntegration { native_pixels_per_point: Some(native_pixels_per_point), window_info: read_window_info(window, egui_ctx.pixels_per_point()), }, - output: Default::default(), + output: epi::backend::AppOutput { + visible: Some(true), + ..Default::default() + }, storage, #[cfg(feature = "glow")] gl, @@ -325,7 +324,10 @@ impl EpiIntegration { if app_output.close { self.close = app.on_close_event(); } + let visible = app_output.visible; handle_app_output(window, self.egui_ctx.pixels_per_point(), app_output); + // TODO(logandark): find a nicer way to do this + self.frame.output.visible = visible; } let frame_time = (std::time::Instant::now() - frame_start).as_secs_f64() as f32; @@ -341,6 +343,12 @@ impl EpiIntegration { app.post_rendering(window_size_px, &self.frame); } + pub fn post_present(&mut self, window: &winit::window::Window) { + if let Some(visible) = self.frame.output.visible.take() { + window.set_visible(visible); + } + } + pub fn handle_platform_output( &mut self, window: &winit::window::Window, diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index f8b824c00c81..619b2ddcca6e 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -416,10 +416,6 @@ mod glow_integration { integration, app, }); - - // TODO(logandark): This is a hack. we should put window visibility into Frame - self.paint(); - self.running.as_ref().unwrap().gl_window.window().set_visible(true); } } @@ -498,6 +494,8 @@ mod glow_integration { gl_window.swap_buffers().unwrap(); } + integration.post_present(window); + let control_flow = if integration.should_close() { EventResult::Exit } else if repaint_after.is_zero() { @@ -542,10 +540,8 @@ mod glow_integration { winit::event::Event::Resumed => { if self.running.is_none() { self.init_run_state(event_loop); - EventResult::Wait - } else { - EventResult::RepaintNow } + EventResult::RepaintAsap } winit::event::Event::Suspended => { #[cfg(target_os = "android")] @@ -792,10 +788,6 @@ mod wgpu_integration { app, }); self.window = Some(window); - - // TODO(logandark): This is a hack. we should put window visibility into Frame - self.paint(); - self.window.as_ref().unwrap().set_visible(true); } } @@ -862,6 +854,7 @@ mod wgpu_integration { ); integration.post_rendering(app.as_mut(), window); + integration.post_present(window); let control_flow = if integration.should_close() { EventResult::Exit @@ -915,7 +908,6 @@ mod wgpu_integration { ); self.set_window(window); } - EventResult::RepaintNow } else { let storage = epi_integration::create_storage(&self.app_name); let window = Self::create_window( @@ -925,8 +917,8 @@ mod wgpu_integration { &self.native_options, ); self.init_run_state(event_loop, storage, window); - EventResult::Wait } + EventResult::RepaintAsap } winit::event::Event::Suspended => { #[cfg(target_os = "android")] From 854132b8df6930b1c60fd057176849806c77e365 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Nov 2022 20:12:54 +0100 Subject: [PATCH 5/9] fmt --- crates/eframe/src/native/epi_integration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 0b044e14280f..fc5fd48b71e3 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -345,7 +345,7 @@ impl EpiIntegration { pub fn post_present(&mut self, window: &winit::window::Window) { if let Some(visible) = self.frame.output.visible.take() { - window.set_visible(visible); + window.set_visible(visible); } } From ce12ae720080d9a0bcf895daa7d35749cde6e678 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Nov 2022 20:18:17 +0100 Subject: [PATCH 6/9] add comments --- crates/eframe/src/native/run.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 619b2ddcca6e..414a21d3f6c0 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -332,7 +332,7 @@ mod glow_integration { let window_builder = epi_integration::window_builder(native_options, &window_settings) .with_title(title) - .with_visible(false); + .with_visible(false); // Keep hidden until we've painted something. See https://github.com/emilk/egui/pull/2279 let gl_window = unsafe { glutin::ContextBuilder::new() From b75f8ee7f8087e7bfde6b44e9ddd2a76fac96fa2 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Nov 2022 20:18:23 +0100 Subject: [PATCH 7/9] add comments --- crates/eframe/src/native/run.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 414a21d3f6c0..0ee6eaea7c4f 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -695,7 +695,7 @@ mod wgpu_integration { let window_settings = epi_integration::load_window_settings(storage); epi_integration::window_builder(native_options, &window_settings) .with_title(title) - .with_visible(false) + .with_visible(false) // Keep hidden until we've painted something. See https://github.com/emilk/egui/pull/2279 .build(event_loop) .unwrap() } From 48a1559450b07ec9d68907ef6ef79a736beef23b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Nov 2022 20:18:29 +0100 Subject: [PATCH 8/9] add comments --- crates/eframe/src/native/epi_integration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index fc5fd48b71e3..5c02ea8aa548 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -146,7 +146,7 @@ pub fn handle_app_output( fullscreen, drag_window, window_pos, - visible: _, + visible: _, // handled in post_present always_on_top, } = app_output; From 3b4906e2140d1e2d8cf576bf8faabcafa2d10646 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Nov 2022 20:18:34 +0100 Subject: [PATCH 9/9] add comments --- crates/eframe/src/native/epi_integration.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 5c02ea8aa548..a27cb738caa6 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -324,10 +324,8 @@ impl EpiIntegration { if app_output.close { self.close = app.on_close_event(); } - let visible = app_output.visible; + self.frame.output.visible = app_output.visible; // this is handled by post_present handle_app_output(window, self.egui_ctx.pixels_per_point(), app_output); - // TODO(logandark): find a nicer way to do this - self.frame.output.visible = visible; } let frame_time = (std::time::Instant::now() - frame_start).as_secs_f64() as f32;