From f5c75562b2ce0ba7cd7e941bc9fe6e4527023a54 Mon Sep 17 00:00:00 2001 From: Boris Melnik Date: Thu, 4 Aug 2022 13:54:13 +0300 Subject: [PATCH] feat(hot reloading): Add first version of hot reloading (#674) Version for: https://github.com/status-im/status-desktop/issues/5690 --- ui/StatusQ/sandbox/handler.cpp | 6 ------ ui/StatusQ/sandbox/handler.h | 16 ---------------- ui/StatusQ/sandbox/sandboxapp.cpp | 24 +++++++++++++++++------- ui/StatusQ/sandbox/sandboxapp.h | 8 ++++++++ 4 files changed, 25 insertions(+), 29 deletions(-) delete mode 100644 ui/StatusQ/sandbox/handler.cpp delete mode 100644 ui/StatusQ/sandbox/handler.h diff --git a/ui/StatusQ/sandbox/handler.cpp b/ui/StatusQ/sandbox/handler.cpp deleted file mode 100644 index 60401c69bd..0000000000 --- a/ui/StatusQ/sandbox/handler.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "handler.h" - -Handler::Handler(QObject *parent) : QObject(parent) -{ - -} diff --git a/ui/StatusQ/sandbox/handler.h b/ui/StatusQ/sandbox/handler.h deleted file mode 100644 index aacbb27a8c..0000000000 --- a/ui/StatusQ/sandbox/handler.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef HANDLER_H -#define HANDLER_H - -#include - -class Handler : public QObject -{ - Q_OBJECT -public: - explicit Handler(QObject *parent = nullptr); - -signals: - void restartQml(); -}; - -#endif // HANDLER_H diff --git a/ui/StatusQ/sandbox/sandboxapp.cpp b/ui/StatusQ/sandbox/sandboxapp.cpp index 7278fa1a60..493ea56171 100644 --- a/ui/StatusQ/sandbox/sandboxapp.cpp +++ b/ui/StatusQ/sandbox/sandboxapp.cpp @@ -3,15 +3,20 @@ #include #include #include +#include #include "statuswindow.h" #include "spellchecker.h" SandboxApp::SandboxApp(int &argc, char **argv) - : QGuiApplication(argc, argv), - m_handler(new Handler(this)) + : QGuiApplication(argc, argv) { - connect(m_handler, &Handler::restartQml, this, &SandboxApp::restartEngine, Qt::QueuedConnection); +#ifdef QT_DEBUG + connect(&m_watcher, &QFileSystemWatcher::directoryChanged, [this](const QString&) { + restartEngine(); + }); + +#endif } void SandboxApp::startEngine() @@ -21,13 +26,18 @@ void SandboxApp::startEngine() #ifdef QT_DEBUG const QUrl url = QUrl::fromLocalFile(SRC_DIR + QStringLiteral("/main.qml")); + m_watcher.addPath(applicationDirPath() + "/../"); + QDirIterator it(applicationDirPath() + "/../", QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); + while (it.hasNext()) { + if (!it.filePath().isEmpty()) { + m_watcher.addPath(it.filePath()); + } + it.next(); + } #else const QUrl url(QStringLiteral("qrc:/main.qml")); #endif - m_engine.rootContext()->setContextProperty("app", m_handler); - - #ifdef QT_DEBUG m_engine.addImportPath(SRC_DIR + QStringLiteral("/../src")); #else @@ -44,7 +54,7 @@ void SandboxApp::startEngine() void SandboxApp::restartEngine() { - const QUrl url(applicationDirPath() + "/../main.qml"); + const QUrl url = QUrl::fromLocalFile(SRC_DIR + QStringLiteral("/main.qml")); QWindow *rootWindow = qobject_cast(m_engine.rootObjects().at(0)); if (rootWindow) { rootWindow->close(); diff --git a/ui/StatusQ/sandbox/sandboxapp.h b/ui/StatusQ/sandbox/sandboxapp.h index 8589baab72..a74c82a292 100644 --- a/ui/StatusQ/sandbox/sandboxapp.h +++ b/ui/StatusQ/sandbox/sandboxapp.h @@ -4,6 +4,10 @@ #include #include +#ifdef QT_DEBUG +#include +#endif + #include "handler.h" class SandboxApp : public QGuiApplication @@ -19,6 +23,10 @@ public slots: private: QQmlApplicationEngine m_engine; Handler *m_handler; + +#ifdef QT_DEBUG + QFileSystemWatcher m_watcher; +#endif }; #endif // SANDBOXAPP_H