From 0fe900dfd23c758ff96b8ba50fdd0ae2e989c5bd Mon Sep 17 00:00:00 2001 From: falkTX Date: Thu, 9 May 2024 21:28:02 +0200 Subject: [PATCH] allow web-ui late init Signed-off-by: falkTX --- dgl/Web.hpp | 6 ++++-- dgl/src/Web.cpp | 34 ++++++++++++++++++++++++++-------- distrho/extra/WebViewImpl.hpp | 7 +++++++ distrho/src/DistrhoUI.cpp | 8 ++++++-- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/dgl/Web.hpp b/dgl/Web.hpp index 67cb3782..2a4db1ee 100644 --- a/dgl/Web.hpp +++ b/dgl/Web.hpp @@ -43,7 +43,7 @@ public: /** Constructor for a WebViewWidget. */ - explicit WebViewWidget(Window& windowToMapTo); + explicit WebViewWidget(Window& windowToMapTo, bool initLater = false); /** Destructor. @@ -55,11 +55,13 @@ public: void reload(); protected: + void init(const char* initialJS); + virtual void onMessage(char* message); void onResize(const ResizeEvent& ev) override; private: - const DISTRHO_NAMESPACE::WebViewHandle webview; + DISTRHO_NAMESPACE::WebViewHandle webview; void idleCallback() override; void onDisplay() override {} diff --git a/dgl/src/Web.cpp b/dgl/src/Web.cpp index 8deef02d..822990fa 100644 --- a/dgl/src/Web.cpp +++ b/dgl/src/Web.cpp @@ -26,15 +26,15 @@ START_NAMESPACE_DGL -// ----------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------- -WebViewWidget::WebViewWidget(Window& windowToMapTo) +WebViewWidget::WebViewWidget(Window& windowToMapTo, bool initLater) : TopLevelWidget(windowToMapTo), - webview(webViewCreate(windowToMapTo.getNativeWindowHandle(), - windowToMapTo.getWidth(), - windowToMapTo.getHeight(), - windowToMapTo.getScaleFactor(), - WebViewOptions(_on_msg, this))) + webview(initLater ? nullptr : webViewCreate(windowToMapTo.getNativeWindowHandle(), + windowToMapTo.getWidth(), + windowToMapTo.getHeight(), + windowToMapTo.getScaleFactor(), + WebViewOptions(_on_msg, this))) { #if !(defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS)) if (webview != nullptr) @@ -53,6 +53,24 @@ WebViewWidget::~WebViewWidget() } } +void WebViewWidget::init(const char* const initialJS) +{ + DISTRHO_SAFE_ASSERT_RETURN(webview == nullptr,); + + WebViewOptions options(_on_msg, this); + options.initialJS = initialJS; + webview = webViewCreate(getWindow().getNativeWindowHandle(), getWidth(), getHeight(), getScaleFactor(), options); + + // FIXME implement initialJS + if (webview != nullptr) + webViewEvaluateJS(webview, initialJS); + + #if !(defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS)) + if (webview != nullptr) + addIdleCallback(this, 1000 / 60); + #endif +} + void WebViewWidget::evaluateJS(const char* const js) { if (webview != nullptr) @@ -87,7 +105,7 @@ void WebViewWidget::idleCallback() webViewIdle(webview); } -// ----------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------- static void notImplemented(const char* const name) { diff --git a/distrho/extra/WebViewImpl.hpp b/distrho/extra/WebViewImpl.hpp index 38c7e802..142c6a8f 100644 --- a/distrho/extra/WebViewImpl.hpp +++ b/distrho/extra/WebViewImpl.hpp @@ -45,6 +45,11 @@ struct WebViewOptions { PositionOffset() : x(0), y(0) {} } offset; + /** + Set some JavaScript to evalute on every new page load. + */ + const char* initialJS; + /** Message callback triggered from JavaScript code inside the WebView. */ @@ -54,12 +59,14 @@ struct WebViewOptions { /** Constructor for default values */ WebViewOptions() : offset(), + initialJS(nullptr), callback(nullptr), callbackPtr(nullptr) {} /** Constructor providing a callback */ WebViewOptions(const WebViewMessageCallback cb, void* const ptr) : offset(), + initialJS(nullptr), callback(cb), callbackPtr(ptr) {} }; diff --git a/distrho/src/DistrhoUI.cpp b/distrho/src/DistrhoUI.cpp index 2bb5200b..9edddc40 100644 --- a/distrho/src/DistrhoUI.cpp +++ b/distrho/src/DistrhoUI.cpp @@ -246,7 +246,11 @@ UI::UI(const uint width, const uint height, const bool automaticallyScaleAndSetA #else false #endif - )), + ) + #if DISTRHO_UI_WEB_VIEW + , true + #endif + ), uiData(UI::PrivateData::s_nextPrivateData) { #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI @@ -269,7 +273,7 @@ UI::UI(const uint width, const uint height, const bool automaticallyScaleAndSetA #endif #if DISTRHO_UI_WEB_VIEW - evaluateJS( + init( "editParameter=function(index,started){window.webkit.messageHandlers.external.postMessage('editparam '+index+' '+(started ? 1 : 0))};" "setParameterValue=function(index,value){window.webkit.messageHandlers.external.postMessage('setparam '+index+' '+value)};" #if DISTRHO_PLUGIN_WANT_STATE