diff options
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_wasm.cpp | 42 | ||||
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_wasm_p.h | 5 | ||||
-rw-r--r-- | src/plugins/platforms/wasm/qtloader.js | 2 |
3 files changed, 47 insertions, 2 deletions
diff --git a/src/corelib/kernel/qeventdispatcher_wasm.cpp b/src/corelib/kernel/qeventdispatcher_wasm.cpp index cc24b5ff7c..aa44158551 100644 --- a/src/corelib/kernel/qeventdispatcher_wasm.cpp +++ b/src/corelib/kernel/qeventdispatcher_wasm.cpp @@ -217,6 +217,9 @@ QEventDispatcherWasm::QEventDispatcherWasm() #if QT_CONFIG(thread) g_mainThread = pthread_self(); #endif + // Call the "onLoaded" JavaScript callback, unless startup tasks + // have been registered which should complete first. + checkCallQtLoaded(); } else { #if QT_CONFIG(thread) std::lock_guard<std::mutex> lock(g_staticDataMutex); @@ -895,6 +898,45 @@ void QEventDispatcherWasm::socketSelect(int timeout, int socket, bool waitForRea } namespace { + int g_startupTasks = 0; +} + +// The following functions manages sending the "qtLoaded" event/callback +// from qtloader.js on startup, once Qt initialization has been completed +// and the application is ready to display the first frame. This can be +// either as soon as the event loop is running, or later, if additional +// startup tasks (e.g. local font loading) have been registered. + +void QEventDispatcherWasm::registerStartupTask() +{ + ++g_startupTasks; +} + +void QEventDispatcherWasm::completeStarupTask() +{ + --g_startupTasks; + checkCallQtLoaded(); +} + +void QEventDispatcherWasm::checkCallQtLoaded() +{ + if (g_startupTasks > 0) + return; + + static bool qtLoadedCalled = false; + if (qtLoadedCalled) + return; + qtLoadedCalled = true; + + QTimer::singleShot(0, [](){ + emscripten::val qt = emscripten::val::module_property("qt"); + if (qt.isUndefined()) + return; + qt.call<void>("onLoaded"); + }); +} + +namespace { void trampoline(void *context) { auto async_fn = [](void *context){ diff --git a/src/corelib/kernel/qeventdispatcher_wasm_p.h b/src/corelib/kernel/qeventdispatcher_wasm_p.h index 564bbffa47..afffc8a813 100644 --- a/src/corelib/kernel/qeventdispatcher_wasm_p.h +++ b/src/corelib/kernel/qeventdispatcher_wasm_p.h @@ -56,6 +56,11 @@ public: static void runOnMainThreadAsync(std::function<void(void)> fn); static void socketSelect(int timeout, int socket, bool waitForRead, bool waitForWrite, bool *selectForRead, bool *selectForWrite, bool *socketDisconnect); + + static void registerStartupTask(); + static void completeStarupTask(); + static void checkCallQtLoaded(); + protected: virtual bool processPostedEvents(); diff --git a/src/plugins/platforms/wasm/qtloader.js b/src/plugins/platforms/wasm/qtloader.js index 7b9288cd30..1789ad70e3 100644 --- a/src/plugins/platforms/wasm/qtloader.js +++ b/src/plugins/platforms/wasm/qtloader.js @@ -160,8 +160,6 @@ async function qtLoad(config) config.preRun = []; config.preRun.push(qtPreRun); - config.onRuntimeInitialized = () => config.qt.onLoaded?.(); - const originalLocateFile = config.locateFile; config.locateFile = filename => { |