From d69e73b9f96cf9f40d0615078f8c0e659931e511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 17 Jun 2024 17:43:40 +0200 Subject: [PATCH] macOS: Check NSRunningApplication.active instead of comparing pointers When checking whether the current application was the active one during launch, we compared the pointers of the frontmost and current app, but these two can be different even for the same app (PID). This was not a problem in practice, as the result was just that we would always activate, just as we did prior to the change that introduced the code. We now check NSRunningApplication.active instead. Amends 6343caae25179b5895b4169d7b97d61293e49b0f. Pick-to: 6.7 6.5 Change-Id: Ib3557e5ea676be5291904aaa444f7ede2160e1fd Reviewed-by: Volker Hilsheimer (cherry picked from commit 0255d3a9af69c9accb23f7ac2c08b19bec9dd6a0) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm b/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm index d6421159262..81cd16a78c6 100644 --- a/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm +++ b/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm @@ -192,9 +192,8 @@ QT_USE_NAMESPACE inLaunch = false; if (qEnvironmentVariableIsEmpty("QT_MAC_DISABLE_FOREGROUND_APPLICATION_TRANSFORM")) { - auto frontmostApplication = NSWorkspace.sharedWorkspace.frontmostApplication; auto currentApplication = NSRunningApplication.currentApplication; - if (frontmostApplication != currentApplication) { + if (!currentApplication.active) { // Move the application to front to avoid launching behind the terminal. // Ignoring other apps is necessary (we must ignore the terminal), but makes // Qt apps play slightly less nice with other apps when launching from Finder @@ -202,6 +201,7 @@ QT_USE_NAMESPACE // being non-active here because another application stole activation in the // time it took us to launch from Finder, and being non-active because we were // launched from Terminal or something that doesn't activate us at all. + auto frontmostApplication = NSWorkspace.sharedWorkspace.frontmostApplication; qCDebug(lcQpaApplication) << "Launched with" << frontmostApplication << "as frontmost application. Activating" << currentApplication << "instead."; [NSApplication.sharedApplication activateIgnoringOtherApps:YES];