-
Notifications
You must be signed in to change notification settings - Fork 924
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The way that the `rfd` crate and Winit interact is poorly defined and fragile. Applications are even more likely to break assumptions if they have an external event loop based on `pump_events`. This example can be used to smoke test how the use of the `rfd` crate currently interacts with applications based on an external event loop.
- Loading branch information
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} |