From 6c19e733673e24684707836fb2c9e0f45d1ac3f9 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Tue, 11 Oct 2022 21:21:09 +0200 Subject: [PATCH] Fix Wayland window activation when attaching to an existing instance The application launching Dolphin passes a token via the XDG_ACTIVATION_TOKEN environment variable We need to pass that to the running instance so that it can use it to raise itself --- src/dbusinterface.cpp | 6 ++---- src/dolphinmainwindow.cpp | 12 +++++++++--- src/dolphinmainwindow.h | 2 +- src/global.cpp | 5 +++-- src/global.h | 2 +- src/main.cpp | 10 +++++++++- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/dbusinterface.cpp b/src/dbusinterface.cpp index 0d43dfc231..8d5bf7645b 100644 --- a/src/dbusinterface.cpp +++ b/src/dbusinterface.cpp @@ -31,9 +31,8 @@ void DBusInterface::ShowFolders(const QStringList& uriList, const QString& start if (urls.isEmpty()) { return; } - KWindowSystem::setCurrentXdgActivationToken(startUpId); const auto serviceName = isDaemon() ? QString() : QStringLiteral("org.kde.dolphin-%1").arg(QCoreApplication::applicationPid()); - if(!Dolphin::attachToExistingInstance(urls, false, GeneralSettings::splitView(), serviceName)) { + if(!Dolphin::attachToExistingInstance(urls, false, GeneralSettings::splitView(), serviceName, startUpId)) { Dolphin::openNewWindow(urls); } } @@ -44,9 +43,8 @@ void DBusInterface::ShowItems(const QStringList& uriList, const QString& startUp if (urls.isEmpty()) { return; } - KWindowSystem::setCurrentXdgActivationToken(startUpId); const auto serviceName = isDaemon() ? QString() : QStringLiteral("org.kde.dolphin-%1").arg(QCoreApplication::applicationPid()); - if(!Dolphin::attachToExistingInstance(urls, true, GeneralSettings::splitView(), serviceName)) { + if(!Dolphin::attachToExistingInstance(urls, true, GeneralSettings::splitView(), serviceName, startUpId)) { Dolphin::openNewWindow(urls, nullptr, Dolphin::OpenNewWindowFlag::Select); }; } diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 305c7f2821..384e91ec91 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -281,11 +281,17 @@ void DolphinMainWindow::openFiles(const QStringList& files, bool splitView) openFiles(QUrl::fromStringList(files), splitView); } -void DolphinMainWindow::activateWindow() +void DolphinMainWindow::activateWindow(const QString &activationToken) { window()->setAttribute(Qt::WA_NativeWindow, true); - KStartupInfo::setNewStartupId(window()->windowHandle(), KStartupInfo::startupId()); - KWindowSystem::activateWindow(window()->effectiveWinId()); + + if (KWindowSystem::isPlatformWayland()) { + KWindowSystem::setCurrentXdgActivationToken(activationToken); + } else { + KStartupInfo::setNewStartupId(window()->windowHandle(), KStartupInfo::startupId()); + } + + KWindowSystem::activateWindow(window()->windowHandle()); } bool DolphinMainWindow::isActiveWindow() diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h index 6f922168c0..bf74b77b72 100644 --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -139,7 +139,7 @@ public Q_SLOTS: /** * Tries to raise/activate the Dolphin window. */ - void activateWindow(); + void activateWindow(const QString &activationToken); bool isActiveWindow(); diff --git a/src/global.cpp b/src/global.cpp index c9ec6a1200..d5fbec6bc4 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include @@ -56,7 +57,7 @@ void Dolphin::openNewWindow(const QList &urls, QWidget *window, const Open job->start(); } -bool Dolphin::attachToExistingInstance(const QList& inputUrls, bool openFiles, bool splitView, const QString& preferredService) +bool Dolphin::attachToExistingInstance(const QList& inputUrls, bool openFiles, bool splitView, const QString& preferredService, const QString &activationToken) { bool attached = false; @@ -121,7 +122,7 @@ bool Dolphin::attachToExistingInstance(const QList& inputUrls, bool openFi interface.first->openDirectories(interface.second, splitView); reply.waitForFinished(); if (!reply.isError()) { - interface.first->activateWindow(); + interface.first->activateWindow(activationToken); attached = true; } } diff --git a/src/global.h b/src/global.h index ee9a7ec271..80ec03fd02 100644 --- a/src/global.h +++ b/src/global.h @@ -39,7 +39,7 @@ namespace Dolphin { * @p preferredService needs to support the org.kde.dolphin.MainWindow dbus interface with the /dolphin/Dolphin_1 path. * Returns true if the URLs were successfully attached. */ - bool attachToExistingInstance(const QList& inputUrls, bool openFiles, bool splitView, const QString& preferredService = QString()); + bool attachToExistingInstance(const QList& inputUrls, bool openFiles, bool splitView, const QString& preferredService, const QString &activationToken); /** * Returns a QVector with all GUI-capable Dolphin instances diff --git a/src/main.cpp b/src/main.cpp index 017a31f1d0..9da0c6fe81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #include @@ -175,7 +176,14 @@ int main(int argc, char **argv) } if (!parser.isSet(QStringLiteral("new-window"))) { - if (Dolphin::attachToExistingInstance(urls, openFiles, splitView)) { + + QString token; + if (KWindowSystem::isPlatformWayland()) { + token = qEnvironmentVariable("XDG_ACTIVATION_TOKEN"); + qunsetenv("XDG_ACTIVATION_TOKEN"); + } + + if (Dolphin::attachToExistingInstance(urls, openFiles, splitView, QString(), token)) { // Successfully attached to existing instance of Dolphin return 0; }