Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 6e41b8d9 authored by Eike Hein's avatar Eike Hein

Port the "Switch Desktop" containment action to libtaskmanager

Summary:
This:
* Makes it work on Wayland.
  Depends on D13745.
* Cleans up includes and linkage.
* Fixes some pre-existing bugs like cached actions not updating
  to changed desktop names.

Reviewers: mart, davidedmundson

Reviewed By: davidedmundson

Subscribers: zzag, broulik, plasma-devel

Tags: #plasma

Maniphest Tasks: T4457

Differential Revision: https://phabricator.kde.org/D15599
parent c64fc469
......@@ -9,9 +9,7 @@ kcoreaddons_desktop_to_json(plasma_containmentactions_switchdesktop plasma-conta
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})
......
/*
* Copyright 2009 by Chani Armitage <chani@kde.org>
* Copyright 2018 by Eike Hein <hein@kde.org>
*
* 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,80 +20,96 @@
#include "desktop.h"
#include <virtualdesktopinfo.h>
#include <QAction>
#include <QWheelEvent>
#include <QDebug>
#include <KWindowSystem>
#include <KLocalizedString>
using namespace TaskManager;
SwitchDesktop::SwitchDesktop(QObject *parent, const QVariantList &args)
: Plasma::ContainmentActions(parent, args)
, m_virtualDesktopInfo(new VirtualDesktopInfo(this))
{
}
SwitchDesktop::~SwitchDesktop()
{
qDeleteAll(m_actions);
}
QList<QAction*> SwitchDesktop::contextualActions()
{
QList<QAction*> list;
const int numDesktops = m_virtualDesktopInfo->numberOfDesktops();
const QVariantList &desktopIds = m_virtualDesktopInfo->desktopIds();
const QStringList &desktopNames = m_virtualDesktopInfo->desktopNames();
const QVariant &currentDesktop = m_virtualDesktopInfo->currentDesktop();
const int numDesktops = KWindowSystem::numberOfDesktops();
const int currentDesktop = KWindowSystem::currentDesktop();
QList<QAction*> 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);
for (int i = m_actions.count(); i < numDesktops; ++i) {
QString name = desktopNames.at(i);
QAction *action = new QAction(this);
connect(action, &QAction::triggered, this, &SwitchDesktop::switchTo);
action->setData(i);
m_actions[i] = action;
}
} else if (m_actions.count() > numDesktops) {
for (int i = numDesktops +1; i <= m_actions.count(); ++i) {
delete m_actions[i];
m_actions.remove(i);
for (int i = m_actions.count(); i > numDesktops; --i) {
delete m_actions.take(i - 1);
}
}
for (int i = 1; i <= numDesktops; ++i) {
for (int i = 0; i < numDesktops; ++i) {
QAction *action = m_actions.value(i);
action->setEnabled(i != currentDesktop);
list << action;
action->setText(QStringLiteral("%1: %2").arg(QString::number(i), 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<QAction *>(sender());
const QAction *action = qobject_cast<QAction *>(sender());
if (!action) {
return;
}
const int desktop = action->data().toInt();
KWindowSystem::setCurrentDesktop(desktop);
m_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 = m_virtualDesktopInfo->desktopIds();
const QVariant &currentDesktop = m_virtualDesktopInfo->currentDesktop();
const int currentDesktopIndex = desktopIds.indexOf(currentDesktop);
int nextDesktopIndex = currentDesktopIndex + 1;
if (nextDesktopIndex == desktopIds.count()) {
nextDesktopIndex = 0;
}
m_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 = m_virtualDesktopInfo->desktopIds();
const QVariant &currentDesktop = m_virtualDesktopInfo->currentDesktop();
const int currentDesktopIndex = desktopIds.indexOf(currentDesktop);
int previousDesktopIndex = currentDesktopIndex - 1;
if (previousDesktopIndex < 0) {
previousDesktopIndex = desktopIds.count() - 1;
}
m_virtualDesktopInfo->requestActivate(desktopIds.at(previousDesktopIndex));
}
K_EXPORT_PLASMA_CONTAINMENTACTIONS_WITH_JSON(switchdesktop, SwitchDesktop, "plasma-containmentactions-switchdesktop.json")
......
/*
* Copyright 2009 by Chani Armitage <chani@kde.org>
* Copyright 2018 by Eike Hein <hein@kde.org>
*
* 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,7 @@ class SwitchDesktop : public Plasma::ContainmentActions
private:
QHash<int, QAction *> m_actions;
TaskManager::VirtualDesktopInfo* m_virtualDesktopInfo;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment