Commit 43f81aa3 authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Reuse applets list for Windowed Widgets runner

This caused a lot of CPU load, because the applets were listed and filtered for each letter typed. Now they are only listed when the match() method is called for the first time and reused for the rest of the match session.
parent 9bc1f3bc
......@@ -30,6 +30,7 @@
#include <Plasma/Applet>
#include <Plasma/PluginLoader>
#include <QMutexLocker>
K_EXPORT_PLASMA_RUNNER_WITH_JSON(WindowedWidgetsRunner, "plasma-runner-windowedwidgets.json")
......@@ -42,6 +43,7 @@ WindowedWidgetsRunner::WindowedWidgetsRunner(QObject *parent, const QVariantList
addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds Plasma widgets whose name or description match :q:")));
addSyntax(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "mobile applications"), i18n("list all Plasma widgets that can run as standalone applications")));
setMinLetterCount(3);
connect(this, &AbstractRunner::teardown, this, [this](){ m_applets.clear(); });
}
WindowedWidgetsRunner::~WindowedWidgetsRunner()
......@@ -50,28 +52,22 @@ WindowedWidgetsRunner::~WindowedWidgetsRunner()
void WindowedWidgetsRunner::match(Plasma::RunnerContext &context)
{
loadMetadataList();
const QString term = context.query();
QList<Plasma::QueryMatch> matches;
const auto &listMetadata = Plasma::PluginLoader::self()->listAppletMetaData(QString());
for (const KPluginMetaData &md : listMetadata) {
if (!md.isValid()) {
continue;
}
for (const KPluginMetaData &md : qAsConst(m_applets)) {
if (((md.name().contains(term, Qt::CaseInsensitive) ||
md.value(QLatin1String("GenericName")).contains(term, Qt::CaseInsensitive) ||
md.description().contains(term, Qt::CaseInsensitive)) ||
md.category().contains(term, Qt::CaseInsensitive) ||
term.startsWith(i18nc("Note this is a KRunner keyword", "mobile applications"))) &&
!md.rawData().value(QStringLiteral("NoDisplay")).toBool()) {
if (!md.rawData().value(QStringLiteral("X-Plasma-StandAloneApp")).toBool()) {
continue;
}
term.startsWith(i18nc("Note this is a KRunner keyword", "mobile applications")))) {
Plasma::QueryMatch match(this);
setupMatch(md, match);
match.setText(md.name());
match.setSubtext(md.description());
match.setIconName(md.iconName());
match.setData(md.pluginId());
if (md.name().compare(term, Qt::CaseInsensitive) == 0) {
match.setType(Plasma::QueryMatch::ExactMatch);
match.setRelevance(1);
......@@ -96,14 +92,6 @@ void WindowedWidgetsRunner::run(const Plasma::RunnerContext &context, const Plas
QProcess::startDetached(QStringLiteral("plasmawindowed"), {match.data().toString()});
}
void WindowedWidgetsRunner::setupMatch(const KPluginMetaData &md, Plasma::QueryMatch &match)
{
match.setText(md.name());
match.setSubtext(md.description());
match.setIconName(md.iconName());
match.setData(md.pluginId());
}
QMimeData * WindowedWidgetsRunner::mimeDataForMatch(const Plasma::QueryMatch &match)
{
QMimeData *data = new QMimeData();
......@@ -111,6 +99,23 @@ QMimeData * WindowedWidgetsRunner::mimeDataForMatch(const Plasma::QueryMatch &ma
return data;
}
void WindowedWidgetsRunner::loadMetadataList()
{
// We call this method in the match thread
QMutexLocker locker(&m_mutex);
// If the entries have already been loaded we reuse them for the same session
if (!m_applets.isEmpty()) {
return;
}
const auto &listMetadata = Plasma::PluginLoader::self()->listAppletMetaData(QString());
for (const KPluginMetaData &md : listMetadata) {
if (md.isValid()
&& !md.rawData().value(QStringLiteral("NoDisplay")).toBool()
&& !md.rawData().value(QStringLiteral("X-Plasma-StandAloneApp")).toBool()) {
m_applets << md;
}
}
}
#include "windowedwidgetsrunner.moc"
......@@ -22,6 +22,7 @@
#include <KService>
#include <QMutex>
#include <krunner/abstractrunner.h>
......@@ -45,10 +46,10 @@ public:
protected Q_SLOTS:
QMimeData * mimeDataForMatch(const Plasma::QueryMatch &match) override;
protected:
void setupMatch(const KPluginMetaData &md, Plasma::QueryMatch &action);
private:
void loadMetadataList();
QList<KPluginMetaData> m_applets;
QMutex m_mutex;
};
#endif
......
Supports Markdown
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