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 <volker.hilsheimer@qt.io>
(cherry picked from commit 0255d3a9af69c9accb23f7ac2c08b19bec9dd6a0)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2024-06-17 17:43:40 +02:00 committed by Qt Cherry-pick Bot
parent 5b5d6dfa49
commit d69e73b9f9

View File

@ -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];