Support Translucent Window Background

This commit is contained in:
Lea Anthony
2020-10-02 07:45:22 +10:00
parent ee9c98c515
commit 26a1f78d56
4 changed files with 49 additions and 45 deletions

View File

@@ -153,6 +153,7 @@ struct Application {
int fullSizeContent; int fullSizeContent;
int useToolBar; int useToolBar;
int hideToolbarSeparator; int hideToolbarSeparator;
int windowBackgroundIsTranslucent;
// User Data // User Data
char *HTML; char *HTML;
@@ -241,6 +242,10 @@ void Show(struct Application *app) {
) )
} }
void SetWindowBackgroundIsTranslucent(struct Application *app) {
app->windowBackgroundIsTranslucent = 1;
}
// Sends messages to the backend // Sends messages to the backend
void messageHandler(id self, SEL cmd, id contentController, id message) { void messageHandler(id self, SEL cmd, id contentController, id message) {
struct Application *app = (struct Application *)objc_getAssociatedObject( 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->useToolBar = 0;
result->hideToolbarSeparator = 0; result->hideToolbarSeparator = 0;
result->appearance = NULL; result->appearance = NULL;
result->windowBackgroundIsTranslucent = 0;
// Window data // Window data
result->vibrancyLayer = NULL; result->vibrancyLayer = NULL;
@@ -695,19 +701,17 @@ void SetBindings(struct Application *app, const char *bindings) {
app->bindings = jscall; app->bindings = jscall;
} }
void applyVibrancy(struct Application *app) { void makeWindowBackgroundTranslucent(struct Application *app) {
id contentView = msg(app->mainWindow, s("contentView")); id contentView = msg(app->mainWindow, s("contentView"));
id effectView = msg(c("NSVisualEffectView"), s("alloc")); id effectView = msg(c("NSVisualEffectView"), s("alloc"));
CGRect bounds = GET_BOUNDS(contentView); CGRect bounds = GET_BOUNDS(contentView);
effectView = msg(effectView, s("initWithFrame:"), bounds); 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("setBlendingMode:"), NSVisualEffectBlendingModeBehindWindow);
msg(effectView, s("setState:"), NSVisualEffectStateFollowsWindowActiveState); msg(effectView, s("setState:"), NSVisualEffectStateActive);
// id blendingMode = msg(effectView, s("blendingMode"));
// int positioned = blendingMode == NSVisualEffectBlendingModeBehindWindow ? NSWindowBelow : NSWindowAbove;
msg(effectView, s("setMaterial:"), NSVisualEffectMaterialWindowBackground);
msg(contentView, s("addSubview:positioned:relativeTo:"), effectView, NSWindowBelow, NULL); msg(contentView, s("addSubview:positioned:relativeTo:"), effectView, NSWindowBelow, NULL);
app->vibrancyLayer = effectView; app->vibrancyLayer = effectView;
Debug("effectView: %p", effectView); Debug("effectView: %p", effectView);
} }
@@ -778,6 +782,14 @@ void createMainWindow(struct Application *app) {
mainWindow = msg(mainWindow, s("initWithContentRect:styleMask:backing:defer:"), mainWindow = msg(mainWindow, s("initWithContentRect:styleMask:backing:defer:"),
CGRectMake(0, 0, app->width, app->height), app->decorations, NSBackingStoreBuffered, NO); CGRectMake(0, 0, app->width, app->height), app->decorations, NSBackingStoreBuffered, NO);
msg(mainWindow, s("autorelease")); 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; app->mainWindow = mainWindow;
} }
@@ -803,42 +815,35 @@ void Run(struct Application *app, int argc, char **argv) {
// Center Window // Center Window
Center(app); Center(app);
// Set Style Mask
// msg(mainWindow, s("setStyleMask:"), app->decorations);
// Set Colour // Set Colour
applyWindowColour(app); applyWindowColour(app);
// applyVibrancy(app); if (app->windowBackgroundIsTranslucent) {
makeWindowBackgroundTranslucent(app);
}
// Setup webview // Setup webview
id config = msg(c("WKWebViewConfiguration"), s("new")); 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")); msg(config, s("setValue:forKey:"), msg(c("NSNumber"), s("numberWithBool:"), 1), str("suppressesIncrementalRendering"));
// TODO: Fix "NSWindow warning: adding an unknown subview: <WKInspectorWKWebView: 0x465ed90>. Break on NSLog to debug." error
if (app->devtools) { if (app->devtools) {
Debug("Enabling devtools"); Debug("Enabling devtools");
enableBoolConfig(config, "developerExtrasEnabled"); enableBoolConfig(config, "developerExtrasEnabled");
} }
msg(wkwebview, s("initWithFrame:configuration:"), CGRectMake(0, 0, 0, 0), config); app->config = config;
id manager = msg(config, s("userContentController"));
// Andd message handlers
msg(manager, s("addScriptMessageHandler:name:"), app->delegate, str("external")); 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 ) { id wkwebview = msg(c("WKWebView"), s("alloc"));
// msg(mainWindow, s("setContentView:"), wkwebview); app->wkwebview = wkwebview;
// } else {
// msg(app->vibrancyLayer, s("addSubview:"), wkwebview); // Only show content when fully rendered
// }
// TODO: Fix "NSWindow warning: adding an unknown subview: <WKInspectorWKWebView: 0x465ed90>. Break on NSLog to debug." error
msg(wkwebview, s("initWithFrame:configuration:"), CGRectMake(0, 0, 0, 0), config);
msg(contentView, s("addSubview:"), wkwebview); msg(contentView, s("addSubview:"), wkwebview);
msg(wkwebview, s("setAutoresizingMask:"), NSViewWidthSizable | NSViewHeightSizable); 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")); 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 // Finally call run
Debug("Run called"); Debug("Run called");
msg(app->application, s("run")); msg(app->application, s("run"));

View File

@@ -13,6 +13,7 @@ extern void HideToolbarSeparator(void *);
extern void DisableFrame(void *); extern void DisableFrame(void *);
extern void SetAppearance(void *, const char *); extern void SetAppearance(void *, const char *);
extern void WebviewIsTransparent(void *); extern void WebviewIsTransparent(void *);
extern void SetWindowBackgroundIsTranslucent(void *);
*/ */
import "C" import "C"
@@ -63,4 +64,9 @@ func (a *Application) processPlatformSettings() {
if mac.WebviewIsTransparent { if mac.WebviewIsTransparent {
C.WebviewIsTransparent(a.app) C.WebviewIsTransparent(a.app)
} }
// Check if window should be translucent
if mac.WindowBackgroundIsTranslucent {
C.SetWindowBackgroundIsTranslucent(a.app)
}
} }

View File

@@ -10,8 +10,9 @@ var Default = &App{
DevTools: true, DevTools: true,
RGBA: 0xFFFFFFFF, RGBA: 0xFFFFFFFF,
Mac: &mac.Options{ Mac: &mac.Options{
TitleBar: mac.TitleBarDefault(), TitleBar: mac.TitleBarDefault(),
Appearance: mac.DefaultAppearance, Appearance: mac.DefaultAppearance,
WebviewIsTransparent: false, WebviewIsTransparent: false,
WindowBackgroundIsTranslucent: false,
}, },
} }

View File

@@ -1,8 +1,9 @@
package mac package mac
// Options ae options speific to Mac // Options are options speific to Mac
type Options struct { type Options struct {
TitleBar *TitleBar TitleBar *TitleBar
Appearance AppearanceType Appearance AppearanceType
WebviewIsTransparent bool WebviewIsTransparent bool
WindowBackgroundIsTranslucent bool
} }