diff --git a/Cargo.toml b/Cargo.toml index a919867524..9fbdd5e055 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,6 +63,9 @@ smol_str = "0.2.0" image = { version = "0.24.0", default-features = false, features = ["png"] } simple_logger = { version = "2.1.0", default_features = false } +[target.'cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))'.dev-dependencies] +rfd = { version = "0.11.0" } + [target.'cfg(target_os = "android")'.dependencies] # Coordinate the next winit release with android-ndk-rs: https://github.com/rust-windowing/winit/issues/1995 android-activity = "0.4.0" diff --git a/examples/window_pump_events.rs b/examples/window_pump_events.rs index 56ea0d5458..3d55a91221 100644 --- a/examples/window_pump_events.rs +++ b/examples/window_pump_events.rs @@ -48,8 +48,11 @@ fn main() -> std::process::ExitCode { break 'main ExitCode::from(exit_code as u8); } - // Sleep for 1/60 second to simulate rendering - println!("rendering"); + // Sleep for 1/60 second to simulate application work + // + // Since `pump_events` doesn't block it will be important to + // throttle the loop in the app somehow. + println!("Update()"); sleep(Duration::from_millis(16)); } } diff --git a/examples/window_pump_events_rfd.rs b/examples/window_pump_events_rfd.rs new file mode 100644 index 0000000000..5ae9ed5e82 --- /dev/null +++ b/examples/window_pump_events_rfd.rs @@ -0,0 +1,61 @@ +#![allow(clippy::single_match)] + +// Limit this example to only compatible platforms. +#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform,))] +fn main() -> std::process::ExitCode { + use std::{process::ExitCode, thread::sleep, time::Duration}; + + use simple_logger::SimpleLogger; + use winit::{ + event::{Event, PumpStatus, WindowEvent}, + event_loop::EventLoop, + platform::pump_events::EventLoopExtPumpEvents, + window::WindowBuilder, + }; + let mut event_loop = EventLoop::new(); + + SimpleLogger::new().init().unwrap(); + let window = WindowBuilder::new() + .with_title("A fantastic window!") + .build(&event_loop) + .unwrap(); + + 'main: loop { + let status = event_loop.pump_events(|event, _, control_flow| { + if let Event::WindowEvent { event, .. } = &event { + // Print only Window events to reduce noise + println!("{:?}", event); + } + + match event { + Event::WindowEvent { + event: WindowEvent::CloseRequested, + window_id, + } if window_id == window.id() => control_flow.set_exit(), + Event::MainEventsCleared => { + window.request_redraw(); + } + _ => (), + } + }); + if let PumpStatus::Exit(exit_code) = status { + break 'main ExitCode::from(exit_code as u8); + } + + // Sleep for 1/60 second to simulate rendering + println!("rendering"); + + let _dialog = rfd::MessageDialog::new() + .set_title("Msg!") + .set_description("Description!") + .set_buttons(rfd::MessageButtons::YesNo) + .show(); + + sleep(Duration::from_millis(33)); + } +} + +#[cfg(any(target_os = "ios", target_arch = "wasm32"))] +fn main() { + println!("This platform doesn't support pump_events."); +}