Update applet alternatives menu entry visibility on demand

It's reasnoble to expect people to download an applet just before trying
to change an existing applet using the alternatives system.

Therefore doing the check at applet initialisation time is semantically
wrong. Especially as it doesn't just disable the action but also skips
creating it.

Finally this gives a startup performance boost; loading all the applets
metadata takes a small amount of time (~20 milliseconds) not really
noticable when you're doing it once, but it adds up if you're doing it
over multiple applets sequentially.
This commit is contained in:
David Edmundson 2016-10-03 01:33:01 +01:00
parent 818a67ea91
commit 048d5243f0
2 changed files with 30 additions and 29 deletions

View File

@ -545,12 +545,6 @@ void Applet::flushPendingConstraintsEvents()
action->setEnabled(unlocked);
}
action = d->actions->action(QStringLiteral("alternatives"));
if (action) {
action->setVisible(unlocked);
action->setEnabled(unlocked);
}
action = d->actions->action(QStringLiteral("configure"));
if (action && d->hasConfigurationInterface) {
bool canConfig = unlocked || KAuthorized::authorize(QStringLiteral("plasma/allow_configure_when_locked"));

View File

@ -184,30 +184,37 @@ void AppletPrivate::init(const QString &packagePath, const QVariantList &args)
api, appletDescription.name()));
}
if (!q->isContainment() && q->pluginInfo().isValid()) {
QStringList provides = q->pluginInfo().property(QStringLiteral("X-Plasma-Provides")).toStringList();
if (!provides.isEmpty()) {
auto filter = [&provides](const KPluginMetaData &md) -> bool
{
foreach (const QString &p, provides) {
if (md.value(QStringLiteral("X-Plasma-Provides")).contains(p)) {
return true;
}
}
return false;
};
QList<KPluginMetaData> applets = KPackage::PackageLoader::self()->findPackages(QStringLiteral("Plasma/Applet"), QString(), filter);
if (applets.count() > 1) {
QAction *a = new QAction(QIcon::fromTheme(QStringLiteral("preferences-desktop-default-applications")), i18n("Alternatives..."), q);
q->actions()->addAction(QStringLiteral("alternatives"), a);
QObject::connect(a, &QAction::triggered,[=] {
if (q->containment()) {
emit q->containment()->appletAlternativesRequested(q);
}
});
if (!q->isContainment()) {
QAction *a = new QAction(QIcon::fromTheme(QStringLiteral("preferences-desktop-default-applications")), i18n("Alternatives..."), q);
q->actions()->addAction(QStringLiteral("alternatives"), a);
QObject::connect(a, &QAction::triggered,[=] {
if (q->containment()) {
emit q->containment()->appletAlternativesRequested(q);
}
}
});
QObject::connect(q, &Applet::contextualActionsAboutToShow, a, [=]() {
bool hasAlternatives = false;
QStringList provides = q->pluginInfo().property(QStringLiteral("X-Plasma-Provides")).toStringList();
if (!provides.isEmpty() && q->immutability() == Types::Mutable) {
auto filter = [&provides](const KPluginMetaData &md) -> bool
{
foreach (const QString &p, provides) {
if (md.value(QStringLiteral("X-Plasma-Provides")).contains(p)) {
return true;
}
}
return false;
};
QList<KPluginMetaData> applets = KPackage::PackageLoader::self()->findPackages(QStringLiteral("Plasma/Applet"), QString(), filter);
if (applets.count() > 1) {
hasAlternatives = true;
}
}
a->setVisible(hasAlternatives);
});
}
}