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
SANDBOX_SRC_DIR="${CMAKE_CURRENT_LIST_DIR}"
STATUSQ_MODULE_PATH="${STATUSQ_MODULE_PATH}"
STATUSQ_MODULE_IMPORT_PATH="${STATUSQ_MODULE_IMPORT_PATH}"
)

View File

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

View File

@ -9,43 +9,54 @@ SandboxApp::SandboxApp(int &argc, char **argv)
: QGuiApplication(argc, argv)
{
#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();
});
#endif
}
void SandboxApp::startEngine()
void SandboxApp::watchDirectoryChanges(const QString& path)
{
#ifdef QT_DEBUG
m_watcher.addPath(applicationDirPath() + "/../");
QDirIterator it(applicationDirPath() + "/../", QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (it.hasNext()) {
qDebug() << "Iterating to watch over:" << path;
const auto dirFilters = QDir::Files | QDir::NoDotAndDotDot;
const auto fileNameFilters = QStringList { "*.qml" };
for (QDirIterator it(path, fileNameFilters, dirFilters, QDirIterator::Subdirectories); it.hasNext(); it.next()) {
if (!it.filePath().isEmpty()) {
m_watcher.addPath(it.filePath());
}
it.next();
}
}
void SandboxApp::startEngine()
{
#ifdef QT_DEBUG
watchDirectoryChanges(SANDBOX_SRC_DIR);
watchDirectoryChanges(STATUSQ_MODULE_PATH);
#endif
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);
restartEngine();
}
void SandboxApp::restartEngine()
{
QWindow *rootWindow = qobject_cast<QWindow*>(m_engine.rootObjects().at(0));
if (rootWindow) {
rootWindow->close();
rootWindow->deleteLater();
}
m_engine.clearComponentCache();
m_engine.load(m_url);
const bool firstRun { !m_engine };
if (!firstRun)
qDebug() << "Restarting QML engine";
m_engine = std::make_unique<QQmlApplicationEngine>();
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();
private:
QQmlApplicationEngine m_engine;
std::unique_ptr<QQmlApplicationEngine> m_engine;
#ifdef QT_DEBUG
QFileSystemWatcher m_watcher;
@ -32,6 +32,8 @@ private:
QStringLiteral("qrc:/main.qml")
#endif
};
void watchDirectoryChanges(const QString& path);
};
#endif // SANDBOXAPP_H

View File

@ -2,10 +2,6 @@ cmake_minimum_required(VERSION 3.5)
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()
set(CMAKE_AUTOMOC ON)