diff --git a/containmentactions/switchdesktop/desktop.h b/containmentactions/switchdesktop/desktop.h --- a/containmentactions/switchdesktop/desktop.h +++ b/containmentactions/switchdesktop/desktop.h @@ -17,31 +17,48 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef SWITCHDESKTOP_HEADER -#define SWITCHDESKTOP_HEADER +#pragma once +#include +#include +#include #include class QAction; +class QButtonGroup; class SwitchDesktop : public Plasma::ContainmentActions { Q_OBJECT public: - SwitchDesktop(QObject* parent, const QVariantList& args); + SwitchDesktop(QObject *parent, const QVariantList &args); ~SwitchDesktop() override; - QList contextualActions() override; + QList contextualActions() override; + + void restore(const KConfigGroup &config) override; + QWidget* createConfigurationInterface(QWidget *parent) override; + void configurationAccepted() override; + void save(KConfigGroup &config) override; void performNextAction() override; void performPreviousAction() override; + void switchDesktop(bool next); + private Q_SLOTS: void switchTo(); private: QHash m_actions; -}; + QButtonGroup *m_settingsButtons; + bool m_kwinrcRollOverDesktops; + struct Option { + QString cfgKey; // also key in m_options map + QString description; + bool value; + }; + QMap m_options; +}; -#endif diff --git a/containmentactions/switchdesktop/desktop.cpp b/containmentactions/switchdesktop/desktop.cpp --- a/containmentactions/switchdesktop/desktop.cpp +++ b/containmentactions/switchdesktop/desktop.cpp @@ -19,15 +19,32 @@ #include "desktop.h" +#include +#include #include -#include +#include +#include +#include #include #include #include +namespace { +constexpr const char *cfgKeyProperty = "cfgKey"; + +constexpr bool invertMouseWheelDefault = false; +const QString invertMouseCfgKey = "invert mouse wheel"; + +bool readKwinrcRollOverDesktops() { + auto cfg = KSharedConfig::openConfig("kwinrc"); + return KConfigGroup{cfg, "Windows"}.readEntry("RollOverDesktops", true); +} +} + SwitchDesktop::SwitchDesktop(QObject *parent, const QVariantList &args) - : Plasma::ContainmentActions(parent, args) + : Plasma::ContainmentActions{parent, args} + , m_kwinrcRollOverDesktops{readKwinrcRollOverDesktops()} { } @@ -37,9 +54,9 @@ } -QList SwitchDesktop::contextualActions() +QList SwitchDesktop::contextualActions() { - QList list; + QList list; const int numDesktops = KWindowSystem::numberOfDesktops(); const int currentDesktop = KWindowSystem::currentDesktop(); @@ -70,6 +87,60 @@ return list; } +void SwitchDesktop::restore(const KConfigGroup &config) +{ + Option invertWheel{ + invertMouseCfgKey, + i18nc("plasma_containmentactions_switchdesktop", "Invert mouse wheel"), + config.readEntry(invertMouseCfgKey, invertMouseWheelDefault) + }; + m_options[invertMouseCfgKey] = invertWheel; +} + +QWidget *SwitchDesktop::createConfigurationInterface(QWidget *parent) +{ + m_kwinrcRollOverDesktops = readKwinrcRollOverDesktops(); + + QWidget *widget = new QWidget(parent); + QVBoxLayout *lay = new QVBoxLayout(); + widget->setLayout(lay); + widget->setWindowTitle(i18nc("plasma_containmentactions_switchdesktop", "Configure Switch Desktop")); + m_settingsButtons = new QButtonGroup(widget); + m_settingsButtons->setExclusive(false); + + for (const Option &e : m_options) { + QCheckBox *item = new QCheckBox(widget); + item->setText(e.description); + item->setChecked(e.value); + item->setProperty(cfgKeyProperty, e.cfgKey); + lay->addWidget(item); + m_settingsButtons->addButton(item); + } + + return widget; +} + +void SwitchDesktop::configurationAccepted() +{ + m_kwinrcRollOverDesktops = readKwinrcRollOverDesktops(); + + QList buttons = m_settingsButtons->buttons(); + QListIterator it(buttons); + for (QAbstractButton *b : buttons) { + QVariant cfgKey = b->property(cfgKeyProperty); + if (cfgKey.isValid() && m_options.contains(cfgKey.toString())) { + m_options[cfgKey.toString()].value = b->isChecked(); + } + } +} + +void SwitchDesktop::save(KConfigGroup &config) +{ + for (const Option &e : m_options) { + config.writeEntry(e.cfgKey, e.value); + } +} + void SwitchDesktop::switchTo() { QAction *action = qobject_cast(sender()); @@ -81,18 +152,31 @@ KWindowSystem::setCurrentDesktop(desktop); } -void SwitchDesktop::performNextAction() -{ +void SwitchDesktop::switchDesktop(bool next) { const int numDesktops = KWindowSystem::numberOfDesktops(); const int currentDesktop = KWindowSystem::currentDesktop(); - KWindowSystem::setCurrentDesktop(currentDesktop % numDesktops + 1); + if (m_options.contains(invertMouseCfgKey) && m_options[invertMouseCfgKey].value) { + next = !next; + } + bool wouldRollover = (next && currentDesktop == numDesktops) || (!next && currentDesktop == 1); + if (wouldRollover && !m_kwinrcRollOverDesktops) { + return; + } + if (next) { + KWindowSystem::setCurrentDesktop(currentDesktop % numDesktops + 1); + } else { + KWindowSystem::setCurrentDesktop((numDesktops + currentDesktop - 2) % numDesktops + 1); + } +} + +void SwitchDesktop::performNextAction() +{ + switchDesktop(true); } void SwitchDesktop::performPreviousAction() { - const int numDesktops = KWindowSystem::numberOfDesktops(); - const int currentDesktop = KWindowSystem::currentDesktop(); - KWindowSystem::setCurrentDesktop((numDesktops + currentDesktop - 2) % numDesktops + 1); + switchDesktop(false); } K_EXPORT_PLASMA_CONTAINMENTACTIONS_WITH_JSON(switchdesktop, SwitchDesktop, "plasma-containmentactions-switchdesktop.json")