From df3239321591f79caa6a7a83bb45449cede59e20 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sat, 1 May 2021 07:20:36 +1000 Subject: [PATCH] [windows] Make initial loading a sync operation --- v2/internal/ffenestri/ffenestri_windows.cpp | 39 +++++++++++++------ v2/internal/ffenestri/ffenestri_windows.h | 7 ++++ v2/internal/ffenestri/wv2ComHandler_windows.h | 7 ++++ 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/v2/internal/ffenestri/ffenestri_windows.cpp b/v2/internal/ffenestri/ffenestri_windows.cpp index 6bb4c5fab..1b59de0ac 100644 --- a/v2/internal/ffenestri/ffenestri_windows.cpp +++ b/v2/internal/ffenestri/ffenestri_windows.cpp @@ -161,14 +161,14 @@ void execJS(struct Application* app, const char *script) { void loadAssets(struct Application* app) { // patch window.external.invoke - execJS(app, "window.external={invoke:s=>window.chrome.webview.postMessage(s)}"); + std::string initialCode = std::string("window.external={invoke:s=>window.chrome.webview.postMessage(s)};"); // Load bindings - execJS(app, app->bindings); + initialCode += std::string(app->bindings); delete[] app->bindings; // Load runtime - execJS(app, (const char*)&runtime); + initialCode += std::string((const char*)&runtime); int index = 1; while(1) { @@ -180,19 +180,33 @@ void loadAssets(struct Application* app) { break; } - execJS(app, (const char*)asset); + initialCode += std::string((const char*)asset); index++; }; - // Disable context menu if not in debug mode - if( debug != 1 ) { - execJS(app, "wails._.DisableDefaultContextMenu();"); - } + // Disable context menu if not in debug mode + if( debug != 1 ) { + initialCode += std::string("wails._.DisableDefaultContextMenu();"); + } - // Show app if we need to - if( app->startHidden == false ) { - Show(app); - } + initialCode += std::string("window.external.invoke('completed');"); + + // Keep a copy of the code + app->initialCode = new char[initialCode.length()+1]; + memcpy(app->initialCode, initialCode.c_str(), initialCode.length()+1); + + execJS(app, app->initialCode); + + // Show app if we need to + if( app->startHidden == false ) { + Show(app); + } +} + +// This is called when all our assets are loaded into the DOM +void completed(struct Application* app) { + delete[] app->initialCode; + app->initialCode = nullptr; } @@ -327,6 +341,7 @@ void Run(struct Application* app, int argc, char **argv) { (*f)(); delete(f); } else if (msg.message == WM_QUIT) { + messageFromWindowCallback("Q"); return; } } diff --git a/v2/internal/ffenestri/ffenestri_windows.h b/v2/internal/ffenestri/ffenestri_windows.h index c42ff9dc3..0f74ee467 100644 --- a/v2/internal/ffenestri/ffenestri_windows.h +++ b/v2/internal/ffenestri/ffenestri_windows.h @@ -42,7 +42,9 @@ struct Application{ LONG maxHeight; int frame; + // placeholders char* bindings; + char* initialCode; }; #define ON_MAIN_THREAD(code) dispatch( [=]{ code; } ) @@ -54,8 +56,13 @@ typedef std::function comHandlerCallback; void center(struct Application*); void setTitle(struct Application* app, const char *title); char* LPWSTRToCstr(LPWSTR input); + +// called when the DOM is ready void loadAssets(struct Application* app); +// called when the application assets have been loaded into the DOM +void completed(struct Application* app); + // Callback extern "C" { void messageFromWindowCallback(const char *); diff --git a/v2/internal/ffenestri/wv2ComHandler_windows.h b/v2/internal/ffenestri/wv2ComHandler_windows.h index 368d92b86..bb3beae6f 100644 --- a/v2/internal/ffenestri/wv2ComHandler_windows.h +++ b/v2/internal/ffenestri/wv2ComHandler_windows.h @@ -60,6 +60,13 @@ class wv2ComHandler return S_OK; } const char *m = LPWSTRToCstr(message); + + // check for internal messages + if (strcmp(m, "completed") == 0) { + completed(app); + return S_OK; + } + switch(m[0]) { // Standard message for backend