feat: add auto save on option changes

This commit is contained in:
2 * r + 2 * t 2026-04-12 03:58:35 +10:00
parent dd15b1d80e
commit f1fb68c721
2 changed files with 27 additions and 0 deletions

View file

@ -96,6 +96,9 @@ void RootConfig::setupFileBackend(const QString& path, const QString& screen) {
m_reloadDebounce->setInterval(50);
connect(m_reloadDebounce, &QTimer::timeout, this, &RootConfig::reload);
// Auto-save when any property changes (debounced by the save timer)
connectAutoSave(this);
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &RootConfig::onWatcherEvent);
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &RootConfig::onWatcherEvent);
@ -111,6 +114,23 @@ void RootConfig::setupFileBackend(const QString& path, const QString& screen) {
});
}
void RootConfig::connectAutoSave(ConfigObject* obj) {
connect(obj, &ConfigObject::propertiesChanged, this, [this] {
if (!m_loading)
saveToFile();
});
// Recurse into sub-objects
const auto* meta = obj->metaObject();
for (int i = meta->propertyOffset(); i < meta->propertyCount(); ++i) {
auto prop = meta->property(i);
auto value = prop.read(obj);
auto* subObj = value.value<ConfigObject*>();
if (subObj)
connectAutoSave(subObj);
}
}
void RootConfig::updateWatch() {
auto targetDir = QFileInfo(m_filePath).absolutePath();
@ -192,11 +212,15 @@ std::optional<QString> RootConfig::reloadFromFile() {
qCDebug(lcConfig) << "Reloading" << metaObject()->className() << "from" << m_filePath;
m_loading = true;
clearLoadedKeys();
auto jsonObj = doc.object();
loadFromJson(jsonObj);
m_loading = false;
// Collect unknown keys — caller is responsible for emitting signals
m_lastUnknownKeys = collectUnknownKeys(this, jsonObj);

View file

@ -39,10 +39,13 @@ private:
void updateWatch();
void onWatcherEvent();
void connectAutoSave(ConfigObject* obj);
QString m_filePath;
QString m_screen;
QString m_watchedDir;
bool m_recentlySaved = false;
bool m_loading = false;
QFileSystemWatcher* m_watcher = nullptr;
QTimer* m_saveTimer = nullptr;