2024-02-01 01:43:18 +00:00
|
|
|
#include "rootwrapper.hpp"
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <utility>
|
|
|
|
|
2024-03-04 01:05:19 +00:00
|
|
|
#include <qdir.h>
|
2024-02-05 07:00:59 +00:00
|
|
|
#include <qfileinfo.h>
|
2024-02-01 01:43:18 +00:00
|
|
|
#include <qlogging.h>
|
|
|
|
#include <qobject.h>
|
|
|
|
#include <qqmlcomponent.h>
|
|
|
|
#include <qqmlengine.h>
|
2024-03-03 09:26:43 +00:00
|
|
|
#include <qtimer.h>
|
2024-02-01 01:43:18 +00:00
|
|
|
#include <qurl.h>
|
|
|
|
|
2024-03-04 01:05:19 +00:00
|
|
|
#include "qmlglobal.hpp"
|
2024-03-03 09:26:43 +00:00
|
|
|
#include "reload.hpp"
|
2024-02-01 01:43:18 +00:00
|
|
|
#include "shell.hpp"
|
2024-02-05 07:00:59 +00:00
|
|
|
#include "watcher.hpp"
|
2024-02-01 01:43:18 +00:00
|
|
|
|
2024-02-24 10:06:40 +00:00
|
|
|
RootWrapper::RootWrapper(QString rootPath)
|
|
|
|
: QObject(nullptr)
|
|
|
|
, rootPath(std::move(rootPath))
|
2024-03-04 01:05:19 +00:00
|
|
|
, engine(this)
|
|
|
|
, originalWorkingDirectory(QDir::current().absolutePath()) {
|
2024-02-01 09:29:45 +00:00
|
|
|
this->reloadGraph(true);
|
2024-02-01 01:43:18 +00:00
|
|
|
|
2024-02-05 07:00:59 +00:00
|
|
|
if (this->root == nullptr) {
|
2024-02-01 01:43:18 +00:00
|
|
|
qCritical() << "could not create scene graph, exiting";
|
|
|
|
exit(-1); // NOLINT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 14:26:41 +00:00
|
|
|
RootWrapper::~RootWrapper() {
|
|
|
|
// event loop may no longer be running so deleteLater is not an option
|
2024-03-04 01:05:19 +00:00
|
|
|
QuickshellGlobal::deleteInstance();
|
2024-03-03 14:26:41 +00:00
|
|
|
delete this->root;
|
|
|
|
}
|
|
|
|
|
2024-02-01 09:29:45 +00:00
|
|
|
void RootWrapper::reloadGraph(bool hard) {
|
2024-03-04 01:05:19 +00:00
|
|
|
QuickshellGlobal::deleteInstance();
|
|
|
|
|
2024-02-05 07:00:59 +00:00
|
|
|
if (this->root != nullptr) {
|
2024-02-01 01:43:18 +00:00
|
|
|
this->engine.clearComponentCache();
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:19 +00:00
|
|
|
QDir::setCurrent(this->originalWorkingDirectory);
|
|
|
|
|
2024-02-05 07:00:59 +00:00
|
|
|
auto component = QQmlComponent(&this->engine, QUrl::fromLocalFile(this->rootPath));
|
2024-02-01 09:29:45 +00:00
|
|
|
|
2024-02-01 01:43:18 +00:00
|
|
|
auto* obj = component.beginCreate(this->engine.rootContext());
|
2024-02-01 09:29:45 +00:00
|
|
|
|
2024-02-01 01:43:18 +00:00
|
|
|
if (obj == nullptr) {
|
2024-02-03 09:02:04 +00:00
|
|
|
qWarning() << component.errorString().toStdString().c_str();
|
2024-02-01 01:43:18 +00:00
|
|
|
qWarning() << "failed to create root component";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-05 07:00:59 +00:00
|
|
|
auto* newRoot = qobject_cast<ShellRoot*>(obj);
|
2024-02-01 09:29:45 +00:00
|
|
|
if (newRoot == nullptr) {
|
2024-02-26 08:57:47 +00:00
|
|
|
qWarning() << "root component was not a Quickshell.ShellRoot";
|
2024-02-01 01:43:18 +00:00
|
|
|
delete obj;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
component.completeCreate();
|
|
|
|
|
2024-03-03 09:26:43 +00:00
|
|
|
auto* oldRoot = this->root;
|
|
|
|
this->root = newRoot;
|
2024-02-16 14:38:20 +00:00
|
|
|
|
2024-03-03 09:26:43 +00:00
|
|
|
this->root->onReload(hard ? nullptr : oldRoot);
|
|
|
|
|
|
|
|
if (oldRoot != nullptr) {
|
|
|
|
oldRoot->deleteLater();
|
|
|
|
|
|
|
|
QTimer::singleShot(0, [this, newRoot]() {
|
|
|
|
if (this->root == newRoot) PostReloadHook::postReloadTree(this->root);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
PostReloadHook::postReloadTree(newRoot);
|
2024-02-01 01:43:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 07:00:59 +00:00
|
|
|
this->onConfigChanged();
|
2024-02-01 01:43:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 07:00:59 +00:00
|
|
|
void RootWrapper::onConfigChanged() {
|
|
|
|
auto config = this->root->config();
|
|
|
|
|
|
|
|
if (config.mWatchFiles && this->configWatcher == nullptr) {
|
|
|
|
this->configWatcher = new FiletreeWatcher();
|
|
|
|
this->configWatcher->addPath(QFileInfo(this->rootPath).dir().path());
|
2024-02-01 01:43:18 +00:00
|
|
|
|
2024-02-05 07:00:59 +00:00
|
|
|
QObject::connect(this->root, &ShellRoot::configChanged, this, &RootWrapper::onConfigChanged);
|
|
|
|
|
|
|
|
QObject::connect(
|
|
|
|
this->configWatcher,
|
|
|
|
&FiletreeWatcher::fileChanged,
|
|
|
|
this,
|
|
|
|
&RootWrapper::onWatchedFilesChanged
|
|
|
|
);
|
|
|
|
} else if (!config.mWatchFiles && this->configWatcher != nullptr) {
|
|
|
|
this->configWatcher->deleteLater();
|
|
|
|
this->configWatcher = nullptr;
|
2024-02-01 01:43:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 07:00:59 +00:00
|
|
|
void RootWrapper::onWatchedFilesChanged() { this->reloadGraph(false); }
|