Skip to content

Commit b0d9048

Browse files
committed
Add examples/window_pump_events_rfd
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.
1 parent bd558d8 commit b0d9048

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ smol_str = "0.2.0"
6363
image = { version = "0.24.0", default-features = false, features = ["png"] }
6464
simple_logger = { version = "2.1.0", default_features = false }
6565

66+
[target.'cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))'.dev-dependencies]
67+
rfd = { version = "0.11.0" }
68+
6669
[target.'cfg(target_os = "android")'.dependencies]
6770
# Coordinate the next winit release with android-ndk-rs: https://github.com/rust-windowing/winit/issues/1995
6871
android-activity = "0.4.0"

examples/window_pump_events.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ fn main() -> std::process::ExitCode {
4848
break 'main ExitCode::from(exit_code as u8);
4949
}
5050

51-
// Sleep for 1/60 second to simulate rendering
52-
println!("rendering");
51+
// Sleep for 1/60 second to simulate application work
52+
//
53+
// Since `pump_events` doesn't block it will be important to
54+
// throttle the loop in the app somehow.
55+
println!("Update()");
5356
sleep(Duration::from_millis(16));
5457
}
5558
}

examples/window_pump_events_rfd.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#![allow(clippy::single_match)]
2+
3+
// Limit this example to only compatible platforms.
4+
#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform,))]
5+
fn main() -> std::process::ExitCode {
6+
use std::{process::ExitCode, thread::sleep, time::Duration};
7+
8+
use simple_logger::SimpleLogger;
9+
use winit::{
10+
event::{Event, PumpStatus, WindowEvent},
11+
event_loop::EventLoop,
12+
platform::pump_events::EventLoopExtPumpEvents,
13+
window::WindowBuilder,
14+
};
15+
let mut event_loop = EventLoop::new();
16+
17+
SimpleLogger::new().init().unwrap();
18+
let window = WindowBuilder::new()
19+
.with_title("A fantastic window!")
20+
.build(&event_loop)
21+
.unwrap();
22+
23+
'main: loop {
24+
let status = event_loop.pump_events(|event, _, control_flow| {
25+
if let Event::WindowEvent { event, .. } = &event {
26+
// Print only Window events to reduce noise
27+
println!("{:?}", event);
28+
}
29+
30+
match event {
31+
Event::WindowEvent {
32+
event: WindowEvent::CloseRequested,
33+
window_id,
34+
} if window_id == window.id() => control_flow.set_exit(),
35+
Event::MainEventsCleared => {
36+
window.request_redraw();
37+
}
38+
_ => (),
39+
}
40+
});
41+
if let PumpStatus::Exit(exit_code) = status {
42+
break 'main ExitCode::from(exit_code as u8);
43+
}
44+
45+
// Sleep for 1/60 second to simulate rendering
46+
println!("rendering");
47+
48+
let _dialog = rfd::MessageDialog::new()
49+
.set_title("Msg!")
50+
.set_description("Description!")
51+
.set_buttons(rfd::MessageButtons::YesNo)
52+
.show();
53+
54+
sleep(Duration::from_millis(33));
55+
}
56+
}
57+
58+
#[cfg(any(target_os = "ios", target_arch = "wasm32"))]
59+
fn main() {
60+
println!("This platform doesn't support pump_events.");
61+
}

0 commit comments

Comments
 (0)