Skip to content

macOS: Improve pump_app_events#4155

Open
madsmtm wants to merge 2 commits intomasterfrom
madsmtm/janky-pump-events
Open

macOS: Improve pump_app_events#4155
madsmtm wants to merge 2 commits intomasterfrom
madsmtm/janky-pump-events

Conversation

@madsmtm
Copy link
Copy Markdown
Member

@madsmtm madsmtm commented Mar 2, 2025

The event loop was reworked in #2767, but unfortunately I didn't know enough about run loops back then to know how to do this kind of stuff correctly. I think I have a pretty good idea now, in short, it works as follows:

AppKit retains a queue of events, which can be popped from using -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:], and then passed to -[NSApplication sendEvent:] (though you're free to filter them if you so desire). This allows us to process events one at a time. It is suboptimal because certain actions like resizing will enter a "modal" mode (similar to this issue), which will prevent events from being delivered while that is ongoing; but that's the cost of pump_app_events.

The implementation is now similar to the implementation we had all the way back in v0.19, and is very close to SDL's approach.

Probably improves #1418.

  • Tested on all platforms changed
  • Added an entry to the changelog module if knowledge of this change could be valuable to users

This is unnecessary, unwinding in CF observer callbacks is safe (and is
safe in Rust after the introduction of `extern "C-unwind"`).

Panicking elsewhere (such as in NSNotificationCenter callbacks or
delegate methods) _may_ still lead to an abort, if AppKit tries to catch
it with libc++, since Rust panics are not compatible with those.

That's "just" a quality-of-implementation detail of current Rust though,
not an inherent limitation, and should really be solved upstream.
@madsmtm madsmtm added B - bug Dang, that shouldn't have happened DS - appkit Affects the AppKit/macOS backend labels Mar 2, 2025
@madsmtm madsmtm force-pushed the madsmtm/janky-pump-events branch 2 times, most recently from 07bdc91 to 42ea395 Compare March 2, 2025 07:58
Use the system's mechanisms for pumping events, namely
`nextEventMatchingMask:untilDate:inMode:dequeue:`. This still has a few
problems as detailed in the documentation for `pump_app_events` (which
is why we switched away from it all the way back in Winit v0.20), but it
doesn't have nearly as many problems as the current implementation does.
@madsmtm madsmtm force-pushed the madsmtm/janky-pump-events branch from 42ea395 to 48974ed Compare March 3, 2025 07:47
@rib
Copy link
Copy Markdown
Contributor

rib commented Mar 7, 2025

Out of interest I'd be curious to understand what's broken with what we did for #2767? (the issue description just says that you didn't know how to do it correctly before, but doesn't point to or describe some concrete issues that will be addressed by the change).

It sounds like the blocking on resize events could be a pretty serious issue with this API, that looks like it was only addressed previously by EventLoop 2.0: #219

@madsmtm madsmtm force-pushed the madsmtm/apple-remove-panic branch from 82021cc to 05d3e71 Compare May 23, 2025 13:13
Base automatically changed from madsmtm/apple-remove-panic to master May 23, 2025 13:53
@madsmtm madsmtm changed the title macOS: Fix pump_app_events macOS: Improve pump_app_events Sep 5, 2025
@madsmtm madsmtm mentioned this pull request Sep 9, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B - bug Dang, that shouldn't have happened DS - appkit Affects the AppKit/macOS backend

Development

Successfully merging this pull request may close these issues.

2 participants