Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swizzle sendEvent: instead of subclassing NSApplication #4036

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

madsmtm
Copy link
Member

@madsmtm madsmtm commented Dec 8, 2024

This is done to avoid order-dependent behavior that you'd otherwise encounter where EventLoop::new had to be called at the beginning of fn main to ensure that Winit's application was the one being registered as the main application by calling sharedApplication.

Fixes #3772.

This should also make it (more) possible to use multiple versions of Winit in the same application (though that's still untested and officially unsupported).

Finally, it should allow the user to override NSApplication themselves if they need to do that for some reason.

The solution chosen here, method swizzling, is a bit magical (though fully supported by the runtime), alternatives include:

  • Attempting to override if NSApplication, and exposing a method for users to call themselves if they override NSApplication. This would be quite confusing, since suddenly overriding the application class would disable parts of Winit's behaviour (unless you knew to call this exposed method), and it still wouldn't fix the ordering issue.
  • Creating a subclass of the user's NSApplication, and class-swizzling that in. This is still swizzling, and would just be more work to do (note also that we wouldn't be able to add instance variables to such a class, since we don't allocate it ourselves).

Just to re-confirm that overriding sendEvent: is a good idea by itself, I'll note that this is also what's done in other UI frameworks, including the SDL and FLTK.

  • Tested on all platforms changed
  • Added an entry to the changelog module if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
    • It is rare enough to want to override NSApplication that I doubt it makes sense to provide an example. We already have an example of overriding the NSApplicationDelegate, that should be enough.
  • Created or updated an example program if it would help users understand this functionality

@madsmtm madsmtm added B - bug Dang, that shouldn't have happened DS - macos labels Dec 8, 2024
@madsmtm madsmtm added this to the Version 0.31.0 milestone Dec 8, 2024
@madsmtm madsmtm force-pushed the madsmtm/swizzle-app branch from 7cad081 to 1c80f39 Compare December 8, 2024 23:27
This is done to avoid order-dependent behavior that you'd otherwise
encounter where `EventLoop::new` had to be called at the beginning of
`fn main` to ensure that Winit's application was the one being
registered as the main application by calling `sharedApplication`.

Fixes #3772.

This should also make it (more) possible to use multiple versions of
Winit in the same application (though that's still untested).

Finally, it should allow the user to override `NSApplication` themselves
if they need to do that for some reason.
@madsmtm madsmtm force-pushed the madsmtm/swizzle-app branch from 1c80f39 to 62cb23f Compare December 9, 2024 00:00
Copy link
Member

@kchibisov kchibisov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's fine to backport it?

@@ -76,6 +76,7 @@ changelog entry.
- Added `Window::surface_position`, which is the position of the surface inside the window.
- Added `Window::safe_area`, which describes the area of the surface that is unobstructed.
- On X11, Wayland, Windows and macOS, improved scancode conversions for more obscure key codes.
- On macOS, no longer need control of the main `NSApplication` class (which means you can now override it yourself).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be in the changed.

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 - macos
2 participants