pump_events/
pump_events.rs

1#![allow(clippy::single_match)]
2
3// Limit this example to only compatible platforms.
4#[cfg(any(
5    windows_platform,
6    macos_platform,
7    x11_platform,
8    wayland_platform,
9    android_platform,
10))]
11fn main() -> std::process::ExitCode {
12    use std::process::ExitCode;
13    use std::thread::sleep;
14    use std::time::Duration;
15
16    use rio_window::application::ApplicationHandler;
17    use rio_window::event::WindowEvent;
18    use rio_window::event_loop::{ActiveEventLoop, EventLoop};
19    use rio_window::platform::pump_events::{EventLoopExtPumpEvents, PumpStatus};
20    use rio_window::window::{Window, WindowId};
21
22    #[path = "util/fill.rs"]
23    mod fill;
24
25    #[derive(Default)]
26    struct PumpDemo {
27        window: Option<Window>,
28    }
29
30    impl ApplicationHandler for PumpDemo {
31        fn resumed(&mut self, event_loop: &ActiveEventLoop) {
32            let window_attributes =
33                Window::default_attributes().with_title("A fantastic window!");
34            self.window = Some(event_loop.create_window(window_attributes).unwrap());
35        }
36
37        fn window_event(
38            &mut self,
39            event_loop: &ActiveEventLoop,
40            _window_id: WindowId,
41            event: WindowEvent,
42        ) {
43            println!("{event:?}");
44
45            let window = match self.window.as_ref() {
46                Some(window) => window,
47                None => return,
48            };
49
50            match event {
51                WindowEvent::CloseRequested => event_loop.exit(),
52                WindowEvent::RedrawRequested => {
53                    fill::fill_window(window);
54                    window.request_redraw();
55                }
56                _ => (),
57            }
58        }
59    }
60
61    let mut event_loop = EventLoop::new().unwrap();
62
63    tracing_subscriber::fmt::init();
64
65    let mut app = PumpDemo::default();
66
67    loop {
68        let timeout = Some(Duration::ZERO);
69        let status = event_loop.pump_app_events(timeout, &mut app);
70
71        if let PumpStatus::Exit(exit_code) = status {
72            break ExitCode::from(exit_code as u8);
73        }
74
75        // Sleep for 1/60 second to simulate application work
76        //
77        // Since `pump_events` doesn't block it will be important to
78        // throttle the loop in the app somehow.
79        println!("Update()");
80        sleep(Duration::from_millis(16));
81    }
82}
83
84#[cfg(any(ios_platform, web_platform, orbital_platform))]
85fn main() {
86    println!("This platform doesn't support pump_events.");
87}