rio_window/platform/
run_on_demand.rs

1use crate::application::ApplicationHandler;
2use crate::error::EventLoopError;
3use crate::event::Event;
4use crate::event_loop::{self, ActiveEventLoop, EventLoop};
5
6#[cfg(doc)]
7use crate::{platform::pump_events::EventLoopExtPumpEvents, window::Window};
8
9/// Additional methods on [`EventLoop`] to return control flow to the caller.
10pub trait EventLoopExtRunOnDemand {
11    /// A type provided by the user that can be passed through [`Event::UserEvent`].
12    type UserEvent: 'static;
13
14    /// See [`run_app_on_demand`].
15    ///
16    /// [`run_app_on_demand`]: Self::run_app_on_demand
17    #[deprecated = "use EventLoopExtRunOnDemand::run_app_on_demand"]
18    fn run_on_demand<F>(&mut self, event_handler: F) -> Result<(), EventLoopError>
19    where
20        F: FnMut(Event<Self::UserEvent>, &ActiveEventLoop);
21
22    /// Run the application with the event loop on the calling thread.
23    ///
24    /// Unlike [`EventLoop::run_app`], this function accepts non-`'static` (i.e. non-`move`)
25    /// closures and it is possible to return control back to the caller without
26    /// consuming the `EventLoop` (by using [`exit()`]) and
27    /// so the event loop can be re-run after it has exit.
28    ///
29    /// It's expected that each run of the loop will be for orthogonal instantiations of your
30    /// Winit application, but internally each instantiation may re-use some common window
31    /// system resources, such as a display server connection.
32    ///
33    /// This API is not designed to run an event loop in bursts that you can exit from and return
34    /// to while maintaining the full state of your application. (If you need something like this
35    /// you can look at the [`EventLoopExtPumpEvents::pump_app_events()`] API)
36    ///
37    /// Each time `run_app_on_demand` is called the startup sequence of `init`, followed by
38    /// `resume` is being preserved.
39    ///
40    /// See the [`set_control_flow()`] docs on how to change the event loop's behavior.
41    ///
42    /// # Caveats
43    /// - This extension isn't available on all platforms, since it's not always possible to return
44    ///   to the caller (specifically this is impossible on iOS and Web - though with the Web
45    ///   backend it is possible to use `EventLoopExtWebSys::spawn()`[^1] more than once instead).
46    /// - No [`Window`] state can be carried between separate runs of the event loop.
47    ///
48    /// You are strongly encouraged to use [`EventLoop::run_app()`] for portability, unless you
49    /// specifically need the ability to re-run a single event loop more than once
50    ///
51    /// # Supported Platforms
52    /// - Windows
53    /// - Linux
54    /// - macOS
55    /// - Android
56    ///
57    /// # Unsupported Platforms
58    /// - **Web:**  This API is fundamentally incompatible with the event-based way in which Web
59    ///   browsers work because it's not possible to have a long-running external loop that would
60    ///   block the browser and there is nothing that can be polled to ask for new events. Events
61    ///   are delivered via callbacks based on an event loop that is internal to the browser itself.
62    /// - **iOS:** It's not possible to stop and start an `UIApplication` repeatedly on iOS.
63    #[cfg_attr(not(web_platform), doc = "[^1]: `spawn()` is only available on `wasm` platforms.")]
64    #[rustfmt::skip]
65    ///
66    /// [`exit()`]: ActiveEventLoop::exit()
67    /// [`set_control_flow()`]: ActiveEventLoop::set_control_flow()
68    fn run_app_on_demand<A: ApplicationHandler<Self::UserEvent>>(
69        &mut self,
70        app: &mut A,
71    ) -> Result<(), EventLoopError> {
72        #[allow(deprecated)]
73        self.run_on_demand(|event, event_loop| {
74            event_loop::dispatch_event_for_app(app, event_loop, event)
75        })
76    }
77}
78
79impl<T> EventLoopExtRunOnDemand for EventLoop<T> {
80    type UserEvent = T;
81
82    fn run_on_demand<F>(&mut self, event_handler: F) -> Result<(), EventLoopError>
83    where
84        F: FnMut(Event<Self::UserEvent>, &ActiveEventLoop),
85    {
86        self.event_loop.window_target().clear_exit();
87        self.event_loop.run_on_demand(event_handler)
88    }
89}
90
91impl ActiveEventLoop {
92    /// Clear exit status.
93    pub(crate) fn clear_exit(&self) {
94        self.p.clear_exit()
95    }
96}
97
98/// ```compile_fail
99/// use rio_window::event_loop::EventLoop;
100/// use rio_window::platform::run_on_demand::EventLoopExtRunOnDemand;
101///
102/// let mut event_loop = EventLoop::new().unwrap();
103/// event_loop.run_on_demand(|_, _| {
104///     // Attempt to run the event loop re-entrantly; this must fail.
105///     event_loop.run_on_demand(|_, _| {});
106/// });
107/// ```
108#[allow(dead_code)]
109fn test_run_on_demand_cannot_access_event_loop() {}