feat(hot reloading): Add first version of hot reloading (#674)

Version for: https://github.com/status-im/status-desktop/issues/5690
This commit is contained in:
Boris Melnik 2022-08-04 13:54:13 +03:00 committed by GitHub
parent 9738890d9d
commit 40992f6718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 29 deletions

View File

@ -1,6 +0,0 @@
#include "handler.h"
Handler::Handler(QObject *parent) : QObject(parent)
{
}

View File

@ -1,16 +0,0 @@
#ifndef HANDLER_H
#define HANDLER_H
#include <QObject>
class Handler : public QObject
{
Q_OBJECT
public:
explicit Handler(QObject *parent = nullptr);
signals:
void restartQml();
};
#endif // HANDLER_H

View File

@ -3,15 +3,20 @@
#include <QQmlContext> #include <QQmlContext>
#include <QWindow> #include <QWindow>
#include <QDebug> #include <QDebug>
#include <QDirIterator>
#include "statuswindow.h" #include "statuswindow.h"
#include "spellchecker.h" #include "spellchecker.h"
SandboxApp::SandboxApp(int &argc, char **argv) SandboxApp::SandboxApp(int &argc, char **argv)
: QGuiApplication(argc, argv), : QGuiApplication(argc, argv)
m_handler(new Handler(this))
{ {
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() void SandboxApp::startEngine()
@ -21,13 +26,18 @@ void SandboxApp::startEngine()
#ifdef QT_DEBUG #ifdef QT_DEBUG
const QUrl url = QUrl::fromLocalFile(SRC_DIR + QStringLiteral("/main.qml")); 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 #else
const QUrl url(QStringLiteral("qrc:/main.qml")); const QUrl url(QStringLiteral("qrc:/main.qml"));
#endif #endif
m_engine.rootContext()->setContextProperty("app", m_handler);
#ifdef QT_DEBUG #ifdef QT_DEBUG
m_engine.addImportPath(SRC_DIR + QStringLiteral("/../src")); m_engine.addImportPath(SRC_DIR + QStringLiteral("/../src"));
#else #else
@ -44,7 +54,7 @@ void SandboxApp::startEngine()
void SandboxApp::restartEngine() void SandboxApp::restartEngine()
{ {
const QUrl url(applicationDirPath() + "/../main.qml"); const QUrl url = QUrl::fromLocalFile(SRC_DIR + QStringLiteral("/main.qml"));
QWindow *rootWindow = qobject_cast<QWindow*>(m_engine.rootObjects().at(0)); QWindow *rootWindow = qobject_cast<QWindow*>(m_engine.rootObjects().at(0));
if (rootWindow) { if (rootWindow) {
rootWindow->close(); rootWindow->close();

View File

@ -4,6 +4,10 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#ifdef QT_DEBUG
#include <QFileSystemWatcher>
#endif
#include "handler.h" #include "handler.h"
class SandboxApp : public QGuiApplication class SandboxApp : public QGuiApplication
@ -19,6 +23,10 @@ public slots:
private: private:
QQmlApplicationEngine m_engine; QQmlApplicationEngine m_engine;
Handler *m_handler; Handler *m_handler;
#ifdef QT_DEBUG
QFileSystemWatcher m_watcher;
#endif
}; };
#endif // SANDBOXAPP_H #endif // SANDBOXAPP_H