Changeset View
Changeset View
Standalone View
Standalone View
containmentactions/switchwindow/switch.cpp
| 1 | /* | 1 | /* | ||
|---|---|---|---|---|---|
| 2 | * Copyright 2009 by Chani Armitage <chani@kde.org> | 2 | * Copyright 2009 by Chani Armitage <chani@kde.org> | ||
| 3 | * Copyright 2018 by Eike Hein <hein@kde.org> | ||||
| 3 | * | 4 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | 5 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU Library General Public License as | 6 | * it under the terms of the GNU Library General Public License as | ||
| 6 | * published by the Free Software Foundation; either version 2, or | 7 | * published by the Free Software Foundation; either version 2, or | ||
| 7 | * (at your option) any later version. | 8 | * (at your option) any later version. | ||
| 8 | * | 9 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | 10 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details | 13 | * GNU General Public License for more details | ||
| 13 | * | 14 | * | ||
| 14 | * You should have received a copy of the GNU Library General Public | 15 | * You should have received a copy of the GNU Library General Public | ||
| 15 | * License along with this program; if not, write to the | 16 | * License along with this program; if not, write to the | ||
| 16 | * Free Software Foundation, Inc., | 17 | * Free Software Foundation, Inc., | ||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 18 | */ | 19 | */ | ||
| 19 | 20 | | |||
| 20 | #include "switch.h" | 21 | #include "switch.h" | ||
| 21 | 22 | | |||
| 22 | #include <QTimer> | 23 | #include "abstracttasksmodel.h" | ||
| 24 | #include "activityinfo.h" | ||||
| 25 | #include "tasksmodel.h" | ||||
| 26 | #include "virtualdesktopinfo.h" | ||||
| 23 | 27 | | |||
| 24 | #include <QDebug> | 28 | #include <QAction> | ||
| 25 | #include <QMenu> | 29 | #include <QMenu> | ||
| 26 | #include <KWindowSystem> | | |||
| 27 | 30 | | |||
| 28 | #include <Plasma/DataEngine> | 31 | using namespace TaskManager; | ||
| 29 | #include <Plasma/Service> | | |||
| 30 | 32 | | |||
| 31 | #include "abstracttasksmodel.h" | 33 | ActivityInfo *SwitchWindow::s_activityInfo = nullptr; | ||
| 34 | TasksModel *SwitchWindow::s_tasksModel = nullptr;int SwitchWindow::s_instanceCount = 0; | ||||
| 32 | 35 | | |||
| 33 | SwitchWindow::SwitchWindow(QObject *parent, const QVariantList &args) | 36 | SwitchWindow::SwitchWindow(QObject *parent, const QVariantList &args) | ||
| 34 | : Plasma::ContainmentActions(parent, args), | 37 | : Plasma::ContainmentActions(parent, args) | ||
| 35 | m_activityInfo(new TaskManager::ActivityInfo(this)), | 38 | , m_mode(AllFlat) | ||
| 36 | m_tasksModel(new TaskManager::TasksModel(this)), | 39 | , m_virtualDesktopInfo(new VirtualDesktopInfo(this)) | ||
| 37 | m_mode(AllFlat), | 40 | { | ||
| 38 | m_clearOrderTimer(nullptr) | 41 | ++s_instanceCount; | ||
| 39 | { | 42 | | ||
| 40 | m_tasksModel->setGroupMode(TaskManager::TasksModel::GroupDisabled); | 43 | if (!s_activityInfo) { | ||
| 41 | 44 | s_activityInfo = new ActivityInfo(); | |||
| 42 | m_tasksModel->setActivity(m_activityInfo->currentActivity()); | 45 | } | ||
| 43 | m_tasksModel->setFilterByActivity(true); | 46 | | ||
| 44 | connect(m_activityInfo, &TaskManager::ActivityInfo::currentActivityChanged, | 47 | if (!s_tasksModel) { | ||
| 45 | this, [this]() { m_tasksModel->setActivity(m_activityInfo->currentActivity()); }); | 48 | s_tasksModel = new TasksModel(); | ||
| 49 | | ||||
| 50 | s_tasksModel->setGroupMode(TasksModel::GroupDisabled); | ||||
| 51 | | ||||
| 52 | s_tasksModel->setActivity(s_activityInfo->currentActivity()); | ||||
| 53 | s_tasksModel->setFilterByActivity(true); | ||||
| 54 | connect(s_activityInfo, &ActivityInfo::currentActivityChanged, | ||||
| 55 | this, [this]() { s_tasksModel->setActivity(s_activityInfo->currentActivity()); }); | ||||
| 56 | } | ||||
| 46 | } | 57 | } | ||
| 47 | 58 | | |||
| 48 | SwitchWindow::~SwitchWindow() | 59 | SwitchWindow::~SwitchWindow() | ||
| 49 | { | 60 | { | ||
| 61 | --s_instanceCount; | ||||
| 62 | | ||||
| 63 | if (!s_instanceCount) { | ||||
| 64 | delete s_activityInfo; | ||||
| 65 | s_activityInfo = nullptr; | ||||
| 66 | delete s_tasksModel; | ||||
| 67 | s_tasksModel = nullptr; | ||||
| 68 | } | ||||
| 69 | | ||||
| 70 | qDeleteAll(m_actions); | ||||
| 50 | } | 71 | } | ||
| 51 | 72 | | |||
| 52 | void SwitchWindow::restore(const KConfigGroup &config) | 73 | void SwitchWindow::restore(const KConfigGroup &config) | ||
| 53 | { | 74 | { | ||
| 54 | m_mode = (MenuMode)config.readEntry("mode", (int)AllFlat); | 75 | m_mode = (MenuMode)config.readEntry("mode", (int)AllFlat); | ||
| 55 | } | 76 | } | ||
| 56 | 77 | | |||
| 57 | QWidget* SwitchWindow::createConfigurationInterface(QWidget* parent) | 78 | QWidget *SwitchWindow::createConfigurationInterface(QWidget *parent) | ||
| 58 | { | 79 | { | ||
| 59 | QWidget *widget = new QWidget(parent); | 80 | QWidget *widget = new QWidget(parent); | ||
| 60 | m_ui.setupUi(widget); | 81 | m_ui.setupUi(widget); | ||
| 61 | widget->setWindowTitle(i18nc("plasma_containmentactions_switchwindow", "Configure Switch Window Plugin")); | 82 | widget->setWindowTitle(i18nc("plasma_containmentactions_switchwindow", "Configure Switch Window Plugin")); | ||
| 62 | switch (m_mode) { | 83 | switch (m_mode) { | ||
| 63 | case AllFlat: | 84 | case AllFlat: | ||
| 64 | m_ui.flatButton->setChecked(true); | 85 | m_ui.flatButton->setChecked(true); | ||
| 65 | break; | 86 | break; | ||
| Show All 23 Lines | 109 | { | |||
| 89 | config.writeEntry("mode", (int)m_mode); | 110 | config.writeEntry("mode", (int)m_mode); | ||
| 90 | } | 111 | } | ||
| 91 | 112 | | |||
| 92 | void SwitchWindow::makeMenu() | 113 | void SwitchWindow::makeMenu() | ||
| 93 | { | 114 | { | ||
| 94 | qDeleteAll(m_actions); | 115 | qDeleteAll(m_actions); | ||
| 95 | m_actions.clear(); | 116 | m_actions.clear(); | ||
| 96 | 117 | | |||
| 97 | if (m_tasksModel->rowCount() == 0) { | 118 | if (s_tasksModel->rowCount() == 0) { | ||
| 98 | return; | 119 | return; | ||
| 99 | } | 120 | } | ||
| 100 | 121 | | |||
| 101 | QMultiHash<int, QAction*> desktops; | 122 | QMultiMap<QVariant, QAction *> desktops; | ||
| 123 | QList<QAction *> allDesktops; | ||||
| 102 | 124 | | |||
| 103 | //make all the window actions | 125 | // Make all the window actions. | ||
| 104 | for (int i = 0; i < m_tasksModel->rowCount(); ++i) { | 126 | for (int i = 0; i < s_tasksModel->rowCount(); ++i) { | ||
| 105 | if (m_tasksModel->data(m_tasksModel->index(i, 0), TaskManager::AbstractTasksModel::IsStartup).toBool()) { | 127 | const QModelIndex &idx = s_tasksModel->index(i, 0); | ||
| 106 | qDebug() << "skipped fake task"; | 128 | | ||
| 129 | if (!idx.data(AbstractTasksModel::IsWindow).toBool()) { | ||||
| 107 | continue; | 130 | continue; | ||
| 108 | } | 131 | } | ||
| 109 | 132 | | |||
| 110 | QString name = m_tasksModel->data(m_tasksModel->index(i, 0), Qt::DisplayRole).toString(); | 133 | const QString &name = idx.data().toString(); | ||
| 111 | 134 | | |||
| 112 | if (name.isEmpty()) { | 135 | if (name.isEmpty()) { | ||
| 113 | continue; | 136 | continue; | ||
| 114 | } | 137 | } | ||
| 115 | 138 | | |||
| 116 | const QVariantList &idList = m_tasksModel->data(m_tasksModel->index(i, 0), TaskManager::AbstractTasksModel::LegacyWinIdList).toList(); | 139 | QAction *action = new QAction(name, this); | ||
| 140 | action->setIcon(idx.data(Qt::DecorationRole).value<QIcon>()); | ||||
| 141 | action->setData(idx.data(AbstractTasksModel::WinIdList).toList()); | ||||
| 142 | | ||||
| 143 | const QVariantList &desktopList = idx.data(AbstractTasksModel::VirtualDesktops).toList(); | ||||
| 117 | 144 | | |||
| 118 | if (!idList.count()) { | 145 | for (const QVariant &desktop : desktopList) { | ||
| 119 | continue; | 146 | desktops.insert(desktop, action); | ||
| 147 | } | ||||
| 148 | | ||||
| 149 | if (idx.data(AbstractTasksModel::IsOnAllVirtualDesktops).toBool()) { | ||||
| 150 | allDesktops << action; | ||||
| 120 | } | 151 | } | ||
| 121 | 152 | | |||
| 122 | QAction *action = new QAction(name, this); | | |||
| 123 | action->setIcon(m_tasksModel->data(m_tasksModel->index(i, 0), Qt::DecorationRole).value<QIcon>()); | | |||
| 124 | action->setData(idList.at(0)); | | |||
| 125 | desktops.insert(m_tasksModel->data(m_tasksModel->index(i, 0), TaskManager::AbstractTasksModel::VirtualDesktop).toInt(), action); | | |||
| 126 | connect(action, &QAction::triggered, [=]() { | 153 | connect(action, &QAction::triggered, [=]() { | ||
| 127 | switchTo(action); | 154 | switchTo(action); | ||
| 128 | }); | 155 | }); | ||
| 129 | } | 156 | } | ||
| 130 | 157 | | |||
| 131 | //sort into menu | 158 | // Sort into menu(s). | ||
| 132 | if (m_mode == CurrentDesktop) { | 159 | if (m_mode == CurrentDesktop) { | ||
| 133 | int currentDesktop = KWindowSystem::currentDesktop(); | 160 | const QVariant ¤tDesktop = m_virtualDesktopInfo->currentDesktop(); | ||
| 134 | 161 | | |||
| 135 | QAction *a = new QAction(i18nc("plasma_containmentactions_switchwindow", "Windows"), this); | 162 | QAction *a = new QAction(i18nc("plasma_containmentactions_switchwindow", "Windows"), this); | ||
| 136 | a->setSeparator(true); | 163 | a->setSeparator(true); | ||
| 137 | m_actions << a; | 164 | m_actions << a; | ||
| 138 | m_actions << desktops.values(currentDesktop); | 165 | m_actions << desktops.values(currentDesktop); | ||
| 139 | m_actions << desktops.values(-1); | 166 | m_actions << allDesktops; | ||
| 140 | 167 | | |||
| 141 | } else { | 168 | } else { | ||
| 142 | int numDesktops = KWindowSystem::numberOfDesktops(); | 169 | const QVariantList &desktopIds = m_virtualDesktopInfo->desktopIds(); | ||
| 170 | const QStringList &desktopNames = m_virtualDesktopInfo->desktopNames(); | ||||
| 171 | | ||||
| 143 | if (m_mode == AllFlat) { | 172 | if (m_mode == AllFlat) { | ||
| 144 | for (int i = 1; i <= numDesktops; ++i) { | 173 | for (int i = 0; i <= desktopIds.count(); ++i) { | ||
| 145 | if (desktops.contains(i)) { | 174 | const QVariant &desktop = desktopIds.at(i); | ||
| 146 | QString name = KWindowSystem::desktopName(i); | 175 | | ||
| 147 | name = QStringLiteral("%1: %2").arg(i).arg(name); | 176 | if (desktops.contains(desktop)) { | ||
| 177 | const QString &name = QStringLiteral("%1: %2").arg(QString::number(i), desktopNames.at(i)); | ||||
| 148 | QAction *a = new QAction(name, this); | 178 | QAction *a = new QAction(name, this); | ||
| 149 | a->setSeparator(true); | 179 | a->setSeparator(true); | ||
| 150 | m_actions << a; | 180 | m_actions << a; | ||
| 151 | m_actions << desktops.values(i); | 181 | m_actions << desktops.values(desktop); | ||
| 152 | } | 182 | } | ||
| 153 | } | 183 | } | ||
| 154 | if (desktops.contains(-1)) { | 184 | | ||
| 185 | if (allDesktops.count()) { | ||||
| 155 | QAction *a = new QAction(i18nc("plasma_containmentactions_switchwindow", "All Desktops"), this); | 186 | QAction *a = new QAction(i18nc("plasma_containmentactions_switchwindow", "All Desktops"), this); | ||
| 156 | a->setSeparator(true); | 187 | a->setSeparator(true); | ||
| 157 | m_actions << a; | 188 | m_actions << a; | ||
| 158 | m_actions << desktops.values(-1); | 189 | m_actions << allDesktops; | ||
| 159 | } | 190 | } | ||
| 191 | } else { // Submenus. | ||||
| 192 | for (int i = 0; i <= desktopIds.count(); ++i) { | ||||
| 193 | const QVariant &desktop = desktopIds.at(i); | ||||
| 160 | 194 | | |||
| 161 | } else { //submenus | 195 | if (desktops.contains(desktop)) { | ||
| 162 | for (int i = 1; i <= numDesktops; ++i) { | 196 | const QString &name = QStringLiteral("%1: %2").arg(QString::number(i), desktopNames.at(i)); | ||
davidedmundson: .arg(i, desktopNames.at(i)
in case the id contains the string "%1"
(and above)
| |||||
| 163 | if (desktops.contains(i)) { | | |||
| 164 | QString name = KWindowSystem::desktopName(i); | | |||
| 165 | name = QStringLiteral("%1: %2").arg(i).arg(name); | | |||
| 166 | QMenu *subMenu = new QMenu(name); | 197 | QMenu *subMenu = new QMenu(name); | ||
| 167 | subMenu->addActions(desktops.values(i)); | 198 | subMenu->addActions(desktops.values(desktop)); | ||
| 168 | 199 | | |||
| 169 | QAction *a = new QAction(name, this); | 200 | QAction *a = new QAction(name, this); | ||
| 170 | a->setMenu(subMenu); | 201 | a->setMenu(subMenu); | ||
| 171 | m_actions << a; | 202 | m_actions << a; | ||
| 172 | } | 203 | } | ||
| 173 | } | 204 | } | ||
| 174 | if (desktops.contains(-1)) { | 205 | | ||
| 206 | if (allDesktops.count()) { | ||||
| 175 | QMenu *subMenu = new QMenu(i18nc("plasma_containmentactions_switchwindow", "All Desktops")); | 207 | QMenu *subMenu = new QMenu(i18nc("plasma_containmentactions_switchwindow", "All Desktops")); | ||
| 176 | subMenu->addActions(desktops.values(-1)); | 208 | subMenu->addActions(allDesktops); | ||
| 177 | QAction *a = new QAction(i18nc("plasma_containmentactions_switchwindow", "All Desktops"), this); | 209 | QAction *a = new QAction(i18nc("plasma_containmentactions_switchwindow", "All Desktops"), this); | ||
| 178 | a->setMenu(subMenu); | 210 | a->setMenu(subMenu); | ||
| 179 | m_actions << a; | 211 | m_actions << a; | ||
| 180 | } | 212 | } | ||
| 181 | } | 213 | } | ||
| 182 | } | 214 | } | ||
| 183 | } | 215 | } | ||
| 184 | 216 | | |||
| 185 | QList<QAction*> SwitchWindow::contextualActions() | 217 | QList<QAction *> SwitchWindow::contextualActions() | ||
| 186 | { | 218 | { | ||
| 187 | makeMenu(); | 219 | makeMenu(); | ||
| 188 | return m_actions; | 220 | return m_actions; | ||
| 189 | } | 221 | } | ||
| 190 | 222 | | |||
| 191 | void SwitchWindow::switchTo(QAction *action) | 223 | void SwitchWindow::switchTo(QAction *action) | ||
| 192 | { | 224 | { | ||
| 193 | int id = action->data().toInt(); | 225 | const QVariantList &idList = action->data().toList(); | ||
| 194 | 226 | | |||
| 195 | KWindowSystem::forceActiveWindow(id); | 227 | for (int i = 0; i < s_tasksModel->rowCount(); ++i) { | ||
| 196 | } | 228 | const QModelIndex &idx = s_tasksModel->index(i, 0); | ||
| 197 | 229 | | |||
| 198 | void SwitchWindow::clearWindowsOrder() | 230 | if (idList == idx.data(AbstractTasksModel::WinIdList).toList()) { | ||
| 199 | { | 231 | s_tasksModel->requestActivate(idx); | ||
| 200 | qDebug() << "CLEARING>......................."; | 232 | | ||
| 201 | m_windowsOrder.clear(); | 233 | return; | ||
| 234 | } | ||||
| 235 | } | ||||
| 202 | } | 236 | } | ||
| 203 | 237 | | |||
| 204 | void SwitchWindow::performNextAction() | 238 | void SwitchWindow::performNextAction() | ||
| 205 | { | 239 | { | ||
| 206 | doSwitch(true); | 240 | doSwitch(true); | ||
| 207 | } | 241 | } | ||
| 208 | 242 | | |||
| 209 | void SwitchWindow::performPreviousAction() | 243 | void SwitchWindow::performPreviousAction() | ||
| 210 | { | 244 | { | ||
| 211 | doSwitch(false); | 245 | doSwitch(false); | ||
| 212 | } | 246 | } | ||
| 213 | 247 | | |||
| 214 | void SwitchWindow::doSwitch(bool up) | 248 | void SwitchWindow::doSwitch(bool up) | ||
| 215 | { | 249 | { | ||
| 216 | //TODO somehow find the "next" or "previous" window | 250 | const QModelIndex &activeTask = s_tasksModel->activeTask(); | ||
| 217 | //without changing hte window order (don't want to always go between two windows) | | |||
| 218 | if (m_windowsOrder.isEmpty()) { | | |||
| 219 | m_windowsOrder = KWindowSystem::stackingOrder(); | | |||
| 220 | } else { | | |||
| 221 | if (!m_clearOrderTimer) { | | |||
| 222 | m_clearOrderTimer = new QTimer(this); | | |||
| 223 | connect(m_clearOrderTimer, &QTimer::timeout, this, &SwitchWindow::clearWindowsOrder); | | |||
| 224 | m_clearOrderTimer->setSingleShot(true); | | |||
| 225 | m_clearOrderTimer->setInterval(1000); | | |||
| 226 | } | | |||
| 227 | | ||||
| 228 | m_clearOrderTimer->start(); | | |||
| 229 | } | | |||
| 230 | | ||||
| 231 | const WId activeWindow = KWindowSystem::activeWindow(); | | |||
| 232 | bool next = false; | | |||
| 233 | WId first = 0; | | |||
| 234 | WId last = 0; | | |||
| 235 | for (int i = 0; i < m_windowsOrder.count(); ++i) { | | |||
| 236 | const WId id = m_windowsOrder.at(i); | | |||
| 237 | const KWindowInfo info(id, NET::WMDesktop | NET::WMVisibleName | NET::WMWindowType); | | |||
| 238 | if (info.windowType(NET::NormalMask | NET::DialogMask | NET::UtilityMask) != -1 && info.isOnCurrentDesktop()) { | | |||
| 239 | if (next) { | | |||
| 240 | KWindowSystem::forceActiveWindow(id); | | |||
| 241 | return; | | |||
| 242 | } | | |||
| 243 | 251 | | |||
| 244 | if (first == 0) { | 252 | if (!activeTask.isValid()) { | ||
| 245 | first = id; | 253 | return; | ||
| 246 | } | 254 | } | ||
| 247 | 255 | | |||
| 248 | if (id == activeWindow) { | | |||
| 249 | if (up) { | 256 | if (up) { | ||
| 250 | next = true; | 257 | const QModelIndex &next = activeTask.sibling(activeTask.row() + 1, 0); | ||
davidedmundson: The old code wrapped (albeit in a weirdly coded way) | |||||
| 251 | } else if (last) { | 258 | | ||
| 252 | KWindowSystem::forceActiveWindow(last); | 259 | if (next.isValid()) { | ||
| 253 | return; | 260 | s_tasksModel->requestActivate(next); | ||
| 254 | } | 261 | } else if (s_tasksModel->rowCount() > 1) { | ||
| 262 | s_tasksModel->requestActivate(s_tasksModel->index(0, 0)); | ||||
| 255 | } | 263 | } | ||
| 264 | } else { | ||||
| 265 | const QModelIndex &previous = activeTask.sibling(activeTask.row() - 1, 0); | ||||
| 256 | 266 | | |||
| 257 | last = id; | 267 | if (previous.isValid()) { | ||
| 268 | s_tasksModel->requestActivate(previous); | ||||
| 269 | } else if (s_tasksModel->rowCount() > 1) { | ||||
| 270 | s_tasksModel->requestActivate(s_tasksModel->index(s_tasksModel->rowCount() - 1, 0)); | ||||
| 258 | } | 271 | } | ||
| 259 | } | 272 | } | ||
| 260 | | ||||
| 261 | KWindowSystem::forceActiveWindow(up ? first : last); | | |||
| 262 | } | 273 | } | ||
| 263 | 274 | | |||
| 264 | K_EXPORT_PLASMA_CONTAINMENTACTIONS_WITH_JSON(switchwindow, SwitchWindow, "plasma-containmentactions-switchwindow.json") | 275 | K_EXPORT_PLASMA_CONTAINMENTACTIONS_WITH_JSON(switchwindow, SwitchWindow, "plasma-containmentactions-switchwindow.json") | ||
| 265 | 276 | | |||
| 266 | #include "switch.moc" | 277 | #include "switch.moc" | ||
.arg(i, desktopNames.at(i)
in case the id contains the string "%1"
(and above)