Skip to content

Commit

Permalink
Windows: Implement EventLoopExtPumpEvents and EventLoopExtRunOnDemand
Browse files Browse the repository at this point in the history
A surprising amount of work was required to enable these extensions
on Windows.

I had originally assumed that pump_events was going to be very similar
to run except would use PeekMessageW instead of GetMessageW to avoid
blocking the external loop but I found the Windows backend broke
several assumptions I had.

Overall I think these changes can hopefully be considered a quite a
significant simplification (I think it's a net deletion of a fair amount
of code) and I think it also helps bring it into slightly closer alignment
with other backends too

Key changes:
- I have removed the `wait_thread` that was a fairly fiddly way of handling
  `ControlFlow::WaitUntil` timeouts in favor of using `SetTimer` which works
  with the same messages picked up by `GetMessage` and `PeekMessage`.
- I have removed the ordering guarantees between `MainEventsCleared`,
  `RedrawRequested` and `RedrawEventsCleared` events due to the complexity in
  maintaining this artificial ordering, which is already not supported
  consistently across backends anyway (in particular this ordering already
  isn't compatible with how MacOS / iOS work).
- `RedrawRequested` events are now directly dispatched via `WM_PAINT` messages
  - comparable to how `RedrawRequested` is dispatched via `drawRect` in the
  MacOS backend.
- I have re-worked how `NewEvents`, `MainEventsCleared`, and `RedrawEventsCleared`
  get dispatched to be more in line with the MacOS backend and also more in line
  with how we have recently discussed defining them for all platforms.

  `NewEvents` is conceptually delivered when the event loop "wakes up" and
  `MainEventsCleared` gets dispatched when the event loop is about to ask the
  OS to wait for new events.

  This is a more portable model, and is already how these events work in the
  MacOS backend.

  `RedrawEventsCleared` are just delivered after `MainEventsCleared` but this
  event no longer has a useful meaning.

Probably the most controversial thing here is that this "breaks" the ordering
rules for redraw event handling, but since my changes interacted with how the
order is maintained I was very reluctant to figure out how to continue
maintaining something that we have recently been discussing changing:

rust-windowing#2640.

Additionally, since the MacOS backend already doesn't strictly maintain this
order it's somewhat academic to see this as a breakage if Winit applications
can't really rely on it already.

This updates the documentation for `request_redraw()` to reflect that we
no longer guarantee that `RedrawRequested` events must be dispatched
after `MainEventsCleared`.
  • Loading branch information
rib authored and kchibisov committed Aug 14, 2023
1 parent c81a146 commit 96d37a9
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 432 deletions.
8 changes: 4 additions & 4 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//!
//! And the following platform-specific modules:
//!
//! - `run_ondemand` (available on `android`)
//! - `pump_events` (available on `android`)
//! - `run_ondemand` (available on `windows`, `android`)
//! - `pump_events` (available on `windows`, `android`)
//! - `run_return` (available on `windows`, `unix`, `macos`, and `android`)
//!
//! However only the module corresponding to the platform you're compiling to will be available.
Expand All @@ -36,10 +36,10 @@ pub mod windows;
#[cfg(x11_platform)]
pub mod x11;

#[cfg(any(android_platform))]
#[cfg(any(windows_platform, android_platform))]
pub mod run_ondemand;

#[cfg(any(android_platform,))]
#[cfg(any(windows_platform, android_platform,))]
pub mod pump_events;

#[cfg(any(
Expand Down
Loading

0 comments on commit 96d37a9

Please sign in to comment.