1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
SPDX-FileCopyrightText: 2016 Kai Uwe Broulik <kde@privat.broulik.de>
SPDX-FileCopyrightText: 2016 Chinmoy Ranjan Pradhan <chinmoyrp65@gmail.com>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "appmenumodel.h"
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusServiceWatcher>
#include <QGuiApplication>
#include <QMenu>
// Includes for the menu search.
#include <KLocalizedString>
#include <QLineEdit>
#include <QListView>
#include <QWidgetAction>
#include <abstracttasksmodel.h>
#include <dbusmenuimporter.h>
class KDBusMenuImporter : public DBusMenuImporter
{
public:
KDBusMenuImporter(const QString &service, const QString &path, QObject *parent)
: DBusMenuImporter(service, path, parent)
{
}
protected:
QIcon iconForName(const QString &name) override
{
return QIcon::fromTheme(name);
}
};
AppMenuModel::AppMenuModel(QObject *parent)
: QAbstractListModel(parent)
, m_tasksModel(new TaskManager::TasksModel(this))
, m_serviceWatcher(new QDBusServiceWatcher(this))
{
m_tasksModel->setFilterByScreen(true);
connect(m_tasksModel, &TaskManager::TasksModel::activeTaskChanged, this, &AppMenuModel::onActiveWindowChanged);
connect(m_tasksModel,
&TaskManager::TasksModel::dataChanged,
[=](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()) {
Q_UNUSED(topLeft)
Q_UNUSED(bottomRight)
if (roles.contains(TaskManager::AbstractTasksModel::ApplicationMenuObjectPath)
|| roles.contains(TaskManager::AbstractTasksModel::ApplicationMenuServiceName) || roles.isEmpty()) {
onActiveWindowChanged();
}
});
connect(m_tasksModel, &TaskManager::TasksModel::activityChanged, this, &AppMenuModel::onActiveWindowChanged);
connect(m_tasksModel, &TaskManager::TasksModel::virtualDesktopChanged, this, &AppMenuModel::onActiveWindowChanged);
connect(m_tasksModel, &TaskManager::TasksModel::countChanged, this, &AppMenuModel::onActiveWindowChanged);
connect(m_tasksModel, &TaskManager::TasksModel::screenGeometryChanged, this, &AppMenuModel::screenGeometryChanged);
connect(this, &AppMenuModel::modelNeedsUpdate, this, [this] {
if (!m_updatePending) {
m_updatePending = true;
QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection);
}
});
onActiveWindowChanged();
m_serviceWatcher->setConnection(QDBusConnection::sessionBus());
// if our current DBus connection gets lost, close the menu
// we'll select the new menu when the focus changes
connect(m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this](const QString &serviceName) {
if (serviceName == m_serviceName) {
setMenuAvailable(false);
Q_EMIT modelNeedsUpdate();
}
});
// X11 has funky menu behaviour that prevents this from working properly.
if (KWindowSystem::isPlatformWayland()) {
m_searchAction = new QAction(this);
m_searchAction->setText(i18n("Search"));
m_searchAction->setObjectName(QStringLiteral("appmenu"));
m_searchMenu.reset(new QMenu);
auto searchAction = new QWidgetAction(this);
auto searchBar = new QLineEdit;
searchBar->setClearButtonEnabled(true);
searchBar->setPlaceholderText(i18n("Search…"));
searchBar->setMinimumWidth(200);
searchBar->setContentsMargins(4, 4, 4, 4);
connect(m_tasksModel, &TaskManager::TasksModel::activeTaskChanged, [=]() {
searchBar->setText(QString());
});
connect(searchBar, &QLineEdit::textChanged, [=]() mutable {
insertSearchActionsIntoMenu(searchBar->text());
});
connect(searchBar, &QLineEdit::returnPressed, [this]() mutable {
if (!m_currentSearchActions.empty()) {
m_currentSearchActions.constFirst()->trigger();
}
});
connect(this, &AppMenuModel::modelNeedsUpdate, this, [this, searchBar]() mutable {
insertSearchActionsIntoMenu(searchBar->text());
});
searchAction->setDefaultWidget(searchBar);
m_searchMenu->addAction(searchAction);
m_searchMenu->addSeparator();
m_searchAction->setMenu(m_searchMenu.get());
}
}
AppMenuModel::~AppMenuModel() = default;
bool AppMenuModel::menuAvailable() const
{
return m_menuAvailable;
}
void AppMenuModel::setMenuAvailable(bool set)
{
if (m_menuAvailable != set) {
m_menuAvailable = set;
setVisible(true);
Q_EMIT menuAvailableChanged();
}
}
bool AppMenuModel::visible() const
{
return m_visible;
}
void AppMenuModel::setVisible(bool visible)
{
if (m_visible != visible) {
m_visible = visible;
Q_EMIT visibleChanged();
}
}
QRect AppMenuModel::screenGeometry() const
{
return m_tasksModel->screenGeometry();
}
void AppMenuModel::setScreenGeometry(QRect geometry)
{
m_tasksModel->setScreenGeometry(geometry);
}
int AppMenuModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
if (!m_menuAvailable || !m_menu) {
return 0;
}
return m_menu->actions().count() + (m_searchAction ? 1 : 0);
}
void AppMenuModel::removeSearchActionsFromMenu()
{
for (auto action : std::as_const(m_currentSearchActions)) {
m_searchAction->menu()->removeAction(action);
}
m_currentSearchActions = QList<QAction *>();
}
void AppMenuModel::insertSearchActionsIntoMenu(const QString &filter)
{
removeSearchActionsFromMenu();
if (filter.isEmpty()) {
return;
}
const auto actions = flatActionList();
for (const auto &action : actions) {
if (action->text().contains(filter, Qt::CaseInsensitive)) {
m_searchAction->menu()->addAction(action);
m_currentSearchActions << action;
}
}
}
void AppMenuModel::update()
{
beginResetModel();
endResetModel();
m_updatePending = false;
}
void AppMenuModel::onActiveWindowChanged()
{
// Do not change active window when panel gets focus
// See ShellCorona::init() in shell/shellcorona.cpp
if (m_containmentStatus == Plasma::Types::AcceptingInputStatus) {
return;
}
const QModelIndex activeTaskIndex = m_tasksModel->activeTask();
const QString objectPath = m_tasksModel->data(activeTaskIndex, TaskManager::AbstractTasksModel::ApplicationMenuObjectPath).toString();
const QString serviceName = m_tasksModel->data(activeTaskIndex, TaskManager::AbstractTasksModel::ApplicationMenuServiceName).toString();
if (!objectPath.isEmpty() && !serviceName.isEmpty()) {
setMenuAvailable(true);
updateApplicationMenu(serviceName, objectPath);
setVisible(true);
Q_EMIT modelNeedsUpdate();
} else {
setMenuAvailable(false);
setVisible(false);
}
}
QHash<int, QByteArray> AppMenuModel::roleNames() const
{
QHash<int, QByteArray> roleNames;
roleNames[MenuRole] = QByteArrayLiteral("activeMenu");
roleNames[ActionRole] = QByteArrayLiteral("activeActions");
return roleNames;
}
QList<QAction *> AppMenuModel::flatActionList()
{
QList<QAction *> ret;
if (!m_menuAvailable || !m_menu) {
return ret;
}
const auto actions = m_menu->findChildren<QAction *>();
for (auto &action : actions) {
if (action->menu() == nullptr) {
ret << action;
}
}
return ret;
}
QVariant AppMenuModel::data(const QModelIndex &index, int role) const
{
if (!m_menuAvailable || !m_menu) {
return QVariant();
}
if (!index.isValid()) {
if (role == MenuRole) {
return QString();
} else if (role == ActionRole) {
return QVariant::fromValue(m_menu->menuAction());
}
}
const auto actions = m_menu->actions();
const int row = index.row();
if (row == actions.count() && m_searchAction) {
if (role == MenuRole) {
return m_searchAction->text();
} else if (role == ActionRole) {
return QVariant::fromValue(m_searchAction.data());
}
}
if (row >= actions.count()) {
return QVariant();
}
if (role == MenuRole) { // TODO this should be Qt::DisplayRole
return actions.at(row)->text();
} else if (role == ActionRole) {
return QVariant::fromValue(actions.at(row));
}
return QVariant();
}
void AppMenuModel::updateApplicationMenu(const QString &serviceName, const QString &menuObjectPath)
{
if (m_serviceName == serviceName && m_menuObjectPath == menuObjectPath) {
if (m_importer) {
QMetaObject::invokeMethod(m_importer, "updateMenu", Qt::QueuedConnection);
}
return;
}
m_serviceName = serviceName;
m_serviceWatcher->setWatchedServices(QStringList({m_serviceName}));
m_menuObjectPath = menuObjectPath;
if (m_importer) {
m_importer->deleteLater();
}
m_importer = new KDBusMenuImporter(serviceName, menuObjectPath, this);
QMetaObject::invokeMethod(m_importer, "updateMenu", Qt::QueuedConnection);
connect(m_importer.data(), &DBusMenuImporter::menuUpdated, this, [=](QMenu *menu) {
m_menu = m_importer->menu();
if (m_menu.isNull() || menu != m_menu) {
return;
}
// cache first layer of sub menus, which we'll be popping up
const auto actions = m_menu->actions();
for (QAction *a : actions) {
// signal dataChanged when the action changes
connect(a, &QAction::changed, this, [this, a] {
if (m_menuAvailable && m_menu) {
const int actionIdx = m_menu->actions().indexOf(a);
if (actionIdx > -1) {
const QModelIndex modelIdx = index(actionIdx, 0);
Q_EMIT dataChanged(modelIdx, modelIdx);
}
}
});
connect(a, &QAction::destroyed, this, &AppMenuModel::modelNeedsUpdate);
if (a->menu()) {
m_importer->updateMenu(a->menu());
}
}
setMenuAvailable(true);
Q_EMIT modelNeedsUpdate();
});
connect(m_importer.data(), &DBusMenuImporter::actionActivationRequested, this, [this](QAction *action) {
// TODO submenus
if (!m_menuAvailable || !m_menu) {
return;
}
const auto actions = m_menu->actions();
auto it = std::find(actions.begin(), actions.end(), action);
if (it != actions.end()) {
Q_EMIT requestActivateIndex(it - actions.begin());
}
});
}
|