Fix wayland activation

Adds support for xdg_activation_v1 when calling the application from the
system tray by using KStatusNotifier which supports it.
Listens to XDG_ACTIVATION_TOKEN as it's passed when we are started from
dbus.
This commit is contained in:
Aleix Pol
2021-09-03 20:31:12 +02:00
committed by Aleix Pol Gonzalez
parent 5c86692fb5
commit 265fcbfead
7 changed files with 77 additions and 3 deletions

View File

@@ -158,6 +158,7 @@ Kirigami.ApplicationWindow {
root.show()
root.raise()
root.requestActivate()
Controller.raiseWindow(root)
}
contextDrawer: RoomDrawer {

View File

@@ -48,7 +48,12 @@ ecm_add_app_icon(NEOCHAT_ICON ICONS ${CMAKE_SOURCE_DIR}/128-logo.png)
target_sources(neochat PRIVATE ${NEOCHAT_ICON})
if(NOT ANDROID)
target_sources(neochat PRIVATE trayicon.cpp colorschemer.cpp)
target_sources(neochat PRIVATE colorschemer.cpp)
if (NOT WIN32 AND NOT APPLE)
target_sources(neochat PRIVATE trayicon_sni.cpp)
else()
target_sources(neochat PRIVATE trayicon.cpp)
endif()
target_link_libraries(neochat PRIVATE KF5::ConfigWidgets KF5::WindowSystem KF5::SonnetCore)
target_compile_definitions(neochat PRIVATE -DHAVE_COLORSCHEME)
target_compile_definitions(neochat PRIVATE -DHAVE_WINDOWSYSTEM)

View File

@@ -12,6 +12,7 @@
#include <KWindowConfig>
#ifdef HAVE_WINDOWSYSTEM
#include <KWindowEffects>
#include <KWindowSystem>
#endif
#include <QAuthenticator>
@@ -55,8 +56,10 @@
#include "utils.h"
#include <KStandardShortcut>
#ifndef Q_OS_ANDROID
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
#include "trayicon.h"
#elif !defined(Q_OS_ANDROID)
#include "trayicon_sni.h"
#endif
using namespace Quotient;
@@ -634,6 +637,13 @@ void Controller::setBlur(QQuickItem *item, bool blur)
#endif
}
void Controller::raiseWindow(QWindow *window)
{
#ifdef HAVE_WINDOWSYSTEM
KWindowSystem::activateWindow(window->winId());
#endif
}
bool Controller::hasWindowSystem() const
{
#ifdef HAVE_WINDOWSYSTEM

View File

@@ -91,6 +91,7 @@ public:
Q_INVOKABLE void openOrCreateDirectChat(NeoChatUser *user);
Q_INVOKABLE void setBlur(QQuickItem *item, bool blur);
Q_INVOKABLE void raiseWindow(QWindow *window);
private:
explicit Controller(QObject *parent = nullptr);

View File

@@ -22,6 +22,7 @@
#include <KAboutData>
#ifdef HAVE_KDBUSADDONS
#include <KDBusService>
#include <KWindowSystem>
#endif
#include <KLocalizedContext>
#include <KLocalizedString>
@@ -66,6 +67,18 @@
using namespace Quotient;
#ifdef HAVE_KDBUSADDONS
static void raiseWindow(QWindow *window)
{
if (KWindowSystem::isPlatformWayland()) {
KWindowSystem::setCurrentXdgActivationToken(qEnvironmentVariable("XDG_ACTIVATION_TOKEN"));
KWindowSystem::activateWindow(window->winId());
} else {
window->raise();
}
}
#endif
#ifdef Q_OS_ANDROID
Q_DECL_EXPORT
#endif
@@ -232,7 +245,7 @@ int main(int argc, char *argv[])
auto view = qobject_cast<QQuickWindow *>(obj);
if (view) {
view->show();
view->raise();
raiseWindow(view);
return;
}
}

25
src/trayicon_sni.cpp Normal file
View File

@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
// SPDX-License-Identifier: GPL-3.0-only
#include "trayicon_sni.h"
#include <KWindowSystem>
TrayIcon::TrayIcon(QObject *parent)
: KStatusNotifierItem(parent)
{
setIconByName("org.kde.neochat");
connect(this, &KStatusNotifierItem::activateRequested, this, [this] {
KWindowSystem::setCurrentXdgActivationToken(providedToken());
Q_EMIT showWindow();
});
}
void TrayIcon::show()
{
setStatus(Active);
}
void TrayIcon::hide()
{
setStatus(Passive);
}

19
src/trayicon_sni.h Normal file
View File

@@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <KStatusNotifierItem>
class TrayIcon : public KStatusNotifierItem
{
Q_OBJECT
public:
TrayIcon(QObject *parent = nullptr);
void show();
void hide();
Q_SIGNALS:
void showWindow();
};