qt: Avoid implicit NSApplication instantiation

`[NSApplication sharedApplication]` creates the shared application
object if it does not yet exist. When running with the `minimal` or
`offscreen` QPA plugins, which is common for testing purposes, the Cocoa
platform plugin never creates it, so these call sites were instantiating
`NSApplication` as a side effect.

Use the `NSApp` global instead and return early when it is `nil`.
This commit is contained in:
Hennadii Stepanov
2026-07-20 15:00:40 +01:00
parent 006f8f7d49
commit c8b2aeb226

View File

@@ -20,7 +20,8 @@ bool dockClickHandler(id self, SEL _cmd, ...) {
}
void setupDockClickHandler() {
Class delClass = (Class)[[[NSApplication sharedApplication] delegate] class];
if (NSApp == nil) return;
Class delClass = (Class)[[NSApp delegate] class];
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
}
@@ -49,5 +50,6 @@ void MacDockIconHandler::cleanup()
*/
void ForceActivation()
{
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
if (NSApp == nil) return;
[NSApp activateIgnoringOtherApps:YES];
}