Skip to content

Commit 702ea15

Browse files
committed
Add EventLoopExtPumpEvents and EventLoopExtRunOnDemand
This adds two new extensions for running a Winit event loop which will replace `EventLoopExtRunReturn` The `run_return` API is trying to solve multiple problems and address multiple, unrelated, use cases but in doing so it is not succeeding at addressing any of them fully. The notable use cases we have are: 1. Applications want to be able to implement their own external event loop and call some Winit API to poll / pump events, once per iteration of their own loop, without blocking the outer, external loop. Addressing rust-windowing#2706 2. Applications want to be able to re-run separate instantiations of some Winit-based GUI and want to allow the event loop to exit with a status, and then later be able to run the loop again for a new instantiation of their GUI. Addressing rust-windowing#2431 It's very notable that these use cases can't be supported across all platforms and so they are extensions, similar to `EventLoopExtRunReturn` The intention is to support these extensions on: - Windows - Linux (X11 + Wayland) - macOS - Android These extensions aren't compatible with Web or iOS though. Each method of running the loop will behave consistently in terms of how `NewEvents(Init)`, `Resumed` and `LoopDestroyed` events are dispatched (so portable application code wouldn't necessarily need to have any awareness of which method of running the loop was being used) Once all backends have support for these extensions then we can remove `EventLoopExtRunReturn` For simplicitly, the extensions are documented with the assumption that the above platforms will be supported. This patch makes no functional change, it only introduces these new extensions so we can then handle adding platform-specific backends in separate pull requests, so the work can be landed in stages.
1 parent 4748890 commit 702ea15

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed

src/error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ pub enum ExternalError {
99
NotSupported(NotSupportedError),
1010
/// The OS cannot perform the operation.
1111
Os(OsError),
12+
/// The event loop can't be re-run while it's already running
13+
AlreadyRunning,
14+
/// Application has exit with an error status.
15+
ExitFailure(i32),
1216
}
1317

1418
/// The error type for when the requested operation is not supported by the backend.
@@ -59,8 +63,10 @@ impl fmt::Display for OsError {
5963
impl fmt::Display for ExternalError {
6064
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
6165
match self {
66+
ExternalError::AlreadyRunning => write!(f, "EventLoop is already running"),
6267
ExternalError::NotSupported(e) => e.fmt(f),
6368
ExternalError::Os(e) => e.fmt(f),
69+
ExternalError::ExitFailure(status) => write!(f, "Exit Failure: {status}"),
6470
}
6571
}
6672
}

src/event.rs

+8
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ pub enum StartCause {
329329
Init,
330330
}
331331

332+
/// The return status for `pump_events`
333+
pub enum PumpStatus {
334+
/// Continue running external loop
335+
Continue,
336+
/// exit external loop
337+
Exit(i32),
338+
}
339+
332340
/// Describes an event from a [`Window`].
333341
#[derive(Debug, PartialEq)]
334342
pub enum WindowEvent<'a> {

