[network/kdeconnect-kde] app: macos: focus app when opened from indicator

Albert Vaca Cintora <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit c0d17c854546e15332123c62c9d049b157f545e8 by Albert Vaca Cintora, on behalf of Logan Rosen.
Committed on 26/07/2026 at 21:30.
Pushed by albertvaka into branch 'master'.

macos: focus app when opened from indicator

M  +8    -0    app/CMakeLists.txt
A  +11   -0    app/macosforegroundapp.h     [License: GPL(3+eV) GPL(v3.0) GPL(v2.0)]
A  +24   -0    app/macosforegroundapp.mm
M  +9    -0    app/main.cpp

https://invent.kde.org/network/kdeconnect-kde/-/commit/c0d17c854546e15332123c62c9d049b157f545e8

diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index d9f565845..824bf63dc 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -24,6 +24,10 @@ add_executable(kdeconnect-app
     ${kdeconnect_custom_icons_SRCS}
 )
 
+if (APPLE)
+    target_sources(kdeconnect-app PRIVATE macosforegroundapp.mm)
+endif()
+
 ecm_add_qml_module(kdeconnect-app URI org.kde.kdeconnect.app)
 
 ecm_target_qml_sources(kdeconnect-app SOURCES
@@ -46,6 +50,10 @@ ecm_target_qml_sources(kdeconnect-app SOURCES
 
 target_link_libraries(kdeconnect-app PRIVATE Qt::Quick Qt::QuickControls2 Qt::Widgets KF6::CoreAddons KF6::I18n KF6::ColorScheme KF6::Crash KF6::ItemModels KF6::DBusAddons KF6::WindowSystem)
 
+if (APPLE)
+    target_link_libraries(kdeconnect-app PRIVATE "-framework AppKit")
+endif()
+
 set_target_properties(kdeconnect-app PROPERTIES MACOSX_BUNDLE FALSE)
 install(TARGETS kdeconnect-app ${KDECONNECT_INSTALL_TARGETS_DEFAULT_ARGS_BUNDLE})
 install(PROGRAMS org.kde.kdeconnect.app.desktop DESTINATION ${KDE_INSTALL_APPDIR})
diff --git a/app/macosforegroundapp.h b/app/macosforegroundapp.h
new file mode 100644
index 000000000..3dfb589eb
--- /dev/null
+++ b/app/macosforegroundapp.h
@@ -0,0 +1,11 @@
+/**
+ * SPDX-FileCopyrightText: 2026 KDE Connect Team
+ *
+ * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
+ */
+
+#pragma once
+
+class QWindow;
+
+void activateWindowForMacOS(QWindow *window);
diff --git a/app/macosforegroundapp.mm b/app/macosforegroundapp.mm
new file mode 100644
index 000000000..92692071c
--- /dev/null
+++ b/app/macosforegroundapp.mm
@@ -0,0 +1,24 @@
+/**
+ * SPDX-FileCopyrightText: 2026 KDE Connect Team
+ *
+ * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
+ */
+
+#include "macosforegroundapp.h"
+
+#include <QWindow>
+
+#import <AppKit/NSApplication.h>
+
+void activateWindowForMacOS(QWindow *window)
+{
+    if (window == nullptr) {
+        return;
+    }
+
+    [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
+    [NSApp activateIgnoringOtherApps:YES];
+    window->show();
+    window->raise();
+    window->requestActivate();
+}
diff --git a/app/main.cpp b/app/main.cpp
index 41028eada..6e5d95b33 100644
--- a/app/main.cpp
+++ b/app/main.cpp
@@ -23,10 +23,18 @@
 #include <KLocalizedString>
 #include <KWindowSystem>
 
+#if defined(Q_OS_MAC)
+#include "macosforegroundapp.h"
+#endif
+
 static void raiseWindow(QWindow *window)
 {
+#if defined(Q_OS_MAC)
+    activateWindowForMacOS(window);
+#else
     KWindowSystem::updateStartupId(window);
     KWindowSystem::activateWindow(window);
+#endif
 }
 
 int main(int argc, char *argv[])
@@ -124,6 +132,7 @@ int main(int argc, char *argv[])
         auto mo = obj->metaObject();
         mo->invokeMethod(obj, "openDevice", Qt::QueuedConnection, Q_ARG(QVariant, device), Q_ARG(QVariant, config));
     }
+    raiseWindow(obj);
     QObject::connect(&dbusService, &KDBusService::activateRequested, obj, [obj](const QStringList & /*arguments*/, const QString & /*workingDirectory*/) {
         raiseWindow(obj);
     });
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.