From 7501039d57526f75a96842b709144449f73c47d2 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Sun, 18 Jun 2023 13:50:26 +0100 Subject: [PATCH] Add examples/window_ondemand A minimal example that shows an application running the event loop more than once via `run_ondemand` There is a 5 second delay between each run to help highlight problems with destroying the window from the first loop. --- examples/window_ondemand.rs | 105 ++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 examples/window_ondemand.rs diff --git a/examples/window_ondemand.rs b/examples/window_ondemand.rs new file mode 100644 index 00000000..1960b163 --- /dev/null +++ b/examples/window_ondemand.rs @@ -0,0 +1,105 @@ +#![allow(clippy::single_match)] + +// Limit this example to only compatible platforms. +#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform,))] +fn main() -> Result<(), impl std::error::Error> { + use simple_logger::SimpleLogger; + + use winit::{ + event::{Event, WindowEvent}, + event_loop::EventLoop, + platform::run_ondemand::EventLoopExtRunOnDemand, + window::{Window, WindowBuilder}, + }; + + #[path = "util/fill.rs"] + mod fill; + + #[derive(Default)] + struct App { + window: Option, + } + + SimpleLogger::new().init().unwrap(); + let mut event_loop = EventLoop::new(); + + { + let mut app = App::default(); + + event_loop.run_ondemand(move |event, event_loop, control_flow| { + control_flow.set_wait(); + println!("Run 1: {:?}", event); + + if let Some(window) = &app.window { + match event { + Event::WindowEvent { + event: WindowEvent::CloseRequested, + window_id, + } if window.id() == window_id => { + app.window = None; + control_flow.set_exit(); + } + Event::MainEventsCleared => window.request_redraw(), + Event::RedrawRequested(_) => { + fill::fill_window(window); + } + _ => (), + } + } else if let Event::Resumed = event { + app.window = Some( + WindowBuilder::new() + .with_title("Fantastic window number one!") + .with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0)) + .build(event_loop) + .unwrap(), + ); + } + })?; + } + + println!("--------------------------------------------------------- Finished first loop"); + println!("--------------------------------------------------------- Waiting 5 seconds"); + std::thread::sleep_ms(5000); + + let ret = { + let mut app = App::default(); + + event_loop.run_ondemand(move |event, event_loop, control_flow| { + control_flow.set_wait(); + println!("Run 2: {:?}", event); + + if let Some(window) = &app.window { + match event { + Event::WindowEvent { + event: WindowEvent::CloseRequested, + window_id, + } if window.id() == window_id => { + app.window = None; + control_flow.set_exit(); + } + Event::MainEventsCleared => window.request_redraw(), + Event::RedrawRequested(_) => { + fill::fill_window(window); + } + _ => (), + } + } else if let Event::Resumed = event { + app.window = Some( + WindowBuilder::new() + .with_title("Fantastic window number two!") + .with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0)) + .build(event_loop) + .unwrap(), + ); + } + }) + }; + + println!("--------------------------------------------------------- Finished second loop"); + ret +} + +#[cfg(not(any(windows_platform, macos_platform, x11_platform, wayland_platform,)))] +fn main() { + println!("This example is not supported on this platform"); +}