feat: emit signals on startup

Except loaded signal
This commit is contained in:
2 * r + 2 * t 2026-04-12 03:20:25 +10:00
parent 5a13cc22f6
commit bea72b4a64
2 changed files with 28 additions and 11 deletions

View file

@ -102,7 +102,13 @@ void RootConfig::setupFileBackend(const QString& path, const QString& screen) {
qCDebug(lcConfig) << "Setting up file backend for" << metaObject()->className() << "at" << path; qCDebug(lcConfig) << "Setting up file backend for" << metaObject()->className() << "at" << path;
updateWatch(); updateWatch();
reload();
// Load immediately so values are available during construction.
// Defer signal emissions to next event loop tick so QML has time to connect.
auto result = reloadFromFile();
QTimer::singleShot(0, this, [this, result] {
emitLoadSignals(result, false);
});
} }
void RootConfig::updateWatch() { void RootConfig::updateWatch() {
@ -189,12 +195,11 @@ std::optional<QString> RootConfig::reloadFromFile() {
clearLoadedKeys(); clearLoadedKeys();
auto jsonObj = doc.object(); auto jsonObj = doc.object();
const auto unknownKeys = collectUnknownKeys(this, jsonObj);
for (const auto& key : unknownKeys)
emit unknownOption(key, m_screen);
loadFromJson(jsonObj); loadFromJson(jsonObj);
// Collect unknown keys — caller is responsible for emitting signals
m_lastUnknownKeys = collectUnknownKeys(this, jsonObj);
return QString(); // success return QString(); // success
} }
@ -202,14 +207,24 @@ void RootConfig::save() {
saveToFile(); saveToFile();
} }
void RootConfig::reload() { void RootConfig::emitLoadSignals(const std::optional<QString>& result, bool emitLoaded) {
auto result = reloadFromFile(); if (!result.has_value())
if (result.has_value()) { return;
if (result->isEmpty())
for (const auto& key : std::as_const(m_lastUnknownKeys))
emit unknownOption(key, m_screen);
m_lastUnknownKeys.clear();
if (result->isEmpty()) {
if (emitLoaded)
emit loaded(m_screen); emit loaded(m_screen);
else } else {
emit loadFailed(*result, m_screen); emit loadFailed(*result, m_screen);
} }
} }
void RootConfig::reload() {
emitLoadSignals(reloadFromFile());
}
} // namespace caelestia::config } // namespace caelestia::config

View file

@ -35,6 +35,7 @@ signals:
private: private:
static QStringList collectUnknownKeys(const ConfigObject* obj, const QJsonObject& json); static QStringList collectUnknownKeys(const ConfigObject* obj, const QJsonObject& json);
void emitLoadSignals(const std::optional<QString>& result, bool emitLoaded = true);
void updateWatch(); void updateWatch();
void onWatcherEvent(); void onWatcherEvent();
@ -49,6 +50,7 @@ private:
QTimer* m_retryTimer = nullptr; QTimer* m_retryTimer = nullptr;
QTimer* m_reloadDebounce = nullptr; QTimer* m_reloadDebounce = nullptr;
int m_parseRetries = 0; int m_parseRetries = 0;
QStringList m_lastUnknownKeys;
}; };
} // namespace caelestia::config } // namespace caelestia::config