diff --git a/containmentactions/switchdesktop/CMakeLists.txt b/containmentactions/switchdesktop/CMakeLists.txt --- a/containmentactions/switchdesktop/CMakeLists.txt +++ b/containmentactions/switchdesktop/CMakeLists.txt @@ -9,9 +9,8 @@ target_link_libraries(plasma_containmentactions_switchdesktop Qt5::Widgets KF5::Plasma - KF5::WindowSystem KF5::I18n - KF5::KIOCore + PW::LibTaskManager ) install(TARGETS plasma_containmentactions_switchdesktop DESTINATION ${KDE_INSTALL_PLUGINDIR}) diff --git a/containmentactions/switchdesktop/desktop.h b/containmentactions/switchdesktop/desktop.h --- a/containmentactions/switchdesktop/desktop.h +++ b/containmentactions/switchdesktop/desktop.h @@ -1,5 +1,6 @@ /* * Copyright 2009 by Chani Armitage + * Copyright 2018 by Eike Hein * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as @@ -24,6 +25,10 @@ class QAction; +namespace TaskManager { + class VirtualDesktopInfo; +} + class SwitchDesktop : public Plasma::ContainmentActions { Q_OBJECT @@ -41,6 +46,8 @@ private: QHash m_actions; + static TaskManager::VirtualDesktopInfo* s_virtualDesktopInfo; + static int s_instanceCount; }; diff --git a/containmentactions/switchdesktop/desktop.cpp b/containmentactions/switchdesktop/desktop.cpp --- a/containmentactions/switchdesktop/desktop.cpp +++ b/containmentactions/switchdesktop/desktop.cpp @@ -1,5 +1,6 @@ /* * Copyright 2009 by Chani Armitage + * Copyright 2018 by Eike Hein * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as @@ -19,38 +20,56 @@ #include "desktop.h" +#include + #include #include -#include -#include #include +using namespace TaskManager; + +VirtualDesktopInfo *SwitchDesktop::s_virtualDesktopInfo = nullptr; +int SwitchDesktop::s_instanceCount = 0; + SwitchDesktop::SwitchDesktop(QObject *parent, const QVariantList &args) : Plasma::ContainmentActions(parent, args) { + ++s_instanceCount; + + if (!s_virtualDesktopInfo) { + s_virtualDesktopInfo = new VirtualDesktopInfo(); + } } SwitchDesktop::~SwitchDesktop() { + --s_instanceCount; + + if (!s_virtualDesktopInfo) { + delete s_virtualDesktopInfo; + s_virtualDesktopInfo = nullptr; + } + qDeleteAll(m_actions); } QList SwitchDesktop::contextualActions() { - QList list; + const int numDesktops = s_virtualDesktopInfo->numberOfDesktops(); + const QVariantList &desktopIds = s_virtualDesktopInfo->desktopIds(); + const QStringList &desktopNames = s_virtualDesktopInfo->desktopNames(); + const QVariant ¤tDesktop = s_virtualDesktopInfo->currentDesktop(); - const int numDesktops = KWindowSystem::numberOfDesktops(); - const int currentDesktop = KWindowSystem::currentDesktop(); + QList actions; + actions.reserve(numDesktops); - //Is it either the first time or the desktop number changed? if (m_actions.count() < numDesktops) { for (int i = m_actions.count() + 1; i <= numDesktops; ++i) { - QString name = KWindowSystem::desktopName(i); - QAction *action = new QAction(QStringLiteral("%1: %2").arg(i).arg(name), this); + QString name = desktopNames.at(i); + QAction *action = new QAction(this); connect(action, &QAction::triggered, this, &SwitchDesktop::switchTo); - action->setData(i); m_actions[i] = action; } @@ -63,36 +82,46 @@ for (int i = 1; i <= numDesktops; ++i) { QAction *action = m_actions.value(i); - action->setEnabled(i != currentDesktop); - list << action; + action->setText(QStringLiteral("%1: %2").arg(i).arg(desktopNames.at(i))); + action->setData(desktopIds.at(i)); + action->setEnabled(desktopIds.at(i) != currentDesktop); + actions << action; } - return list; + return actions; } void SwitchDesktop::switchTo() { - QAction *action = qobject_cast(sender()); + const QAction *action = qobject_cast(sender()); + if (!action) { return; } - const int desktop = action->data().toInt(); - KWindowSystem::setCurrentDesktop(desktop); + s_virtualDesktopInfo->requestActivate(action->data()); } void SwitchDesktop::performNextAction() { - const int numDesktops = KWindowSystem::numberOfDesktops(); - const int currentDesktop = KWindowSystem::currentDesktop(); - KWindowSystem::setCurrentDesktop(currentDesktop % numDesktops + 1); + const QVariantList &desktopIds = s_virtualDesktopInfo->desktopIds(); + const QVariant ¤tDesktop = s_virtualDesktopInfo->currentDesktop(); + const int currentDesktopIndex = desktopIds.indexOf(currentDesktop); + + const int nextDesktopIndex = (currentDesktopIndex % desktopIds.count() + 1); + + s_virtualDesktopInfo->requestActivate(desktopIds.at(nextDesktopIndex)); } void SwitchDesktop::performPreviousAction() { - const int numDesktops = KWindowSystem::numberOfDesktops(); - const int currentDesktop = KWindowSystem::currentDesktop(); - KWindowSystem::setCurrentDesktop((numDesktops + currentDesktop - 2) % numDesktops + 1); + const QVariantList &desktopIds = s_virtualDesktopInfo->desktopIds(); + const QVariant ¤tDesktop = s_virtualDesktopInfo->currentDesktop(); + const int currentDesktopIndex = desktopIds.indexOf(currentDesktop); + + const int previousDesktopIndex = ((desktopIds.count() + currentDesktopIndex - 2) % desktopIds.count() + 1); + + s_virtualDesktopInfo->requestActivate(desktopIds.at(previousDesktopIndex)); } K_EXPORT_PLASMA_CONTAINMENTACTIONS_WITH_JSON(switchdesktop, SwitchDesktop, "plasma-containmentactions-switchdesktop.json")