Skip to content

Commit bd558d8

Browse files
committed
Add examples/window_ondemand
A minimal example that shows an application running the event loop more than once via `run_ondemand`
1 parent e73dab8 commit bd558d8

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

examples/window_ondemand.rs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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() -> Result<(), impl std::error::Error> {
6+
use simple_logger::SimpleLogger;
7+
8+
use winit::{
9+
event::{Event, WindowEvent},
10+
event_loop::EventLoop,
11+
platform::run_ondemand::EventLoopExtRunOnDemand,
12+
window::{Window, WindowBuilder},
13+
};
14+
15+
#[derive(Default)]
16+
struct App {
17+
window: Option<Window>,
18+
}
19+
20+
SimpleLogger::new().init().unwrap();
21+
let mut event_loop = EventLoop::new();
22+
23+
{
24+
let mut app = App::default();
25+
26+
event_loop.run_ondemand(move |event, event_loop, control_flow| {
27+
control_flow.set_wait();
28+
println!("Run 1: {:?}", event);
29+
30+
if let Some(window) = &app.window {
31+
match event {
32+
Event::WindowEvent {
33+
event: WindowEvent::CloseRequested,
34+
window_id,
35+
} => {
36+
if window_id == window.id() {
37+
control_flow.set_exit()
38+
}
39+
}
40+
Event::MainEventsCleared => {
41+
window.request_redraw();
42+
}
43+
_ => (),
44+
}
45+
} else if let Event::Resumed = event {
46+
app.window = Some(
47+
WindowBuilder::new()
48+
.with_title("Fantastic window number one!")
49+
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
50+
.build(event_loop)
51+
.unwrap(),
52+
);
53+
}
54+
})?;
55+
}
56+
57+
{
58+
let mut app = App::default();
59+
60+
event_loop.run_ondemand(move |event, event_loop, control_flow| {
61+
control_flow.set_wait();
62+
println!("Run 2: {:?}", event);
63+
64+
if let Some(window) = &app.window {
65+
match event {
66+
Event::WindowEvent {
67+
event: WindowEvent::CloseRequested,
68+
window_id,
69+
} => {
70+
if window_id == window.id() {
71+
control_flow.set_exit()
72+
}
73+
}
74+
Event::MainEventsCleared => {
75+
window.request_redraw();
76+
}
77+
_ => (),
78+
}
79+
} else if let Event::Resumed = event {
80+
app.window = Some(
81+
WindowBuilder::new()
82+
.with_title("Fantastic window number two!")
83+
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
84+
.build(event_loop)
85+
.unwrap(),
86+
);
87+
}
88+
})
89+
}
90+
}

0 commit comments

Comments
 (0)