src/platform/pump_events.rs

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
use crate::{
2+
event::{Event, PumpStatus},
3+
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
4+
};
5+
6+
/// Additional methods on [`EventLoop`] for pumping events within an external event loop
7+
pub trait EventLoopExtPumpEvents {
8+
/// A type provided by the user that can be passed through [`Event::UserEvent`].
9+
type UserEvent;
10+
11+
fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus
12+
where
13+
F: FnMut(
14+
Event<'_, Self::UserEvent>,
15+
&EventLoopWindowTarget<Self::UserEvent>,
16+
&mut ControlFlow,
17+
);
18+
}
19+
20+
impl<T> EventLoopExtPumpEvents for EventLoop<T> {
21+
type UserEvent = T;
22+
23+
/// Pump the `EventLoop` to check for and dispatch pending events, without blocking
24+
///
25+
/// This API is designed to enable applications to integrate Winit into an
26+
/// external event loop, for platforms that can support this.
27+
///
28+
/// ```rust
29+
/// use winit::{event::{Event, PumpStatus, WindowEvent}, event_loop::EventLoop, window::WindowBuilder };
30+
/// use winit::platform::pump_events::EventLoopExtPumpEvents;
31+
/// let mut event_loop = EventLoop::new();
32+
///
33+
/// let window = WindowBuilder::new()
34+
/// .with_title("A fantastic window!")
35+
/// .build(&event_loop)
36+
/// .unwrap();
37+
///
38+
/// 'main: loop {
39+
/// let status = event_loop.pump_events(|event, _, control_flow| {
40+
/// match event {
41+
/// Event::WindowEvent {
42+
/// event: WindowEvent::CloseRequested,
43+
/// window_id,
44+
/// } if window_id == window.id() => control_flow.set_exit(),
45+
/// Event::MainEventsCleared => {
46+
/// window.request_redraw();
47+
/// }
48+
/// _ => (),
49+
/// }
50+
/// });
51+
/// if let PumpStatus::Exit(exit_code) = status {
52+
/// break 'main ExitCode::from(exit_code as u8);
53+
/// }
54+
///
55+
/// // Sleep for 1/60 second to simulate application work
56+
/// //
57+
/// // Since `pump_events` doesn't block it will be important to
58+
/// // throttle the loop in the app somehow.
59+
/// println!("Update()");
60+
/// sleep(Duration::from_millis(16));
61+
/// }
62+
/// ```
63+
///
64+
/// **Note:** This is not a portable API, and its usage involves a number of
65+
/// caveats and trade offs that should be considered before using this API!
66+
///
67+
/// You almost certainly shouldn't use this API, unless you absolutely know it's
68+
/// the only practical option you have.
69+
///
70+
/// # Synchronous events
71+
///
72+
/// Some events _must_ only be handled synchronously via the closure that
73+
/// is passed to Winit so that the handler will also be synchronized with
74+
/// the window system and operating system.
75+
///
76+
/// This is because some events are driven by a window system callback
77+
/// where the window systems expects the application to have handled the
78+
/// event before returning.
79+
///
80+
/// **These events can not be buffered and handled outside of the closure
81+
/// passed to Winit**
82+
///
83+
/// As a general rule it is not recommended to ever buffer events to handle
84+
/// them outside of the closure passed to Winit since it's difficult to
85+
/// provide guarantees about which events are safe to buffer across all
86+
/// operating systems.
87+
///
88+
/// Notable events that will certainly create portability problems if
89+
/// buffered and handled outside of Winit include:
90+
/// - `RenderRequested` events, used to schedule rendering.
91+
///
92+
/// macOS for example uses a `drawRect` callback to drive rendering
93+
/// within applications and expects rendering to be finished before
94+
/// the `drawRect` callback returns.
95+
///
96+
/// For portability it's strongly recommended that applications should
97+
/// keep their rendering inside the closure provided to Winit.
98+
/// - Any lifecycle events, such as `Suspended` / `Resumed`.
99+
///
100+
/// The handling of these events needs to be synchronized with the
101+
/// operating system and it would never be appropriate to buffer a
102+
/// notification that your application has been suspended or resumed and
103+
/// then handled that later since there would always be a chance that
104+
/// other lifecycle events occur while the event is buffered.
105+
///
106+
/// # Supported Platforms
107+
/// - Windows
108+
/// - Linux
109+
/// - MacOS
110+
/// - Android
111+
///
112+
/// # Unsupported Platforms
113+
/// - Web: This API is fundamentally incompatible with the event-based way in which
114+
/// Web browsers work because it's not possible to have a long-running external
115+
/// loop that would block the browser and there is nothing that can be
116+
/// polled to ask for new new events. Events are delivered via callbacks based
117+
/// on an event loop that is internal to the browser itself.
118+
/// - iOS: It's not possible to stop and start an `NSApplication` repeatedly on iOS so
119+
/// there's no way to support the same approach to polling as on MacOS.
120+
///
121+
/// # Platform-specific
122+
/// - **Windows**: The implementation will use `PeekMessage` when checking for
123+
/// window messages to avoid blocking your external event loop.
124+
///
125+
/// - **MacOS**: The implementation works in terms of stopping the global `NSApp`
126+
/// whenever the application `RunLoop` indicates that it is preparing to block
127+
/// and wait for new events.
128+
///
129+
/// This is very different to the polling APIs that are available on other
130+
/// platforms (the lower level polling primitives on MacOS are private
131+
/// implementation details for `NSApp` which aren't accessible to application
132+
/// developers)
133+
///
134+
/// It's likely this will be less efficient than polling on other OSs and
135+
/// it also means the `NSApp` is stopped while outside of the Winit
136+
/// event loop - and that's observable (for example to crates like `rfd`)
137+
/// because the `NSApp` is global state.
138+
///
139+
/// If you render outside of Winit you are likely to see window resizing artifacts
140+
/// since MacOS expects applications to render synchronously during any `drawRect`
141+
/// callback.
142+
fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus
143+
where
144+
F: FnMut(
145+
Event<'_, Self::UserEvent>,
146+
&EventLoopWindowTarget<Self::UserEvent>,
147+
&mut ControlFlow,
148+
),
149+
{
150+
self.event_loop.pump_events(event_handler)
151+
}
152+
}

