root: recreate the qml engine on reload instead of clearing it

This causes singletons to be recreated instead of kept alive.
This commit is contained in:
outfoxxed 2024-03-12 14:55:51 -07:00
parent 9f6ef37f61
commit 463f9a297f
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
9 changed files with 109 additions and 78 deletions

View file

@ -70,6 +70,8 @@ file(GENERATE
CONTENT "" CONTENT ""
) )
# pch breaks clang-tidy..... somehow
if (NOT NO_PCH)
add_library(qt-pch ${CMAKE_CURRENT_BINARY_DIR}/pchstub.cpp) add_library(qt-pch ${CMAKE_CURRENT_BINARY_DIR}/pchstub.cpp)
target_link_libraries(qt-pch PRIVATE ${QT_DEPS}) target_link_libraries(qt-pch PRIVATE ${QT_DEPS})
target_precompile_headers(qt-pch PUBLIC target_precompile_headers(qt-pch PUBLIC
@ -81,10 +83,13 @@ target_precompile_headers(qt-pch PUBLIC
<qquickitem.h> <qquickitem.h>
<qevent.h> <qevent.h>
) )
endif()
function (qs_pch target) function (qs_pch target)
if (NOT NO_PCH)
target_precompile_headers(${target} REUSE_FROM qt-pch) target_precompile_headers(${target} REUSE_FROM qt-pch)
target_link_libraries(${target} PRIVATE ${QT_DEPS}) # required for gcc to accept the pch on plugin targets target_link_libraries(${target} PRIVATE ${QT_DEPS}) # required for gcc to accept the pch on plugin targets
endif()
endfunction() endfunction()
add_subdirectory(src) add_subdirectory(src)

View file

@ -1,5 +1,4 @@
#include "main.hpp" #include "main.hpp"
#include <iostream> #include <iostream>
#include <qcommandlineoption.h> #include <qcommandlineoption.h>

View file

@ -57,38 +57,81 @@ void QuickshellSettings::setWatchFiles(bool watchFiles) {
emit this->watchFilesChanged(); emit this->watchFilesChanged();
} }
QuickshellGlobal::QuickshellGlobal(QObject* parent): QObject(parent) { QuickshellTracked::QuickshellTracked() {
// clang-format off
QObject::connect(QuickshellSettings::instance(), &QuickshellSettings::workingDirectoryChanged, this, &QuickshellGlobal::workingDirectoryChanged);
QObject::connect(QuickshellSettings::instance(), &QuickshellSettings::watchFilesChanged, this, &QuickshellGlobal::watchFilesChanged);
QObject::connect(QuickshellSettings::instance(), &QuickshellSettings::lastWindowClosed, this, &QuickshellGlobal::lastWindowClosed);
// clang-format on
auto* app = QCoreApplication::instance(); auto* app = QCoreApplication::instance();
auto* guiApp = qobject_cast<QGuiApplication*>(app); auto* guiApp = qobject_cast<QGuiApplication*>(app);
if (guiApp != nullptr) { if (guiApp != nullptr) {
// clang-format off // clang-format off
QObject::connect(guiApp, &QGuiApplication::primaryScreenChanged, this, &QuickshellGlobal::updateScreens); QObject::connect(guiApp, &QGuiApplication::primaryScreenChanged, this, &QuickshellTracked::updateScreens);
QObject::connect(guiApp, &QGuiApplication::screenAdded, this, &QuickshellGlobal::updateScreens); QObject::connect(guiApp, &QGuiApplication::screenAdded, this, &QuickshellTracked::updateScreens);
QObject::connect(guiApp, &QGuiApplication::screenRemoved, this, &QuickshellGlobal::updateScreens); QObject::connect(guiApp, &QGuiApplication::screenRemoved, this, &QuickshellTracked::updateScreens);
// clang-format on // clang-format on
this->updateScreens(); this->updateScreens();
} }
} }
QuickshellTracked* QuickshellTracked::instance() {
static QuickshellTracked* instance = nullptr; // NOLINT
if (instance == nullptr) {
QJSEngine::setObjectOwnership(instance, QJSEngine::CppOwnership);
instance = new QuickshellTracked();
}
return instance;
}
void QuickshellTracked::updateScreens() {
auto screens = QGuiApplication::screens();
auto newScreens = QList<QuickshellScreenInfo*>();
for (auto* newScreen: screens) {
for (auto i = 0; i < this->screens.length(); i++) {
auto* oldScreen = this->screens[i];
if (newScreen == oldScreen->screen) {
newScreens.push_back(oldScreen);
this->screens.remove(i);
goto next;
}
}
{
auto* si = new QuickshellScreenInfo(this, newScreen);
QQmlEngine::setObjectOwnership(si, QQmlEngine::CppOwnership);
newScreens.push_back(si);
}
next:;
}
for (auto* oldScreen: this->screens) {
oldScreen->deleteLater();
}
this->screens = newScreens;
emit this->screensChanged();
}
QuickshellGlobal::QuickshellGlobal(QObject* parent): QObject(parent) {
// clang-format off
QObject::connect(QuickshellSettings::instance(), &QuickshellSettings::workingDirectoryChanged, this, &QuickshellGlobal::workingDirectoryChanged);
QObject::connect(QuickshellSettings::instance(), &QuickshellSettings::watchFilesChanged, this, &QuickshellGlobal::watchFilesChanged);
QObject::connect(QuickshellSettings::instance(), &QuickshellSettings::lastWindowClosed, this, &QuickshellGlobal::lastWindowClosed);
QObject::connect(QuickshellTracked::instance(), &QuickshellTracked::screensChanged, this, &QuickshellGlobal::screensChanged);
// clang-format on
}
qint32 QuickshellGlobal::processId() const { // NOLINT qint32 QuickshellGlobal::processId() const { // NOLINT
return getpid(); return getpid();
} }
qsizetype QuickshellGlobal::screensCount(QQmlListProperty<QuickshellScreenInfo>* prop) { qsizetype QuickshellGlobal::screensCount(QQmlListProperty<QuickshellScreenInfo>* /*unused*/) {
return static_cast<QuickshellGlobal*>(prop->object)->mScreens.size(); // NOLINT return QuickshellTracked::instance()->screens.size();
} }
QuickshellScreenInfo* QuickshellScreenInfo*
QuickshellGlobal::screenAt(QQmlListProperty<QuickshellScreenInfo>* prop, qsizetype i) { QuickshellGlobal::screenAt(QQmlListProperty<QuickshellScreenInfo>* /*unused*/, qsizetype i) {
return static_cast<QuickshellGlobal*>(prop->object)->mScreens.at(i); // NOLINT return QuickshellTracked::instance()->screens.at(i);
} }
QQmlListProperty<QuickshellScreenInfo> QuickshellGlobal::screens() { QQmlListProperty<QuickshellScreenInfo> QuickshellGlobal::screens() {
@ -128,36 +171,6 @@ void QuickshellGlobal::setWatchFiles(bool watchFiles) { // NOLINT
QuickshellSettings::instance()->setWatchFiles(watchFiles); QuickshellSettings::instance()->setWatchFiles(watchFiles);
} }
void QuickshellGlobal::updateScreens() {
auto screens = QGuiApplication::screens();
auto newScreens = QList<QuickshellScreenInfo*>();
for (auto* newScreen: screens) {
for (auto i = 0; i < this->mScreens.length(); i++) {
auto* oldScreen = this->mScreens[i];
if (newScreen == oldScreen->screen) {
newScreens.push_back(oldScreen);
this->mScreens.remove(i);
goto next;
}
}
{
auto* si = new QuickshellScreenInfo(this, newScreen);
QQmlEngine::setObjectOwnership(si, QQmlEngine::CppOwnership);
newScreens.push_back(si);
}
next:;
}
for (auto* oldScreen: this->mScreens) {
oldScreen->deleteLater();
}
this->mScreens = newScreens;
emit this->screensChanged();
}
QVariant QuickshellGlobal::env(const QString& variable) { // NOLINT QVariant QuickshellGlobal::env(const QString& variable) { // NOLINT
auto vstr = variable.toStdString(); auto vstr = variable.toStdString();
if (!qEnvironmentVariableIsSet(vstr.data())) return QVariant::fromValue(nullptr); if (!qEnvironmentVariableIsSet(vstr.data())) return QVariant::fromValue(nullptr);

View file

@ -53,6 +53,23 @@ private:
bool mWatchFiles = true; bool mWatchFiles = true;
}; };
class QuickshellTracked: public QObject {
Q_OBJECT;
public:
QuickshellTracked();
QVector<QuickshellScreenInfo*> screens;
static QuickshellTracked* instance();
private slots:
void updateScreens();
signals:
void screensChanged();
};
class QuickshellGlobal: public QObject { class QuickshellGlobal: public QObject {
Q_OBJECT; Q_OBJECT;
// clang-format off // clang-format off
@ -124,12 +141,7 @@ signals:
void workingDirectoryChanged(); void workingDirectoryChanged();
void watchFilesChanged(); void watchFilesChanged();
public slots:
void updateScreens();
private: private:
static qsizetype screensCount(QQmlListProperty<QuickshellScreenInfo>* prop); static qsizetype screensCount(QQmlListProperty<QuickshellScreenInfo>* prop);
static QuickshellScreenInfo* screenAt(QQmlListProperty<QuickshellScreenInfo>* prop, qsizetype i); static QuickshellScreenInfo* screenAt(QQmlListProperty<QuickshellScreenInfo>* prop, qsizetype i);
QVector<QuickshellScreenInfo*> mScreens;
}; };

