azul_winit/platform/run_return.rs
1#![cfg(any(
2 target_os = "windows",
3 target_os = "macos",
4 target_os = "android",
5 target_os = "linux",
6 target_os = "dragonfly",
7 target_os = "freebsd",
8 target_os = "netbsd",
9 target_os = "openbsd"
10))]
11
12use crate::{
13 event::Event,
14 event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
15};
16
17/// Additional methods on `EventLoop` to return control flow to the caller.
18pub trait EventLoopExtRunReturn {
19 /// A type provided by the user that can be passed through `Event::UserEvent`.
20 type UserEvent;
21
22 /// Initializes the `winit` event loop.
23 ///
24 /// Unlike `run`, this function accepts non-`'static` (i.e. non-`move`) closures and returns
25 /// control flow to the caller when `control_flow` is set to `ControlFlow::Exit`.
26 ///
27 /// # Caveats
28 /// Despite its appearance at first glance, this is *not* a perfect replacement for
29 /// `poll_events`. For example, this function will not return on Windows or macOS while a
30 /// window is getting resized, resulting in all application logic outside of the
31 /// `event_handler` closure not running until the resize operation ends. Other OS operations
32 /// may also result in such freezes. This behavior is caused by fundamental limitations in the
33 /// underlying OS APIs, which cannot be hidden by `winit` without severe stability repercussions.
34 ///
35 /// You are strongly encouraged to use `run`, unless the use of this is absolutely necessary.
36 fn run_return<F>(&mut self, event_handler: F)
37 where
38 F: FnMut(
39 Event<'_, Self::UserEvent>,
40 &EventLoopWindowTarget<Self::UserEvent>,
41 &mut ControlFlow,
42 );
43}
44
45impl<T> EventLoopExtRunReturn for EventLoop<T> {
46 type UserEvent = T;
47
48 fn run_return<F>(&mut self, event_handler: F)
49 where
50 F: FnMut(
51 Event<'_, Self::UserEvent>,
52 &EventLoopWindowTarget<Self::UserEvent>,
53 &mut ControlFlow,
54 ),
55 {
56 self.event_loop.run_return(event_handler)
57 }
58}