reload: encapsulate each engine generation more

This commit is contained in:
outfoxxed 2024-03-13 22:53:05 -07:00
parent 211f454de9
commit 1687ff3614
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
7 changed files with 125 additions and 79 deletions

53
src/core/generation.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "generation.hpp"
#include <qcontainerfwd.h>
#include <qcoreapplication.h>
#include <qhash.h>
#include <qobject.h>
#include <qqmlcontext.h>
#include <qqmlengine.h>
#include <qtimer.h>
#include "plugin.hpp"
#include "reload.hpp"
static QHash<QQmlEngine*, EngineGeneration*> g_generations; // NOLINT
EngineGeneration::EngineGeneration() { g_generations.insert(&this->engine, this); }
EngineGeneration::~EngineGeneration() {
g_generations.remove(&this->engine);
if (this->root != nullptr) this->root->deleteLater();
}
void EngineGeneration::onReload(EngineGeneration* old) {
auto* app = QCoreApplication::instance();
QObject::connect(&this->engine, &QQmlEngine::quit, app, &QCoreApplication::quit);
QObject::connect(&this->engine, &QQmlEngine::exit, app, &QCoreApplication::exit);
this->root->onReload(old == nullptr ? nullptr : old->root);
this->singletonRegistry.onReload(old == nullptr ? nullptr : &old->singletonRegistry);
delete old;
if (old != nullptr) {
QTimer::singleShot(0, [this]() {
QuickshellPlugin::runOnReload();
PostReloadHook::postReloadTree(this->root);
});
} else {
QuickshellPlugin::runOnReload();
PostReloadHook::postReloadTree(this->root);
}
}
EngineGeneration* EngineGeneration::findObjectGeneration(QObject* object) {
while (object != nullptr) {
auto* context = QQmlEngine::contextForObject(object);
if (auto* generation = g_generations.value(context->engine())) {
return generation;
}
}
return nullptr;
}