fix(Sandbox): Fixed hot reloading. Fixed MacOS arm64 builds. (#10562)

This commit is contained in:
Igor Sirotin 2023-05-05 19:05:44 +03:00 committed by GitHub
parent b9a2e62602
commit a3ec245b44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 42 deletions

View File

@ -21,6 +21,7 @@ add_executable(${PROJECT_NAME}
target_compile_definitions(${PROJECT_NAME} PRIVATE target_compile_definitions(${PROJECT_NAME} PRIVATE
SANDBOX_SRC_DIR="${CMAKE_CURRENT_LIST_DIR}" SANDBOX_SRC_DIR="${CMAKE_CURRENT_LIST_DIR}"
STATUSQ_MODULE_PATH="${STATUSQ_MODULE_PATH}"
STATUSQ_MODULE_IMPORT_PATH="${STATUSQ_MODULE_IMPORT_PATH}" STATUSQ_MODULE_IMPORT_PATH="${STATUSQ_MODULE_IMPORT_PATH}"
) )

View File

@ -1,11 +1,11 @@
import QtQuick 2.14 import QtQuick 2.15
import QtQuick.Window 2.14 import QtQuick.Window 2.15
import QtQuick.Controls 2.14 import QtQuick.Controls 2.15
import QtGraphicalEffects 1.13 import QtGraphicalEffects 1.15
import QtQuick.Layouts 1.14 import QtQuick.Layouts 1.15
import Qt.labs.settings 1.0 import Qt.labs.settings 1.0
import QtQml.Models 2.14 import QtQml.Models 2.15
import QtMultimedia 5.14 import QtMultimedia 5.15
import StatusQ 0.1 // https://github.com/status-im/status-desktop/issues/10218 import StatusQ 0.1 // https://github.com/status-im/status-desktop/issues/10218
@ -22,10 +22,10 @@ import "demoapp/data" 1.0
StatusWindow { StatusWindow {
id: rootWindow id: rootWindow
width: Qt.platform.os == "ios" || Qt.platform.os == "android" ? Screen.width
: 1224 width: 1224
height: Qt.platform.os == "ios" || Qt.platform.os == "android" ? Screen.height height: 840
:840
visible: true visible: true
title: qsTr("StatusQ Documentation App") title: qsTr("StatusQ Documentation App")
@ -37,8 +37,6 @@ StatusWindow {
property real factor: 1.0 property real factor: 1.0
Component.onCompleted: rootWindow.updatePosition()
QtObject { QtObject {
id: appSectionType id: appSectionType
readonly property int chat: 0 readonly property int chat: 0
@ -592,6 +590,12 @@ StatusWindow {
Settings { Settings {
id: storeSettings id: storeSettings
property alias x: rootWindow.x
property alias y: rootWindow.y
property alias width: rootWindow.width
property alias height: rootWindow.height
property string selected: "" property string selected: ""
property string selectedExample: "" property string selectedExample: ""
property bool lightTheme: true property bool lightTheme: true

View File

@ -9,43 +9,54 @@ SandboxApp::SandboxApp(int &argc, char **argv)
: QGuiApplication(argc, argv) : QGuiApplication(argc, argv)
{ {
#ifdef QT_DEBUG #ifdef QT_DEBUG
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, [this](const QString&) { connect(&m_watcher, &QFileSystemWatcher::fileChanged, this, [this](const QString& path) {
qDebug().noquote() << QString("File change detected in '%1'").arg(path);
restartEngine(); restartEngine();
}); });
#endif #endif
} }
void SandboxApp::startEngine() void SandboxApp::watchDirectoryChanges(const QString& path)
{ {
#ifdef QT_DEBUG qDebug() << "Iterating to watch over:" << path;
m_watcher.addPath(applicationDirPath() + "/../");
QDirIterator it(applicationDirPath() + "/../", QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); const auto dirFilters = QDir::Files | QDir::NoDotAndDotDot;
while (it.hasNext()) { const auto fileNameFilters = QStringList { "*.qml" };
for (QDirIterator it(path, fileNameFilters, dirFilters, QDirIterator::Subdirectories); it.hasNext(); it.next()) {
if (!it.filePath().isEmpty()) { if (!it.filePath().isEmpty()) {
m_watcher.addPath(it.filePath()); m_watcher.addPath(it.filePath());
} }
it.next();
} }
}
void SandboxApp::startEngine()
{
#ifdef QT_DEBUG
watchDirectoryChanges(SANDBOX_SRC_DIR);
watchDirectoryChanges(STATUSQ_MODULE_PATH);
#endif #endif
restartEngine();
m_engine.addImportPath(STATUSQ_MODULE_IMPORT_PATH);
qDebug() << m_engine.importPathList();
QObject::connect(&m_engine, &QQmlApplicationEngine::objectCreated,
this, [this](QObject *obj, const QUrl &objUrl) {
if (!obj && m_url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
m_engine.load(m_url);
} }
void SandboxApp::restartEngine() void SandboxApp::restartEngine()
{ {
QWindow *rootWindow = qobject_cast<QWindow*>(m_engine.rootObjects().at(0)); const bool firstRun { !m_engine };
if (rootWindow) {
rootWindow->close(); if (!firstRun)
rootWindow->deleteLater(); qDebug() << "Restarting QML engine";
}
m_engine.clearComponentCache(); m_engine = std::make_unique<QQmlApplicationEngine>();
m_engine.load(m_url); m_engine->addImportPath(STATUSQ_MODULE_IMPORT_PATH);
if (firstRun)
qDebug() << "QQmlEngine import paths: " << m_engine->importPathList();
QObject::connect(m_engine.get(), &QQmlApplicationEngine::objectCreated,
this, [this](QObject *obj, const QUrl &objUrl) {
if (!obj && m_url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
m_engine->load(m_url);
} }

View File

@ -19,7 +19,7 @@ public slots:
void restartEngine(); void restartEngine();
private: private:
QQmlApplicationEngine m_engine; std::unique_ptr<QQmlApplicationEngine> m_engine;
#ifdef QT_DEBUG #ifdef QT_DEBUG
QFileSystemWatcher m_watcher; QFileSystemWatcher m_watcher;
@ -32,6 +32,8 @@ private:
QStringLiteral("qrc:/main.qml") QStringLiteral("qrc:/main.qml")
#endif #endif
}; };
void watchDirectoryChanges(const QString& path);
}; };
#endif // SANDBOXAPP_H #endif // SANDBOXAPP_H

View File

@ -2,10 +2,6 @@ cmake_minimum_required(VERSION 3.5)
project(TestStatusQ LANGUAGES CXX) project(TestStatusQ LANGUAGES CXX)
# The current StatusQ builds with Qt 5.14 which doesn't support the apple silicon.
# Therefore force the intel architecture for MacOS platforms.
set(CMAKE_OSX_ARCHITECTURES "x86_64")
enable_testing() enable_testing()
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)