src/platform/run_ondemand.rs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use crate::{
2+
error::ExternalError,
3+
event::Event,
4+
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
5+
};
6+
7+
/// Additional methods on [`EventLoop`] to return control flow to the caller.
8+
pub trait EventLoopExtRunOnDemand {
9+
/// A type provided by the user that can be passed through [`Event::UserEvent`].
10+
type UserEvent;
11+
12+
/// Runs the event loop in the calling thread and calls the given `event_handler` closure
13+
/// to dispatch any window system events.
14+
///
15+
/// Unlike [`EventLoop::run`], this function accepts non-`'static` (i.e. non-`move`) closures
16+
/// and it is possible to return control back to the caller without
17+
/// consuming the `EventLoop` (by setting the `control_flow` to [`ControlFlow::Exit`]) and
18+
/// so the event loop can be re-run after it has exit.
19+
///
20+
/// It's expected that each run of the loop will be for orthogonal instantiations of your
21+
/// Winit application, but internally each instantiation may re-use some common window
22+
/// system resources, such as a display server connection.
23+
///
24+
/// This API is not designed to run an event loop in bursts that you can exit from and return
25+
/// to while maintaining the full state of your application. (If you need something like this
26+
/// you can look at the [`pump_events()`] API)
27+
///
28+
/// Each time `run_ondemand` is called the `event_handler` can expect to receive a
29+
/// `NewEvents(Init)` and `Resumed` event (even on platforms that have no suspend/resume
30+
/// lifecycle) - which can be used to consistently initialize application state.
31+
///
32+
/// See the [`ControlFlow`] docs for information on how changes to `&mut ControlFlow` impact the
33+
/// event loop's behavior.
34+
///
35+
/// # Caveats
36+
/// - This extension isn't available on all platforms, since it's not always possible to
37+
/// return to the caller (specifically this is impossible on iOS and Web - though with
38+
/// the Web backend it is possible to use `spawn()` more than once instead).
39+
/// - No [`Window`] state can be carried between separate runs of the event loop.
40+
///
41+
/// You are strongly encouraged to use `run` for portability, unless you specifically need
42+
/// the ability to re-run a single event loop more than once
43+
///
44+
/// # Supported Platforms
45+
/// - Windows
46+
/// - Linux
47+
/// - macOS
48+
/// - Android
49+
///
50+
/// # Unsupported Platforms
51+
/// - Web: This API is fundamentally incompatible with the event-based way in which
52+
/// Web browsers work because it's not possible to have a long-running external
53+
/// loop that would block the browser and there is nothing that can be
54+
/// polled to ask for new new events. Events are delivered via callbacks based
55+
/// on an event loop that is internal to the browser itself.
56+
/// - iOS: It's not possible to stop and start an `NSApplication` repeatedly on iOS.
57+
fn run_ondemand<F>(&mut self, event_handler: F) -> Result<(), ExternalError>
58+
where
59+
F: FnMut(
60+
Event<'_, Self::UserEvent>,
61+
&EventLoopWindowTarget<Self::UserEvent>,
62+
&mut ControlFlow,
63+
);
64+
}
65+
66+
impl<T> EventLoopExtRunOnDemand for EventLoop<T> {
67+
type UserEvent = T;
68+
69+
fn run_ondemand<F>(&mut self, event_handler: F) -> Result<(), ExternalError>
70+
where
71+
F: FnMut(
72+
Event<'_, Self::UserEvent>,
73+
&EventLoopWindowTarget<Self::UserEvent>,
74+
&mut ControlFlow,
75+
),
76+
{
77+
self.event_loop.run_ondemand(event_handler)
78+
}
79+
}

0 commit comments

Comments
 (0)