diff --git a/src/plasma/data/servicetypes/plasma-applet.desktop b/src/plasma/data/servicetypes/plasma-applet.desktop --- a/src/plasma/data/servicetypes/plasma-applet.desktop +++ b/src/plasma/data/servicetypes/plasma-applet.desktop @@ -83,6 +83,9 @@ [PropertyDef::X-Plasma-Provides] Type=QStringList +[PropertyDef::X-Plasma-PreloadWeight] +Type=int + [PropertyDef::X-Plasma-ConfigPlugins] Type=QStringList diff --git a/src/plasmaquick/appletquickitem.cpp b/src/plasmaquick/appletquickitem.cpp --- a/src/plasmaquick/appletquickitem.cpp +++ b/src/plasmaquick/appletquickitem.cpp @@ -43,6 +43,14 @@ QHash AppletQuickItemPrivate::s_rootObjects = QHash(); +//weight values for the logic for when or if to preload +static const int s_defaultPreloadWeight = 50; +static const int s_defaultLauncherPreloadWeight = 100; +static const int s_defaultDatePreloadWeight = 80; +static const int s_immediatePreloadWeight = 70; +static const int s_delayedPreloadWeight = 10; +static const int s_preloadWeightIncrement = 10; + AppletQuickItemPrivate::AppletQuickItemPrivate(Plasma::Applet *a, AppletQuickItem *item) : q(item), @@ -70,6 +78,23 @@ } } +int AppletQuickItemPrivate::preloadWeight() const +{ + int defaultWeight; + const QStringList provides(KPluginMetaData::readStringList(applet->pluginMetaData().rawData(), QStringLiteral("X-Plasma-Provides"))); + + //some applet types we want a bigger weight + if (provides.contains(QStringLiteral("org.kde.plasma.launchermenu"))) { + defaultWeight = s_defaultLauncherPreloadWeight; + } else if (provides.contains(QStringLiteral("org.kde.plasma.date"))) { + defaultWeight = s_defaultDatePreloadWeight; + } else { + defaultWeight = s_defaultPreloadWeight; + } + //default widgets to be barely preloaded + return qBound(0, applet->config().readEntry(QStringLiteral("PreloadWeight"), qMax(defaultWeight, applet->pluginMetaData().rawData().value(QStringLiteral("X-Plasma-PreloadWeight")).toInt())), 100); +} + void AppletQuickItemPrivate::connectLayoutAttached(QObject *item) { QObject *layout = 0; @@ -218,7 +243,7 @@ if (fullRepresentation && fullRepresentation != qmlObject->mainComponent()) { QVariantHash initialProperties; - initialProperties[QStringLiteral("parent")] = QVariant::fromValue(q); + initialProperties[QStringLiteral("parent")] = QVariant(); fullRepresentationItem = qobject_cast(qmlObject->createObjectFromComponent(fullRepresentation, QtQml::qmlContext(qmlObject->rootObject()), initialProperties)); } else { fullRepresentation = qmlObject->mainComponent(); @@ -598,6 +623,26 @@ d->compactRepresentationCheck(); qmlObject()->engine()->rootContext()->setBaseUrl(qmlObject()->source()); qmlObject()->engine()->setContextForObject(this, qmlObject()->engine()->rootContext()); + + //if we're expanded we don't care about preloading because it will already be the case + //as well as for containments + if (d->applet->isContainment() || + d->expanded || d->preferredRepresentation == d->fullRepresentation) { + return; + } + + const int preloadWeight = d->preloadWeight(); + //decrease weight until we open it again + applet()->config().writeEntry(QStringLiteral("PreloadWeight"), qMax(0, preloadWeight - s_preloadWeightIncrement)); + + if (preloadWeight >= s_immediatePreloadWeight) { + d->createFullRepresentationItem(); + } else if (preloadWeight >= s_delayedPreloadWeight) { + //spread the creation over a random delay between 2 and 10 seconds, to make it look plasma started before, and load everything in the background without big noticeable freezes + QTimer::singleShot(qrand() % ((10000 + 1) - 2000) + 2000, [this]() { + d->createFullRepresentationItem(); + }); + } } @@ -738,6 +783,11 @@ } else { d->fullRepresentationItem->setProperty("parent", QVariant::fromValue(this)); } + + //increase on open, ignore containments + if (!d->applet->isContainment()) { + applet()->config().writeEntry(QStringLiteral("PreloadWeight"), qMin(d->preloadWeight() + s_preloadWeightIncrement, 100)); + } } d->expanded = expanded; diff --git a/src/plasmaquick/private/appletquickitem_p.h b/src/plasmaquick/private/appletquickitem_p.h --- a/src/plasmaquick/private/appletquickitem_p.h +++ b/src/plasmaquick/private/appletquickitem_p.h @@ -59,6 +59,8 @@ void init(); + int preloadWeight() const; + QQuickItem *createCompactRepresentationItem(); QQuickItem *createFullRepresentationItem(); QQuickItem *createCompactRepresentationExpanderItem();