File indexing completed on 2024-05-12 05:37:16

0001 /*
0002     SPDX-FileCopyrightText: 2009 Chani Armitage <chani@kde.org>
0003     SPDX-FileCopyrightText: 2018 Eike Hein <hein@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "desktop.h"
0009 
0010 #include <virtualdesktopinfo.h>
0011 
0012 #include <KPluginFactory>
0013 
0014 #include <QAction>
0015 
0016 using namespace TaskManager;
0017 
0018 SwitchDesktop::SwitchDesktop(QObject *parent, const QVariantList &args)
0019     : Plasma::ContainmentActions(parent, args)
0020     , m_virtualDesktopInfo(new VirtualDesktopInfo(this))
0021 {
0022 }
0023 
0024 SwitchDesktop::~SwitchDesktop()
0025 {
0026 }
0027 
0028 QList<QAction *> SwitchDesktop::contextualActions()
0029 {
0030     const int numDesktops = m_virtualDesktopInfo->numberOfDesktops();
0031     const QVariantList &desktopIds = m_virtualDesktopInfo->desktopIds();
0032     const QStringList &desktopNames = m_virtualDesktopInfo->desktopNames();
0033     const QVariant &currentDesktop = m_virtualDesktopInfo->currentDesktop();
0034 
0035     QList<QAction *> actions;
0036     actions.reserve(numDesktops);
0037 
0038     if (m_actions.count() < numDesktops) {
0039         for (int i = m_actions.count(); i < numDesktops; ++i) {
0040             QAction *action = new QAction(this);
0041             connect(action, &QAction::triggered, this, &SwitchDesktop::switchTo);
0042             m_actions[i] = action;
0043         }
0044     } else if (m_actions.count() > numDesktops) {
0045         for (int i = m_actions.count(); i > numDesktops; --i) {
0046             delete m_actions.take(i - 1);
0047         }
0048     }
0049 
0050     for (int i = 0; i < numDesktops; ++i) {
0051         QAction *action = m_actions.value(i);
0052         action->setText(QStringLiteral("%1: %2").arg(QString::number(i), desktopNames.at(i)));
0053         action->setData(desktopIds.at(i));
0054         action->setEnabled(desktopIds.at(i) != currentDesktop);
0055         actions << action;
0056     }
0057 
0058     return actions;
0059 }
0060 
0061 void SwitchDesktop::switchTo()
0062 {
0063     const QAction *action = qobject_cast<QAction *>(sender());
0064 
0065     if (!action) {
0066         return;
0067     }
0068 
0069     m_virtualDesktopInfo->requestActivate(action->data());
0070 }
0071 
0072 void SwitchDesktop::performNextAction()
0073 {
0074     const QVariantList &desktopIds = m_virtualDesktopInfo->desktopIds();
0075     if (desktopIds.isEmpty()) {
0076         return;
0077     }
0078 
0079     const QVariant &currentDesktop = m_virtualDesktopInfo->currentDesktop();
0080     const int currentDesktopIndex = desktopIds.indexOf(currentDesktop);
0081 
0082     int nextDesktopIndex = currentDesktopIndex + 1;
0083 
0084     if (nextDesktopIndex == desktopIds.count()) {
0085         if (m_virtualDesktopInfo->navigationWrappingAround()) {
0086             nextDesktopIndex = 0;
0087         } else {
0088             return;
0089         }
0090     }
0091 
0092     m_virtualDesktopInfo->requestActivate(desktopIds.at(nextDesktopIndex));
0093 }
0094 
0095 void SwitchDesktop::performPreviousAction()
0096 {
0097     const QVariantList &desktopIds = m_virtualDesktopInfo->desktopIds();
0098     if (desktopIds.isEmpty()) {
0099         return;
0100     }
0101     const QVariant &currentDesktop = m_virtualDesktopInfo->currentDesktop();
0102     const int currentDesktopIndex = desktopIds.indexOf(currentDesktop);
0103 
0104     int previousDesktopIndex = currentDesktopIndex - 1;
0105 
0106     if (previousDesktopIndex < 0) {
0107         if (m_virtualDesktopInfo->navigationWrappingAround()) {
0108             previousDesktopIndex = desktopIds.count() - 1;
0109         } else {
0110             return;
0111         }
0112     }
0113 
0114     m_virtualDesktopInfo->requestActivate(desktopIds.at(previousDesktopIndex));
0115 }
0116 
0117 K_PLUGIN_CLASS_WITH_JSON(SwitchDesktop, "plasma-containmentactions-switchdesktop.json")
0118 
0119 #include "desktop.moc"