Swizzle sendEvent:
instead of subclassing NSApplication
#4036
+162
−47
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is done to avoid order-dependent behavior that you'd otherwise encounter where
EventLoop::new
had to be called at the beginning offn main
to ensure that Winit's application was the one being registered as the main application by callingsharedApplication
.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:
NSApplication
, and exposing a method for users to call themselves if they overrideNSApplication
. 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.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.changelog
module if knowledge of this change could be valuable to usersNSApplication
that I doubt it makes sense to provide an example. We already have an example of overriding theNSApplicationDelegate
, that should be enough.