View file

@ -21,12 +21,7 @@
RootWrapper::RootWrapper(QString rootPath) RootWrapper::RootWrapper(QString rootPath)
: QObject(nullptr) : QObject(nullptr)
, rootPath(std::move(rootPath)) , rootPath(std::move(rootPath))
, engine(this)
, originalWorkingDirectory(QDir::current().absolutePath()) { , originalWorkingDirectory(QDir::current().absolutePath()) {
auto* app = QCoreApplication::instance();
QObject::connect(&this->engine, &QQmlEngine::quit, app, &QCoreApplication::quit);
QObject::connect(&this->engine, &QQmlEngine::exit, app, &QCoreApplication::exit);
// clang-format off // clang-format off
QObject::connect(QuickshellSettings::instance(), &QuickshellSettings::watchFilesChanged, this, &RootWrapper::onWatchFilesChanged); QObject::connect(QuickshellSettings::instance(), &QuickshellSettings::watchFilesChanged, this, &RootWrapper::onWatchFilesChanged);
// clang-format on // clang-format on
@ -45,16 +40,22 @@ RootWrapper::~RootWrapper() {
} }
void RootWrapper::reloadGraph(bool hard) { void RootWrapper::reloadGraph(bool hard) {
auto* oldEngine = this->engine;
this->engine = new QQmlEngine(this);
auto* app = QCoreApplication::instance();
QObject::connect(this->engine, &QQmlEngine::quit, app, &QCoreApplication::quit);
QObject::connect(this->engine, &QQmlEngine::exit, app, &QCoreApplication::exit);
if (this->root != nullptr) { if (this->root != nullptr) {
QuickshellSettings::reset(); QuickshellSettings::reset();
this->engine.clearComponentCache();
} }
QDir::setCurrent(this->originalWorkingDirectory); QDir::setCurrent(this->originalWorkingDirectory);
auto component = QQmlComponent(&this->engine, QUrl::fromLocalFile(this->rootPath)); auto component = QQmlComponent(this->engine, QUrl::fromLocalFile(this->rootPath));
auto* obj = component.beginCreate(this->engine.rootContext()); auto* obj = component.beginCreate(this->engine->rootContext());
if (obj == nullptr) { if (obj == nullptr) {
qWarning() << component.errorString().toStdString().c_str(); qWarning() << component.errorString().toStdString().c_str();
@ -90,6 +91,8 @@ void RootWrapper::reloadGraph(bool hard) {
QuickshellPlugin::runOnReload(); QuickshellPlugin::runOnReload();
} }
delete oldEngine;
this->onWatchFilesChanged(); this->onWatchFilesChanged();
} }

View file

@ -25,7 +25,7 @@ private slots:
private: private:
QString rootPath; QString rootPath;
QQmlEngine engine; QQmlEngine* engine = nullptr;
ShellRoot* root = nullptr; ShellRoot* root = nullptr;
FiletreeWatcher* configWatcher = nullptr; FiletreeWatcher* configWatcher = nullptr;
QString originalWorkingDirectory; QString originalWorkingDirectory;

View file

@ -1,5 +1,3 @@
#include "core/main.hpp" #include "core/main.hpp"
int main(int argc, char** argv) { int main(int argc, char** argv) { return qs_main(argc, argv); }
return qs_main(argc, argv);
}

View file

@ -78,7 +78,8 @@ void WlSessionLock::updateSurfaces(WlSessionLock* old) {
auto* instance = qobject_cast<WlSessionLockSurface*>(instanceObj); auto* instance = qobject_cast<WlSessionLockSurface*>(instanceObj);
if (instance == nullptr) { if (instance == nullptr) {
qWarning() << "WlSessionLock.surface does not create a WlSessionLockSurface. Aborting lock."; qWarning(
) << "WlSessionLock.surface does not create a WlSessionLockSurface. Aborting lock.";
if (instanceObj != nullptr) instanceObj->deleteLater(); if (instanceObj != nullptr) instanceObj->deleteLater();
this->unlock(); this->unlock();
return; return;

View file

@ -26,7 +26,7 @@ enum Enum {
}; };
Q_ENUM_NS(Enum); Q_ENUM_NS(Enum);
} // namespace Layer } // namespace WlrLayer
///! WlrLayershell keyboard focus mode ///! WlrLayershell keyboard focus mode
namespace WlrKeyboardFocus { // NOLINT namespace WlrKeyboardFocus { // NOLINT
@ -47,7 +47,7 @@ enum Enum {
}; };
Q_ENUM_NS(Enum); Q_ENUM_NS(Enum);
} // namespace KeyboardFocus } // namespace WlrKeyboardFocus
class QSWaylandLayerSurface; class QSWaylandLayerSurface;