From 471d6260430acdd43b0e092d3236fc8347e077fe Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Thu, 8 Feb 2024 08:31:58 +1100 Subject: [PATCH] Add control to disable window buttons on Mac Three new options have been introduced in the application options for MacOS to disable the minimize, maximize, and close buttons on the window. Corresponding functionality has been added in the webview window for darwin to handle these new options. These changes have been showcased in the window example main.go file. --- v3/examples/window/main.go | 22 ++++++++++++++++ v3/pkg/application/options_mac.go | 9 +++++++ v3/pkg/application/webview_window_darwin.go | 28 +++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/v3/examples/window/main.go b/v3/examples/window/main.go index dc911173..9c0e390f 100644 --- a/v3/examples/window/main.go +++ b/v3/examples/window/main.go @@ -67,6 +67,9 @@ func main() { Windows: application.WindowsWindow{ DisableMinimiseButton: true, }, + Mac: application.MacWindow{ + DisableMinimiseButton: true, + }, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). @@ -80,6 +83,24 @@ func main() { Windows: application.WindowsWindow{ DisableMaximiseButton: true, }, + Mac: application.MacWindow{ + DisableMaximiseButton: true, + }, + }). + SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). + SetRelativePosition(rand.Intn(1000), rand.Intn(800)). + SetURL("https://wails.io"). + Show() + windowCounter++ + }) + } + if runtime.GOOS == "darwin" { + myMenu.Add("New WebviewWindow (Disable Close)"). + OnClick(func(ctx *application.Context) { + app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ + Mac: application.MacWindow{ + DisableCloseButton: true, + }, }). SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)). @@ -89,6 +110,7 @@ func main() { }) } + myMenu.Add("New WebviewWindow (Hides on Close one time)"). SetAccelerator("CmdOrCtrl+H"). OnClick(func(ctx *application.Context) { diff --git a/v3/pkg/application/options_mac.go b/v3/pkg/application/options_mac.go index c79287fc..1078a5aa 100644 --- a/v3/pkg/application/options_mac.go +++ b/v3/pkg/application/options_mac.go @@ -52,6 +52,15 @@ type MacWindow struct { // WebviewPreferences contains preferences for the webview WebviewPreferences MacWebviewPreferences + + // Disables the minimise button + DisableMinimiseButton bool + + // Disables the maximise button + DisableMaximiseButton bool + + // Disables the close button + DisableCloseButton bool } // MacWebviewPreferences contains preferences for the Mac webview diff --git a/v3/pkg/application/webview_window_darwin.go b/v3/pkg/application/webview_window_darwin.go index dc86ae96..10c0f0ed 100644 --- a/v3/pkg/application/webview_window_darwin.go +++ b/v3/pkg/application/webview_window_darwin.go @@ -634,6 +634,24 @@ static void windowHide(void *window) { [(WebviewWindow*)window orderOut:nil]; } +static void enableMinimiseButton(void *window, bool enabled) { + WebviewWindow* nsWindow = (WebviewWindow*)window; + NSButton *minimiseButton = [nsWindow standardWindowButton:NSWindowMiniaturizeButton]; + minimiseButton.enabled = enabled; +} + +static void enableMaximiseButton(void *window, bool enabled) { + WebviewWindow* nsWindow = (WebviewWindow*)window; + NSButton *maximiseButton = [nsWindow standardWindowButton:NSWindowZoomButton]; + maximiseButton.enabled = enabled; +} + +static void enableCloseButton(void *window, bool enabled) { + WebviewWindow* nsWindow = (WebviewWindow*)window; + NSButton *closeButton = [nsWindow standardWindowButton:NSWindowCloseButton]; + closeButton.enabled = enabled; +} + // windowShowMenu opens an NSMenu at the given coordinates static void windowShowMenu(void *window, void *menu, int x, int y) { NSMenu* nsMenu = (NSMenu*)menu; @@ -1108,6 +1126,16 @@ func (w *macosWebviewWindow) run() { case MacBackdropNormal: } + if macOptions.DisableMinimiseButton { + C.enableMinimiseButton(w.nsWindow, C.bool(false)) + } + if macOptions.DisableMaximiseButton { + C.enableMaximiseButton(w.nsWindow, C.bool(false)) + } + if macOptions.DisableCloseButton { + C.enableCloseButton(w.nsWindow, C.bool(false)) + } + if options.IgnoreMouseEvents { C.windowIgnoreMouseEvents(w.nsWindow, C.bool(true)) }