From 225437f1e8a8be5f672713ba272e532f830b3f93 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 16 May 2023 17:54:34 +1000 Subject: [PATCH] [v3 mac] Add `DisableWindowShadow` option --- v3/examples/window/main.go | 6 +++++- v3/pkg/application/options_mac.go | 1 + v3/pkg/application/webview_window_darwin.go | 24 +++++++++++++++++++++ v3/pkg/application/webview_window_darwin.m | 4 ++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/v3/examples/window/main.go b/v3/examples/window/main.go index c428a92fd..50a8aa345 100644 --- a/v3/examples/window/main.go +++ b/v3/examples/window/main.go @@ -273,7 +273,11 @@ func main() { app.InfoDialog().SetTitle(fmt.Sprintf("Screen %s", screen.ID)).SetMessage(msg).Show() }) }) - app.NewWebviewWindow() + app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ + Mac: application.MacWindow{ + DisableWindowShadow: true, + }, + }) app.SetMenu(menu) err := app.Run() diff --git a/v3/pkg/application/options_mac.go b/v3/pkg/application/options_mac.go index 7ad1c5f0f..8e852b17b 100644 --- a/v3/pkg/application/options_mac.go +++ b/v3/pkg/application/options_mac.go @@ -44,6 +44,7 @@ const ( // MacWindow contains macOS specific options type MacWindow struct { Backdrop MacBackdrop + DisableWindowShadow bool TitleBar MacTitleBar Appearance MacAppearanceType InvisibleTitleBarHeight int diff --git a/v3/pkg/application/webview_window_darwin.go b/v3/pkg/application/webview_window_darwin.go index 92f5b9253..9c6f64c08 100644 --- a/v3/pkg/application/webview_window_darwin.go +++ b/v3/pkg/application/webview_window_darwin.go @@ -642,6 +642,17 @@ void windowDestroy(void* nsWindow) { }); } +// Remove drop shadow from window +void windowSetShadow(void* nsWindow, bool hasShadow) { + // Remove shadow on main thread + dispatch_async(dispatch_get_main_queue(), ^{ + // get main window + WebviewWindow* window = (WebviewWindow*)nsWindow; + // set shadow + [window setHasShadow:hasShadow]; + }); +} + // windowClose closes the current window static void windowClose(void *window) { @@ -841,6 +852,10 @@ func (w *macosWebviewWindow) setFrameless(frameless bool) { } } +func (w *macosWebviewWindow) setHasShadow(hasShadow bool) { + C.windowSetShadow(w.nsWindow, C.bool(hasShadow)) +} + func (w *macosWebviewWindow) getScreen() (*Screen, error) { return getScreenForWindow(w) } @@ -1161,7 +1176,16 @@ func (w *macosWebviewWindow) run() { } if w.parent.options.Hidden == false { C.windowShow(w.nsWindow) + w.setHasShadow(!w.parent.options.Mac.DisableWindowShadow) + } else { + // We have to wait until the window is shown before we can remove the shadow + var cancel func() + cancel = w.parent.On(events.Mac.WindowDidBecomeKey, func(_ *WindowEventContext) { + w.setHasShadow(!w.parent.options.Mac.DisableWindowShadow) + cancel() + }) } + }) } diff --git a/v3/pkg/application/webview_window_darwin.m b/v3/pkg/application/webview_window_darwin.m index 9dd79d510..4ca26750b 100644 --- a/v3/pkg/application/webview_window_darwin.m +++ b/v3/pkg/application/webview_window_darwin.m @@ -1,11 +1,14 @@ //go:build darwin + #import #import #import "webview_window_darwin.h" #import "../events/events.h" + extern void processMessage(unsigned int, const char*); extern void processURLRequest(unsigned int, void *); extern bool hasListeners(unsigned int); + @implementation WebviewWindow - (WebviewWindow*) initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation; { @@ -47,6 +50,7 @@ extern bool hasListeners(unsigned int); [super dealloc]; } @end + @implementation WebviewWindowDelegate - (BOOL)windowShouldClose:(NSWindow *)sender { if( self.hideOnClose ) {