mobileshell: Dynamically load in heavy dependencies for singletons

This commit is contained in:
Devin Lin 2023-03-18 22:43:59 -07:00
parent 2d1610aaa2
commit 6160280b0c
3 changed files with 41 additions and 16 deletions

View file

@ -2,20 +2,38 @@
// SPDX-License-Identifier: GPL-2.0-or-later
import QtQuick
import org.kde.plasma.private.mobileshell as MobileShell
import org.kde.plasma.private.mobileshell.windowplugin as WindowPlugin
// NOTE: This is a singleton in the mobileshell library, so we need to be careful to
// make this load as fast as possible (since it may be loaded in other processes ex. lockscreen).
pragma Singleton
QtObject {
id: root
/**
* Activates an application by storage id if it is already running, or launch the application.
*/
function launchOrActivateApp(storageId) {
const launched = WindowPlugin.WindowUtil.activateWindowByStorageId(storageId);
if (!launched) {
MobileShell.ShellUtil.launchApp(storageId);
}
// We don't want to import WindowPlugin initially because it has side-effects and slows down initial load.
// -> only import it if we actually run the function
const component = Qt.createQmlObject(`
import QtQuick
import org.kde.plasma.private.mobileshell as MobileShell
import org.kde.plasma.private.mobileshell.windowplugin as WindowPlugin
QtObject {
Component.onCompleted: {
const launched = WindowPlugin.WindowUtil.activateWindowByStorageId("${storageId}");
if (!launched) {
MobileShell.ShellUtil.launchApp("${storageId}");
}
}
}
`, root, "runSnippet");
component.destroy();
}
}

View file

@ -5,6 +5,9 @@ import QtQuick
import org.kde.plasma.core as PlasmaCore
// NOTE: This is a singleton in the mobileshell library, so we need to be careful to
// make this load as fast as possible (since it may be loaded in other processes ex. lockscreen).
pragma Singleton
QtObject {

View file

@ -26,16 +26,6 @@ ShellUtil::ShellUtil(QObject *parent)
: QObject{parent}
, m_localeConfig{KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::SimpleConfig)}
{
m_localeConfigWatcher = KConfigWatcher::create(m_localeConfig);
// watch for changes to locale config, to update 12/24 hour time
connect(m_localeConfigWatcher.data(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group, const QByteArrayList &names) -> void {
if (group.name() == "Locale") {
// we have to reparse for new changes (from system settings)
m_localeConfig->reparseConfiguration();
Q_EMIT isSystem24HourFormatChanged();
}
});
}
ShellUtil *ShellUtil::instance()
@ -71,6 +61,20 @@ void ShellUtil::executeCommand(const QString &command)
bool ShellUtil::isSystem24HourFormat()
{
// only load the config watcher if this function is actually used once
if (!m_localeConfigWatcher) {
m_localeConfigWatcher = KConfigWatcher::create(m_localeConfig);
// watch for changes to locale config, to update 12/24 hour time
connect(m_localeConfigWatcher.data(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group, const QByteArrayList &names) -> void {
if (group.name() == "Locale") {
// we have to reparse for new changes (from system settings)
m_localeConfig->reparseConfiguration();
Q_EMIT isSystem24HourFormatChanged();
}
});
}
KConfigGroup localeSettings = KConfigGroup(m_localeConfig, "Locale");
QString timeFormat = localeSettings.readEntry("TimeFormat", QStringLiteral(FORMAT24H));