quickshell/src/cpp/rootwrapper.cpp

72 lines
1.6 KiB
C++
Raw Normal View History

#include "rootwrapper.hpp"
#include <cstdlib>
#include <utility>
#include <qlogging.h>
#include <qobject.h>
#include <qqmlcomponent.h>
#include <qqmlengine.h>
#include <qurl.h>
2024-02-01 09:29:45 +00:00
#include "scavenge.hpp"
#include "shell.hpp"
RootWrapper::RootWrapper(QUrl rootUrl):
QObject(nullptr), rootUrl(std::move(rootUrl)), engine(this) {
2024-02-01 09:29:45 +00:00
this->reloadGraph(true);
if (this->activeRoot == nullptr) {
qCritical() << "could not create scene graph, exiting";
exit(-1); // NOLINT
}
}
2024-02-01 09:29:45 +00:00
void RootWrapper::reloadGraph(bool hard) {
if (this->activeRoot != nullptr) {
this->engine.clearComponentCache();
}
auto component = QQmlComponent(&this->engine, this->rootUrl);
2024-02-01 09:29:45 +00:00
SCAVENGE_PARENT = hard ? nullptr : this;
auto* obj = component.beginCreate(this->engine.rootContext());
2024-02-01 09:29:45 +00:00
SCAVENGE_PARENT = nullptr;
if (obj == nullptr) {
qWarning() << "failed to create root component";
return;
}
2024-02-01 09:29:45 +00:00
auto* newRoot = qobject_cast<QtShell*>(obj);
if (newRoot == nullptr) {
qWarning() << "root component was not a QtShell";
delete obj;
return;
}
component.completeCreate();
if (this->activeRoot != nullptr) {
this->activeRoot->deleteLater();
this->activeRoot = nullptr;
}
2024-02-01 09:29:45 +00:00
this->activeRoot = newRoot;
}
void RootWrapper::changeRoot(QtShell* newRoot) {
if (this->activeRoot != nullptr) {
QObject::disconnect(this->destroyConnection);
this->activeRoot->deleteLater();
}
if (newRoot != nullptr) {
this->activeRoot = newRoot;
QObject::connect(this->activeRoot, &QtShell::destroyed, this, &RootWrapper::destroy);
}
}
2024-02-01 09:29:45 +00:00
QObject* RootWrapper::scavengeTargetFor(QObject* /* child */) { return this->activeRoot; }
void RootWrapper::destroy() { this->deleteLater(); }