Skip to content

Commit

Permalink
more web-ui, macos with init js
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 0fe900d commit 6da9fcd
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 91 deletions.
23 changes: 13 additions & 10 deletions Makefile.plugins.mk
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,6 @@ endif

ifeq ($(UI_TYPE),web)
DGL_FLAGS += -DDGL_WEB -DHAVE_DGL
ifeq ($(MACOS),true)
# BUILD_CXX_FLAGS += -std=gnu++17
DGL_LIBS += -framework WebKit
else ifeq ($(WINDOWS),true)
# DGL_FLAGS += -std=gnu++17
DGL_LIBS += -lole32 -luuid
else
DGL_FLAGS += -pthread
DGL_LIBS += -pthread -lrt
endif
DGL_LIB = $(DGL_BUILD_DIR)/libdgl-web.a
HAVE_DGL = true
USE_WEBVIEW = true
Expand All @@ -271,6 +261,19 @@ ifeq ($(HAVE_DGL)$(LINUX)$(USE_WEBVIEW),truetruetrue)
DGL_LIB_SHARED = $(shell $(CC) -print-file-name=Scrt1.o)
endif

ifeq ($(USE_WEBVIEW),true)
ifeq ($(MACOS),true)
# BUILD_CXX_FLAGS += -std=gnu++17
DGL_LIBS += -framework WebKit
else ifeq ($(WINDOWS),true)
# DGL_FLAGS += -std=gnu++17
DGL_LIBS += -lole32 -luuid
else
DGL_FLAGS += -pthread
DGL_LIBS += -pthread -lrt
endif
endif

DGL_LIBS += $(DGL_SYSTEM_LIBS) -lm

# TODO split dsp and ui object build flags
Expand Down
6 changes: 3 additions & 3 deletions dgl/Web.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ class WebViewWidget : public TopLevelWidget,
/**
Constructor for a WebViewWidget.
*/
explicit WebViewWidget(Window& windowToMapTo, bool initLater = false);
explicit WebViewWidget(Window& windowToMapTo);

/**
Destructor.
*/
~WebViewWidget() override;

void init(const char* url, const char* initialJS);

// webview methods
void evaluateJS(const char* js);
void reload();

protected:
void init(const char* initialJS);

virtual void onMessage(char* message);
void onResize(const ResizeEvent& ev) override;

Expand Down
16 changes: 4 additions & 12 deletions dgl/src/Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,10 @@ START_NAMESPACE_DGL

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

WebViewWidget::WebViewWidget(Window& windowToMapTo, bool initLater)
WebViewWidget::WebViewWidget(Window& windowToMapTo)
: TopLevelWidget(windowToMapTo),
webview(initLater ? nullptr : webViewCreate(windowToMapTo.getNativeWindowHandle(),
windowToMapTo.getWidth(),
windowToMapTo.getHeight(),
windowToMapTo.getScaleFactor(),
WebViewOptions(_on_msg, this)))
webview(nullptr)
{
#if !(defined(DISTRHO_OS_MAC) || defined(DISTRHO_OS_WINDOWS))
if (webview != nullptr)
addIdleCallback(this, 1000 / 60);
#endif
}

WebViewWidget::~WebViewWidget()
Expand All @@ -53,13 +45,13 @@ WebViewWidget::~WebViewWidget()
}
}

void WebViewWidget::init(const char* const initialJS)
void WebViewWidget::init(const char* const url, 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);
webview = webViewCreate(url, getWindow().getNativeWindowHandle(), getWidth(), getHeight(), getScaleFactor(), options);

// FIXME implement initialJS
if (webview != nullptr)
Expand Down
7 changes: 3 additions & 4 deletions distrho/DistrhoUI_macOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
# include "extra/WebViewImpl.cpp"
#endif

#if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
# include <algorithm>
# include <cmath>
#include <algorithm>
#include <cmath>

