halcyon: Move settings to config group

Similar to
https://invent.kde.org/plasma/plasma-mobile/-/merge_requests/784 but for
halcyon (see rationale there).
This commit is contained in:
Devin Lin 2025-07-16 01:42:56 -04:00
parent 7dc2d980aa
commit 32584ebd54
6 changed files with 85 additions and 44 deletions

View file

@ -3,14 +3,31 @@
#include "halcyonsettings.h"
const QString CFG_KEY_SHOW_WALLPAPER_BLUR = QStringLiteral("wallpaperBlurEffect");
const QString CFG_KEY_DOUBLE_TAP_TO_LOCK = QStringLiteral("doubleTapToLock");
using namespace Qt::Literals::StringLiterals;
HalcyonSettings::HalcyonSettings(QObject *parent, KConfigGroup config)
: QObject{parent}
, m_config{config}
// The config group all of the settings are under
static constexpr auto CFG_GROUP_HALCYON = "Halcyon"_L1;
static constexpr auto CFG_KEY_PINNED = "pinned"_L1;
static constexpr auto CFG_KEY_SHOW_WALLPAPER_BLUR = "wallpaperBlurEffect"_L1;
static constexpr auto CFG_KEY_DOUBLE_TAP_TO_LOCK = "doubleTapToLock"_L1;
HalcyonSettings::HalcyonSettings(Plasma::Applet *applet, QObject *parent)
: QObject{parent}
, m_applet{applet}
{
load();
}
QString HalcyonSettings::pinned() const
{
return configGroup().readEntry(CFG_KEY_PINNED, u"{}"_s);
}
void HalcyonSettings::setPinned(const QString &pinnedJson)
{
// Saved separately from other options, since it's changed from the homescreen (not settings window)
configGroup().writeEntry(CFG_KEY_PINNED, pinnedJson);
m_applet->config().sync();
}
HalcyonSettings::WallpaperBlurEffect HalcyonSettings::wallpaperBlurEffect() const
@ -43,17 +60,49 @@ void HalcyonSettings::setDoubleTapToLock(bool doubleTapToLock)
void HalcyonSettings::save()
{
m_config.writeEntry(CFG_KEY_SHOW_WALLPAPER_BLUR, (int)m_wallpaperBlurEffect);
m_config.writeEntry(CFG_KEY_DOUBLE_TAP_TO_LOCK, m_doubleTapToLock);
auto group = configGroup();
group.writeEntry(CFG_KEY_SHOW_WALLPAPER_BLUR, (int)m_wallpaperBlurEffect);
group.writeEntry(CFG_KEY_DOUBLE_TAP_TO_LOCK, m_doubleTapToLock);
m_config.sync();
m_applet->config().sync();
}
void HalcyonSettings::load()
{
m_wallpaperBlurEffect = static_cast<WallpaperBlurEffect>(m_config.readEntry(CFG_KEY_SHOW_WALLPAPER_BLUR, (int)Full));
m_doubleTapToLock = m_config.readEntry(CFG_KEY_DOUBLE_TAP_TO_LOCK, true);
migrateConfigFromPlasma6_4();
auto group = configGroup();
m_wallpaperBlurEffect = static_cast<WallpaperBlurEffect>(group.readEntry(CFG_KEY_SHOW_WALLPAPER_BLUR, (int)Full));
m_doubleTapToLock = group.readEntry(CFG_KEY_DOUBLE_TAP_TO_LOCK, true);
Q_EMIT doubleTapToLockChanged();
Q_EMIT wallpaperBlurEffectChanged();
}
KConfigGroup HalcyonSettings::configGroup() const
{
if (!m_applet) {
return KConfigGroup{};
}
return m_applet->config().group(CFG_GROUP_HALCYON);
}
void HalcyonSettings::migrateConfigFromPlasma6_4()
{
// Migrate config options (from before Plasma 6.5) from the root config group to [General]
// When adding new config options, do not update this function!
auto oldConfigGroup = m_applet->config();
auto newConfigGroup = configGroup();
const QString oldKey = u"Pinned"_s;
if (!oldConfigGroup.hasKey(oldKey) || newConfigGroup.hasKey(CFG_KEY_PINNED)) {
return;
}
newConfigGroup.writeEntry(CFG_KEY_PINNED, oldConfigGroup.readEntry(oldKey, u"{}"_s));
oldConfigGroup.deleteEntry(oldKey);
m_applet->config().sync();
}

View file

