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