START_NAMESPACE_DISTRHO
double getDesktopScaleFactor(const uintptr_t parentWindowHandle)
{
Expand All @@ -61,4 +61,3 @@
return [NSScreen mainScreen].backingScaleFactor;
}
END_NAMESPACE_DISTRHO
#endif
68 changes: 49 additions & 19 deletions distrho/extra/WebViewImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ static void getFilenameFromFunctionPtr(char filename[PATH_MAX], const void* cons

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

WebViewHandle webViewCreate(const uintptr_t windowId,
WebViewHandle webViewCreate(const char* const url,
const uintptr_t windowId,
const uint initialWidth,
const uint initialHeight,
const double scaleFactor,
Expand Down Expand Up @@ -464,11 +465,6 @@ WebViewHandle webViewCreate(const uintptr_t windowId,
#elif WEB_VIEW_USING_MACOS_WEBKIT
NSView* const view = reinterpret_cast<NSView*>(windowId);

const CGRect rect = CGRectMake(options.offset.x,
options.offset.y,
(initialWidth - options.offset.x),
(initialHeight - options.offset.y));

WKPreferences* const prefs = [[WKPreferences alloc] init];
[prefs setValue:@YES forKey:@"javaScriptCanAccessClipboard"];
[prefs setValue:@YES forKey:@"DOMPasteAllowed"];
Expand All @@ -483,6 +479,11 @@ WebViewHandle webViewCreate(const uintptr_t windowId,
config.limitsNavigationsToAppBoundDomains = false;
config.preferences = prefs;

const CGRect rect = CGRectMake(options.offset.x / scaleFactor,
options.offset.y / scaleFactor,
initialWidth,
initialHeight);

WKWebView* const webview = [[WKWebView alloc] initWithFrame:rect
configuration:config];
[webview setHidden:YES];
Expand All @@ -495,31 +496,61 @@ WebViewHandle webViewCreate(const uintptr_t windowId,
delegate->callbackPtr = options.callbackPtr;
delegate->loaded = false;

webview.navigationDelegate = delegate;
webview.UIDelegate = delegate;

if (WKUserContentController* const controller = [config userContentController])
{
[controller retain];
[controller addScriptMessageHandler:delegate name:@"external"];

if (options.initialJS != nullptr)
{
NSString* const nsInitialJS = [[NSString alloc] initWithBytes:options.initialJS
length:std::strlen(options.initialJS)
encoding:NSUTF8StringEncoding];

WKUserScript* const script = [[WKUserScript alloc] initWithSource:nsInitialJS
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:true];

[controller addUserScript:script];

[script release];
[nsInitialJS release];
}
}

const char* const url = "file:///Users/falktx/Source/DISTRHO/DPF/examples/WebMeters/index.html";
[webview setNavigationDelegate:delegate];
[webview setUIDelegate:delegate];

NSString* const nsurl = [[NSString alloc] initWithBytes:url
length:std::strlen(url)
encoding:NSUTF8StringEncoding];
NSURLRequest* const urlreq = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString: nsurl]];

// [webview loadRequest:urlreq];
[webview loadFileRequest:urlreq
allowingReadAccessToURL:[NSURL URLWithString:@"file:///Users/falktx/Source/DISTRHO/DPF/examples/WebMeters/"]];
d_stdout("url is '%s'", url);
if (std::strncmp(url, "file://", 7) == 0)
{
const char* const lastsep = std::strrchr(url + 7, '/');

NSString* const urlpath = [[NSString alloc] initWithBytes:url
length:(lastsep - url)
encoding:NSUTF8StringEncoding];

[webview loadFileRequest:urlreq
allowingReadAccessToURL:[NSURL URLWithString:urlpath]];

[urlpath release];
}
else
{
[webview loadRequest:urlreq];
}

d_stdout("waiting for load");

if (! delegate->loaded)
{
NSAutoreleasePool* const pool = [[NSAutoreleasePool alloc] init];
NSDate* const date = [NSDate distantFuture];
NSDate* const date = [NSDate dateWithTimeIntervalSinceNow:0.05];
NSEvent* event;

while (! delegate->loaded)
Expand Down Expand Up @@ -702,7 +733,7 @@ void webViewDestroy(const WebViewHandle handle)
[handle->webview setHidden:YES];
[handle->webview removeFromSuperview];
[handle->urlreq release];
[handle->delegate release];
// [handle->delegate release];
#elif WEB_VIEW_USING_X11_IPC
munmap(handle->shmptr, sizeof(WebViewRingBuffer));
close(handle->shmfd);
Expand Down Expand Up @@ -804,16 +835,15 @@ void webViewResize(const WebViewHandle handle, const uint width, const uint heig
#if WEB_VIEW_USING_CHOC
#ifdef DISTRHO_OS_MAC
NSView* const view = static_cast<NSView*>(handle->webview->getViewHandle());
[view setFrameSize:NSMakeSize(width, height)];
[view setFrameSize:NSMakeSize(width / scaleFactor, height / scaleFactor)];
#else
const HWND hwnd = static_cast<HWND>(handle->webview->getViewHandle());
SetWindowPos(hwnd, nullptr, 0, 0,
width * scaleFactor,
height * scaleFactor,
width, height,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
#endif
#elif WEB_VIEW_USING_MACOS_WEBKIT
[handle->webview setFrameSize:NSMakeSize(width, height)];
[handle->webview setFrameSize:NSMakeSize(width / scaleFactor, height / scaleFactor)];
#elif WEB_VIEW_USING_X11_IPC
if (handle->childWindow == 0)
{
Expand Down
9 changes: 5 additions & 4 deletions distrho/extra/WebViewImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ struct WebViewOptions {
Position offset, for cases of mixing regular widgets with web views.
*/
struct PositionOffset {
/** Horizontal offset */
/** Horizontal offset, with scale factor pre-applied */
int x;

/** Vertical offset */
/** Vertical offset, with scale factor pre-applied */
int y;

/** Constructor for default values */
Expand Down Expand Up @@ -83,10 +83,11 @@ struct WebViewOptions {
Provided metrics must not have scale factor pre-applied.
@p windowId: The native window id to attach this view to (X11 Window, HWND or NSView*)
@p scaleFactor: Scale factor to use (ignored on macOS)
@p scaleFactor: Scale factor in use
@p options: Extra options, optional
*/
WebViewHandle webViewCreate(uintptr_t windowId,
WebViewHandle webViewCreate(const char* url,
uintptr_t windowId,
uint initialWidth,
uint initialHeight,
double scaleFactor,
Expand Down
12 changes: 4 additions & 8 deletions distrho/src/DistrhoPluginJACK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,15 +980,11 @@ int main(int argc, char* argv[])
String tmpPath(getBinaryFilename());
tmpPath.truncate(tmpPath.rfind(DISTRHO_OS_SEP));
#if defined(DISTRHO_OS_MAC)
if (tmpPath.endsWith("/MacOS"))
if (tmpPath.endsWith("/Contents/MacOS"))
{
tmpPath.truncate(tmpPath.rfind('/'));
if (tmpPath.endsWith("/Contents"))
{
tmpPath.truncate(tmpPath.rfind('/'));
bundlePath = tmpPath;
d_nextBundlePath = bundlePath.buffer();
}
tmpPath.truncate(tmpPath.length() - 15);
bundlePath = tmpPath;
d_nextBundlePath = bundlePath.buffer();
}
#else
#ifdef DISTRHO_OS_WINDOWS
Expand Down
Loading

0 comments on commit 6da9fcd

Please sign in to comment.