@ -7,6 +7,8 @@
#include <KConfigGroup>
#include <Plasma/Applet>
#include <qqmlregistration.h>
class HalcyonSettings : public QObject
@ -18,7 +20,7 @@ class HalcyonSettings : public QObject
Q_PROPERTY(bool doubleTapToLock READ doubleTapToLock WRITE setDoubleTapToLock NOTIFY doubleTapToLockChanged)
public:
HalcyonSettings(QObject *parent = nullptr, KConfigGroup config = {});
HalcyonSettings(Plasma::Applet *applet = nullptr, QObject *parent = nullptr);
enum WallpaperBlurEffect {
None = 0,
@ -27,22 +29,29 @@ public:
};
Q_ENUM(WallpaperBlurEffect)
QString pinned() const;
void setPinned(const QString &pinnedJson);
WallpaperBlurEffect wallpaperBlurEffect() const;
void setWallpaperBlurEffect(WallpaperBlurEffect wallpaperBlurEffect);
bool doubleTapToLock() const;
void setDoubleTapToLock(bool doubleTapToLock);
Q_INVOKABLE void load();
Q_SIGNALS:
void wallpaperBlurEffectChanged();
void doubleTapToLockChanged();
private:
void save();
void load();
KConfigGroup configGroup() const;
void migrateConfigFromPlasma6_4();
WallpaperBlurEffect m_wallpaperBlurEffect{Full};
bool m_doubleTapToLock{true};
KConfigGroup m_config;
Plasma::Applet *m_applet;
};

View file

@ -13,8 +13,8 @@ K_PLUGIN_CLASS_WITH_JSON(HomeScreen, "metadata.json")
HomeScreen::HomeScreen(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
: Plasma::Containment{parent, data, args}
, m_settings{new HalcyonSettings{this, config()}}
, m_pinnedModel{new PinnedModel{this}}
, m_settings{new HalcyonSettings{this, this}}
, m_pinnedModel{new PinnedModel{m_settings, this}}
{
setHasConfigurationInterface(true);
}

View file

@ -8,11 +8,10 @@
#include <KLocalizedString>
PinnedModel::PinnedModel(Plasma::Applet *parent)
PinnedModel::PinnedModel(HalcyonSettings *settings, QObject *parent)
: QAbstractListModel{parent}
, m_applet{parent}
, m_settings{settings}
{
load();
}
PinnedModel::~PinnedModel() = default;
@ -181,11 +180,7 @@ void PinnedModel::addAppToFolder(int appRow, int folderRow)
void PinnedModel::load()
{
if (!m_applet) {
return;
}
QJsonDocument doc = QJsonDocument::fromJson(m_applet->config().readEntry("Pinned", "{}").toUtf8());
QJsonDocument doc = QJsonDocument::fromJson(m_settings->pinned().toUtf8());
beginResetModel();
@ -218,10 +213,6 @@ void PinnedModel::load()
void PinnedModel::save()
{
if (!m_applet) {
return;
}
QJsonArray arr;
for (int i = 0; i < m_applications.size() && i < m_folders.size(); i++) {
if (m_applications[i]) {
@ -232,16 +223,10 @@ void PinnedModel::save()
}
QByteArray data = QJsonDocument(arr).toJson(QJsonDocument::Compact);
m_applet->config().writeEntry("Pinned", QString::fromStdString(data.toStdString()));
Q_EMIT m_applet->configNeedsSaving();
m_settings->setPinned(QString::fromStdString(data.toStdString()));
}
void PinnedModel::addAppFromFolder(const QString &storageId)
{
addApp(storageId, 0);
}
Plasma::Applet *PinnedModel::applet()
{
return m_applet;
}

View file

@ -5,6 +5,7 @@
#include "application.h"
#include "applicationfolder.h"
#include "halcyonsettings.h"
#include <QAbstractListModel>
#include <QList>
@ -12,8 +13,6 @@
#include <QQuickItem>
#include <QSet>
#include <Plasma/Applet>
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/plasmawindowmanagement.h>
#include <KWayland/Client/registry.h>
@ -37,7 +36,7 @@ public:
FolderRole
};
PinnedModel(Plasma::Applet *parent = nullptr);
PinnedModel(HalcyonSettings *settings = nullptr, QObject *parent = nullptr);
~PinnedModel() override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
@ -52,19 +51,15 @@ public:
Q_INVOKABLE void createFolderFromApps(int sourceAppRow, int draggedAppRow);
Q_INVOKABLE void addAppToFolder(int appRow, int folderRow);
Q_INVOKABLE void load();
void save();
Plasma::Applet *applet();
void setApplet(Plasma::Applet *applet);
public Q_SLOTS:
void addAppFromFolder(const QString &storageId);
private:
void load();
QList<Application *> m_applications;
QList<ApplicationFolder *> m_folders;
Plasma::Applet *m_applet;
HalcyonSettings *m_settings;
};

View file

@ -19,6 +19,9 @@ ContainmentItem {
id: root
Component.onCompleted: {
Plasmoid.settings.load();
Plasmoid.pinnedModel.load();
Halcyon.ApplicationListModel.loadApplications();
forceActiveFocus();
}