/*************************************************************************************************** Copyright (C) 2024 The Qt Company Ltd. SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only ***************************************************************************************************/ #pragma once #ifndef QT_QUICK_LIB struct IQQmlEngine {}; #else #include "qdotnetinterface.h" #include "qdotnetadapter.h" #ifdef __GNUC__ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wconversion" #endif #include #include #include #ifdef __GNUC__ # pragma GCC diagnostic pop #endif #include struct IQQmlEngine : public QDotNetNativeInterface { static inline const QString &AssemblyQualifiedName = QStringLiteral("Qt.Quick.IQQmlEngine, Qt.DotNet.Adapter"); bool exited = false; int exitCode = -1; IQQmlEngine() : QDotNetNativeInterface(AssemblyQualifiedName, QDotNetAdapter::instance().qmlEngine(), false) { init(); } void init() { const auto *engine = QDotNetAdapter::instance().qmlEngine(); if (engine == nullptr) return; QObject::connect(engine, &QQmlEngine::exit, [this](int code) { exitCode = code; exited = true; }); QObject::connect(engine, &QQmlEngine::quit, [this]() { exitCode = 0; exited = true; }); setCallback("LoadFromModule", [this](void *data, const QString &uri, const QString &typeName) { auto *qmlEngine = reinterpret_cast(data); if (!qmlEngine) return; QMetaObject::invokeMethod(qmlEngine, "loadFromModule", Qt::BlockingQueuedConnection, Q_ARG(QAnyStringView, uri), Q_ARG(QAnyStringView, typeName)); }); setCallback("WaitForExit", [this](void *data, int timeout) { if (timeout == 0) return false; QElapsedTimer timer; timer.start(); while (!timer.hasExpired(timeout) && !IQQmlEngine::exited) QThread::usleep(100); return IQQmlEngine::exited; }); setCallback("ProcessEvents", [this](void *data) { QCoreApplication::processEvents(); }); } static void staticInit(QDotNetInterface *sta) { static IQQmlEngine qmlEngine; sta->setCallback("QQmlEngine_Get", [](void *) { return IQQmlEngine(qmlEngine); }); } }; #endif