From 26a1f78d56a513f062864f8e82eabc5c4239c52e Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Fri, 2 Oct 2020 07:45:22 +1000 Subject: [PATCH] Support Translucent Window Background --- v2/internal/ffenestri/ffenestri_darwin.c | 72 +++++++++++------------ v2/internal/ffenestri/ffenestri_darwin.go | 6 ++ v2/pkg/options/default.go | 7 ++- v2/pkg/options/mac/mac.go | 9 +-- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/v2/internal/ffenestri/ffenestri_darwin.c b/v2/internal/ffenestri/ffenestri_darwin.c index 28ba7e74..bb60ece3 100644 --- a/v2/internal/ffenestri/ffenestri_darwin.c +++ b/v2/internal/ffenestri/ffenestri_darwin.c @@ -153,6 +153,7 @@ struct Application { int fullSizeContent; int useToolBar; int hideToolbarSeparator; + int windowBackgroundIsTranslucent; // User Data char *HTML; @@ -241,6 +242,10 @@ void Show(struct Application *app) { ) } +void SetWindowBackgroundIsTranslucent(struct Application *app) { + app->windowBackgroundIsTranslucent = 1; +} + // Sends messages to the backend void messageHandler(id self, SEL cmd, id contentController, id message) { struct Application *app = (struct Application *)objc_getAssociatedObject( @@ -307,6 +312,7 @@ void* NewApplication(const char *title, int width, int height, int resizable, in result->useToolBar = 0; result->hideToolbarSeparator = 0; result->appearance = NULL; + result->windowBackgroundIsTranslucent = 0; // Window data result->vibrancyLayer = NULL; @@ -695,19 +701,17 @@ void SetBindings(struct Application *app, const char *bindings) { app->bindings = jscall; } -void applyVibrancy(struct Application *app) { +void makeWindowBackgroundTranslucent(struct Application *app) { id contentView = msg(app->mainWindow, s("contentView")); id effectView = msg(c("NSVisualEffectView"), s("alloc")); CGRect bounds = GET_BOUNDS(contentView); effectView = msg(effectView, s("initWithFrame:"), bounds); - // msg(effectView, s("setAutoresizingMask:"), NSViewWidthSizable | NSViewHeightSizable); - msg(effectView, s("setTranslatesAutoresizingMaskIntoConstraints:"), NO); + + msg(effectView, s("setAutoresizingMask:"), NSViewWidthSizable | NSViewHeightSizable); msg(effectView, s("setBlendingMode:"), NSVisualEffectBlendingModeBehindWindow); - msg(effectView, s("setState:"), NSVisualEffectStateFollowsWindowActiveState); - // id blendingMode = msg(effectView, s("blendingMode")); - // int positioned = blendingMode == NSVisualEffectBlendingModeBehindWindow ? NSWindowBelow : NSWindowAbove; - msg(effectView, s("setMaterial:"), NSVisualEffectMaterialWindowBackground); + msg(effectView, s("setState:"), NSVisualEffectStateActive); msg(contentView, s("addSubview:positioned:relativeTo:"), effectView, NSWindowBelow, NULL); + app->vibrancyLayer = effectView; Debug("effectView: %p", effectView); } @@ -778,6 +782,14 @@ void createMainWindow(struct Application *app) { mainWindow = msg(mainWindow, s("initWithContentRect:styleMask:backing:defer:"), CGRectMake(0, 0, app->width, app->height), app->decorations, NSBackingStoreBuffered, NO); msg(mainWindow, s("autorelease")); + + // Set Appearance + if( app->appearance != NULL ) { + msg(mainWindow, s("setAppearance:"), + msg(c("NSAppearance"), s("appearanceNamed:"), str(app->appearance)) + ); + } + app->mainWindow = mainWindow; } @@ -803,42 +815,35 @@ void Run(struct Application *app, int argc, char **argv) { // Center Window Center(app); - // Set Style Mask - // msg(mainWindow, s("setStyleMask:"), app->decorations); - // Set Colour applyWindowColour(app); - // applyVibrancy(app); + if (app->windowBackgroundIsTranslucent) { + makeWindowBackgroundTranslucent(app); + } // Setup webview id config = msg(c("WKWebViewConfiguration"), s("new")); - app->config = config; - id manager = msg(config, s("userContentController")); - app->manager = manager; - id wkwebview = msg(c("WKWebView"), s("alloc")); - app->wkwebview = wkwebview; - - // Only show content when fully rendered msg(config, s("setValue:forKey:"), msg(c("NSNumber"), s("numberWithBool:"), 1), str("suppressesIncrementalRendering")); - - // TODO: Fix "NSWindow warning: adding an unknown subview: . Break on NSLog to debug." error if (app->devtools) { Debug("Enabling devtools"); enableBoolConfig(config, "developerExtrasEnabled"); } - msg(wkwebview, s("initWithFrame:configuration:"), CGRectMake(0, 0, 0, 0), config); + app->config = config; - - // Andd message handlers + id manager = msg(config, s("userContentController")); msg(manager, s("addScriptMessageHandler:name:"), app->delegate, str("external")); - msg(manager, s("addScriptMessageHandler:name:"), app->delegate, str("completed")); + msg(manager, s("addScriptMessageHandler:name:"), app->delegate, str("completed")); + app->manager = manager; - // if( app->vibrancyLayer == NULL ) { - // msg(mainWindow, s("setContentView:"), wkwebview); - // } else { - // msg(app->vibrancyLayer, s("addSubview:"), wkwebview); - // } + id wkwebview = msg(c("WKWebView"), s("alloc")); + app->wkwebview = wkwebview; + + // Only show content when fully rendered + + // TODO: Fix "NSWindow warning: adding an unknown subview: . Break on NSLog to debug." error + + msg(wkwebview, s("initWithFrame:configuration:"), CGRectMake(0, 0, 0, 0), config); msg(contentView, s("addSubview:"), wkwebview); msg(wkwebview, s("setAutoresizingMask:"), NSViewWidthSizable | NSViewHeightSizable); @@ -934,15 +939,6 @@ void Run(struct Application *app, int argc, char **argv) { msg(wkwebview, s("setValue:forKey:"), msg(c("NSNumber"), s("numberWithBool:"), 0), str("drawsBackground")); } - - // Set Appearance - if( app->appearance != NULL ) { - msg(app->mainWindow, s("setAppearance:"), - msg(c("NSAppearance"), s("appearanceNamed:"), str(app->appearance)) - ); - } - - // Finally call run Debug("Run called"); msg(app->application, s("run")); diff --git a/v2/internal/ffenestri/ffenestri_darwin.go b/v2/internal/ffenestri/ffenestri_darwin.go index 62faf506..8c80595e 100644 --- a/v2/internal/ffenestri/ffenestri_darwin.go +++ b/v2/internal/ffenestri/ffenestri_darwin.go @@ -13,6 +13,7 @@ extern void HideToolbarSeparator(void *); extern void DisableFrame(void *); extern void SetAppearance(void *, const char *); extern void WebviewIsTransparent(void *); +extern void SetWindowBackgroundIsTranslucent(void *); */ import "C" @@ -63,4 +64,9 @@ func (a *Application) processPlatformSettings() { if mac.WebviewIsTransparent { C.WebviewIsTransparent(a.app) } + + // Check if window should be translucent + if mac.WindowBackgroundIsTranslucent { + C.SetWindowBackgroundIsTranslucent(a.app) + } } diff --git a/v2/pkg/options/default.go b/v2/pkg/options/default.go index 94fe1afd..b3fdd074 100644 --- a/v2/pkg/options/default.go +++ b/v2/pkg/options/default.go @@ -10,8 +10,9 @@ var Default = &App{ DevTools: true, RGBA: 0xFFFFFFFF, Mac: &mac.Options{ - TitleBar: mac.TitleBarDefault(), - Appearance: mac.DefaultAppearance, - WebviewIsTransparent: false, + TitleBar: mac.TitleBarDefault(), + Appearance: mac.DefaultAppearance, + WebviewIsTransparent: false, + WindowBackgroundIsTranslucent: false, }, } diff --git a/v2/pkg/options/mac/mac.go b/v2/pkg/options/mac/mac.go index 66f546a1..920e0d9d 100644 --- a/v2/pkg/options/mac/mac.go +++ b/v2/pkg/options/mac/mac.go @@ -1,8 +1,9 @@ package mac -// Options ae options speific to Mac +// Options are options speific to Mac type Options struct { - TitleBar *TitleBar - Appearance AppearanceType - WebviewIsTransparent bool + TitleBar *TitleBar + Appearance AppearanceType + WebviewIsTransparent bool + WindowBackgroundIsTranslucent bool }