[network/kdeconnect-kde/release/26.08] app: macos: focus app when opened from indicator
Albert Vaca Cintora <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 3b533090db42ef51b4ae4ee5392dc620a2fd8b5b by Albert Vaca Cintora, on behalf of Logan Rosen.
Committed on 13/08/2026 at 20:08.
Pushed by albertvaka into branch 'release/26.08'.
macos: focus app when opened from indicator
(cherry picked from commit c0d17c854546e15332123c62c9d049b157f545e8)
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/3b533090db42ef51b4ae4ee5392dc620a2fd8b5b
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index aa2f253df..f319bbaee 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
@@ -45,6 +49,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);
});