Support MacBackdrop type

Fix devtools window error
This commit is contained in:
Lea Anthony 2022-12-08 07:47:04 +11:00
commit cb9df61a98
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
3 changed files with 87 additions and 6 deletions

View file

@ -26,13 +26,15 @@ func main() {
Width: 600,
Height: 400,
AlwaysOnTop: false,
URL: "https://google.com",
DisableResize: false,
MinWidth: 100,
MinHeight: 100,
MaxWidth: 1000,
MaxHeight: 1000,
EnableDevTools: true,
Mac: &options.MacWindow{
Backdrop: options.MacBackdropNormal,
},
})
myWindow2 := app.NewWindow(&options.Window{
@ -53,8 +55,6 @@ func main() {
myWindow2.NavigateToURL("https://wails.io")
myWindow.SetMinSize(600, 600)
myWindow.SetMaxSize(650, 650)
time.Sleep(3 * time.Second)
myWindow.ExecJS("window.location.href = 'https://duckduckgo.com'")
}()

View file

@ -19,22 +19,38 @@ void* windowNew(int width, int height) {
styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask
backing:NSBackingStoreBuffered
defer:NO];
// Create delegate
WindowDelegate* delegate = [[WindowDelegate alloc] init];
// Set delegate
[window setDelegate:delegate];
// Add NSView to window
NSView* view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, width-1, height-1)];
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[window setContentView:view];
// Embed wkwebview in window
NSRect frame = NSMakeRect(0, 0, width, height);
WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc] init];
WKWebView* webView = [[WKWebView alloc] initWithFrame:frame configuration:config];
[window setContentView:webView];
[view addSubview:webView];
delegate.webView = webView;
delegate.hideOnClose = false;
return window;
}
// Make NSWindow transparent
void windowSetTransparent(void* nsWindow) {
// On main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSWindow* window = (NSWindow*)nsWindow;
[window setOpaque:NO];
[window setBackgroundColor:[NSColor clearColor]];
});
}
// Set the title of the NSWindow
void windowSetTitle(void* nsWindow, char* title) {
// Set window title on main thread
@ -60,12 +76,18 @@ void windowSetSize(void* nsWindow, int width, int height) {
// Show the NSWindow
void windowShow(void* nsWindow) {
[(NSWindow*)nsWindow makeKeyAndOrderFront:nil];
// Show window on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[(NSWindow*)nsWindow makeKeyAndOrderFront:nil];
});
}
// Hide the NSWindow
void windowHide(void* nsWindow) {
[(NSWindow*)nsWindow orderOut:nil];
// Hide window on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[(NSWindow*)nsWindow orderOut:nil];
});
}
// Set NSWindow always on top
@ -149,6 +171,41 @@ void windowExecJS(void* nsWindow, char* js) {
});
}
// Make NSWindow backdrop translucent
void windowSetTranslucent(void* nsWindow) {
// Set window transparent on main thread
dispatch_async(dispatch_get_main_queue(), ^{
// Get window
NSWindow* window = (NSWindow*)nsWindow;
// Get window delegate
WindowDelegate* delegate = (WindowDelegate*)[(NSWindow*)nsWindow delegate];
id contentView = [window contentView];
NSVisualEffectView *effectView = [NSVisualEffectView alloc];
NSRect bounds = [contentView bounds];
[effectView initWithFrame:bounds];
[effectView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[effectView setBlendingMode:NSVisualEffectBlendingModeBehindWindow];
[effectView setState:NSVisualEffectStateActive];
[contentView addSubview:effectView positioned:NSWindowBelow relativeTo:nil];
});
}
// Make webview background transparent
void webviewSetTransparent(void* nsWindow) {
// Set webview transparent on main thread
dispatch_async(dispatch_get_main_queue(), ^{
// Get window delegate
WindowDelegate* delegate = (WindowDelegate*)[(NSWindow*)nsWindow delegate];
// Set webview background transparent
[delegate.webView setValue:@YES forKey:@"drawsTransparentBackground"];
});
}
*/
import "C"
import (
@ -217,6 +274,16 @@ func (w *macosWindow) run() error {
if w.options.EnableDevTools {
w.enableDevTools()
}
if w.options.Mac != nil {
switch w.options.Mac.Backdrop {
case options.MacBackdropTransparent:
C.windowSetTransparent(w.nsWindow)
C.webviewSetTransparent(w.nsWindow)
case options.MacBackdropTranslucent:
C.windowSetTranslucent(w.nsWindow)
C.webviewSetTransparent(w.nsWindow)
}
}
C.windowShow(w.nsWindow)
return nil
}

View file

@ -32,4 +32,18 @@ type Window struct {
MaxWidth int
MaxHeight int
EnableDevTools bool
Mac *MacWindow
}
type MacBackdrop int
const (
MacBackdropNormal MacBackdrop = iota
MacBackdropTransparent
MacBackdropTranslucent
)
// MacWindow contains macOS specific options
type MacWindow struct {
Backdrop MacBackdrop
}