From ef613e3bfa74a5e9588677d4e720b23b2a94dd5d Mon Sep 17 00:00:00 2001 From: falkTX Date: Wed, 8 May 2024 08:37:25 +0200 Subject: [PATCH] add some mac webui details, cpp->js fully working Signed-off-by: falkTX --- distrho/extra/WebViewImpl.cpp | 90 +++++++++++-- distrho/extra/WebViewImpl.hpp | 4 +- examples/Meters/ExampleUIMeters.cpp | 4 +- examples/WebMeters/ExamplePluginWebMeters.cpp | 3 +- examples/WebMeters/ExampleUIWebMeters.cpp | 15 ++- examples/WebMeters/index.html | 121 +++++++++++++++++- 6 files changed, 211 insertions(+), 26 deletions(-) diff --git a/distrho/extra/WebViewImpl.cpp b/distrho/extra/WebViewImpl.cpp index f2a42c5b..f708439d 100644 --- a/distrho/extra/WebViewImpl.cpp +++ b/distrho/extra/WebViewImpl.cpp @@ -82,11 +82,21 @@ #define WEB_VIEW_DELEGATE_CLASS_NAME \ MACRO_NAME(WebViewDelegate_, _, DISTRHO_NAMESPACE) -@interface WEB_VIEW_DELEGATE_CLASS_NAME : NSObject +// FIXME +static bool loaded = false; + +@interface WEB_VIEW_DELEGATE_CLASS_NAME : NSObject @end @implementation WEB_VIEW_DELEGATE_CLASS_NAME +- (void)webView:(WKWebView *)webview + didFinishNavigation:(WKNavigation*)navigation +{ + d_stdout("page loaded"); + loaded = true; +} + - (void)webView:(WKWebView*)webview runJavaScriptAlertPanelWithMessage:(NSString*)message initiatedByFrame:(WKFrameInfo*)frame @@ -181,6 +191,15 @@ }); } +- (void)userContentController:(WKUserContentController*)userContentController + didReceiveScriptMessage:(WKScriptMessage*)message +{ + NSString* const nsstring = static_cast([message body]); + const char* const string = [nsstring UTF8String]; + + d_stdout("JS call received '%s'", string); +} + @end #endif // WEB_VIEW_USING_MACOS_WEBKIT @@ -349,25 +368,75 @@ WebViewHandle webViewCreate(const uintptr_t windowId, (initialHeight - options.offset.y)); WKPreferences* const prefs = [[WKPreferences alloc] init]; - [prefs setValue:@YES forKey:@"developerExtrasEnabled"]; + [prefs setValue:@YES forKey:@"javaScriptCanAccessClipboard"]; + [prefs setValue:@YES forKey:@"DOMPasteAllowed"]; + + // if (debug) + { + [prefs setValue:@YES forKey:@"developerExtrasEnabled"]; + // TODO enable_write_console_messages_to_stdout + } WKWebViewConfiguration* const config = [[WKWebViewConfiguration alloc] init]; + config.limitsNavigationsToAppBoundDomains = false; config.preferences = prefs; WKWebView* const webview = [[WKWebView alloc] initWithFrame:rect configuration:config]; + [webview setHidden:YES]; [view addSubview:webview]; + // TODO webkit_web_view_set_background_color + WEB_VIEW_DELEGATE_CLASS_NAME* const delegate = [[WEB_VIEW_DELEGATE_CLASS_NAME alloc] init]; + webview.navigationDelegate = delegate; webview.UIDelegate = delegate; - const char* const url = "https://mastodon.falktx.com/"; + if (WKUserContentController* const controller = [config userContentController]) + { + [controller retain]; + [controller addScriptMessageHandler:delegate name:@"external"]; + } + + const char* const url = "file:///Users/falktx/Source/DISTRHO/DPF/examples/WebMeters/index.html"; 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 loadRequest:urlreq]; + [webview loadFileRequest:urlreq + allowingReadAccessToURL:[NSURL URLWithString:@"file:///Users/falktx/Source/DISTRHO/DPF/examples/WebMeters/"]]; + + d_stdout("waiting for load"); + + if (! loaded) + { + NSAutoreleasePool* const pool = [[NSAutoreleasePool alloc] init]; + NSDate* const date = [NSDate distantPast]; + NSEvent* event; + + while (! loaded) + { + event = [NSApp + #ifdef __MAC_10_12 + nextEventMatchingMask:NSEventMaskAny + #else + nextEventMatchingMask:NSAnyEventMask + #endif + untilDate:date + inMode:NSDefaultRunLoopMode + dequeue:YES]; + + if (event == nil) + break; + + [NSApp sendEvent: event]; + } + + [pool release]; + } + [webview setHidden:NO]; [nsurl release]; @@ -441,26 +510,26 @@ void webViewDestroy(const WebViewHandle handle) { #if WEB_VIEW_USING_CHOC delete handle->webview; - delete handle; #elif WEB_VIEW_USING_MACOS_WEBKIT [handle->webview setHidden:YES]; [handle->webview removeFromSuperview]; [handle->urlreq release]; [handle->delegate release]; - delete handle; #elif WEB_VIEW_USING_X11_IPC XCloseDisplay(handle->display); - delete handle; #endif - - // maybe unused - (void)handle; + delete handle; } void webViewEvaluateJS(const WebViewHandle handle, const char* const js) { #if WEB_VIEW_USING_CHOC #elif WEB_VIEW_USING_MACOS_WEBKIT + NSString* const nsjs = [[NSString alloc] initWithBytes:js + length:std::strlen(js) + encoding:NSUTF8StringEncoding]; + [handle->webview evaluateJavaScript:nsjs completionHandler:nullptr]; + [nsjs release]; #elif WEB_VIEW_USING_X11_IPC handle->p.signal(SIGUSR2); #endif @@ -705,6 +774,7 @@ static bool gtk3(Display* const display, WebKitSettings* const settings = webkit_settings_new(); DISTRHO_SAFE_ASSERT_RETURN(settings != nullptr, false); + // TODO DOMPasteAllowed webkit_settings_set_javascript_can_access_clipboard(settings, true); webkit_settings_set_hardware_acceleration_policy(settings, 2 /* WEBKIT_HARDWARE_ACCELERATION_POLICY_NEVER */); diff --git a/distrho/extra/WebViewImpl.hpp b/distrho/extra/WebViewImpl.hpp index df9d8efa..268e9009 100644 --- a/distrho/extra/WebViewImpl.hpp +++ b/distrho/extra/WebViewImpl.hpp @@ -58,8 +58,10 @@ struct WebViewOptions { This means it will draw on top of whatever is below it, something to take into consideration if mixing regular widgets with web views. + 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 (only used on X11) + @p scaleFactor: Scale factor to use (ignored on macOS) @p options: Extra options, optional */ WebViewHandle webViewCreate(uintptr_t windowId, diff --git a/examples/Meters/ExampleUIMeters.cpp b/examples/Meters/ExampleUIMeters.cpp index 0f33eff2..ead23f94 100644 --- a/examples/Meters/ExampleUIMeters.cpp +++ b/examples/Meters/ExampleUIMeters.cpp @@ -112,8 +112,8 @@ protected: static const Color kColorYellow(255, 255, 0); // get meter values - const float outLeft(fOutLeft); - const float outRight(fOutRight); + const float outLeft = fOutLeft; + const float outRight = fOutRight; // tell DSP side to reset meter values setState("reset", ""); diff --git a/examples/WebMeters/ExamplePluginWebMeters.cpp b/examples/WebMeters/ExamplePluginWebMeters.cpp index 0b188017..b91a461a 100644 --- a/examples/WebMeters/ExamplePluginWebMeters.cpp +++ b/examples/WebMeters/ExamplePluginWebMeters.cpp @@ -237,7 +237,8 @@ protected: { fOutLeft = tmpLeft; fOutRight = tmpRight; - fNeedsReset = false; + // TODO + // fNeedsReset = false; } else { diff --git a/examples/WebMeters/ExampleUIWebMeters.cpp b/examples/WebMeters/ExampleUIWebMeters.cpp index 1278b98f..807471b7 100644 --- a/examples/WebMeters/ExampleUIWebMeters.cpp +++ b/examples/WebMeters/ExampleUIWebMeters.cpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2019 Filipe Coelho + * Copyright (C) 2012-2024 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -24,9 +24,8 @@ class ExampleUIMeters : public UI { public: ExampleUIMeters() - : UI(600, 400) + : UI(600, 400, true) { - setGeometryConstraints(600, 400, false); } protected: @@ -39,8 +38,14 @@ protected: */ void parameterChanged(uint32_t index, float value) override { - d_stdout("param changed %u %f", index, value); - evaluateJS("if (typeof(parameterChanged) === 'function') { parameterChanged(0, 0); }"); + // d_stdout("param changed %u %f", index, value); + char msg[512]; + { + const ScopedSafeLocale ssl; + std::snprintf(msg, sizeof(msg) - 1, + "typeof(parameterChanged) === 'function' && parameterChanged(%u, %f)", index, value); + } + evaluateJS(msg); } /** diff --git a/examples/WebMeters/index.html b/examples/WebMeters/index.html index e312877a..956e0ab4 100644 --- a/examples/WebMeters/index.html +++ b/examples/WebMeters/index.html @@ -4,22 +4,129 @@ -
Hello World!
-
-

This is my text, nice neato.

-
-
+

 

+
+
+
+
+
+
+
+