Skip to content

Commit

Permalink
allow web-ui late init
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed May 9, 2024
1 parent 47b1d3f commit 0fe900d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
6 changes: 4 additions & 2 deletions dgl/Web.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class WebViewWidget : public TopLevelWidget,
/**
Constructor for a WebViewWidget.
*/
explicit WebViewWidget(Window& windowToMapTo);
explicit WebViewWidget(Window& windowToMapTo, bool initLater = false);

/**
Destructor.
Expand All @@ -55,11 +55,13 @@ class WebViewWidget : public TopLevelWidget,
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 {}

Expand Down
34 changes: 26 additions & 8 deletions dgl/src/Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -87,7 +105,7 @@ void WebViewWidget::idleCallback()
webViewIdle(webview);
}

// -----------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------

static void notImplemented(const char* const name)
{
Expand Down
7 changes: 7 additions & 0 deletions distrho/extra/WebViewImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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) {}
};
Expand Down
8 changes: 6 additions & 2 deletions distrho/src/DistrhoUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0fe900d

Please sign in to comment.