From 17827d3b7074f9d59f499def2dffc52f85869ff4 Mon Sep 17 00:00:00 2001 From: Serge Belov Date: Thu, 7 Oct 2021 11:39:36 +1300 Subject: [PATCH] Add an option for using custom dialog icons on MacOS --- internal/zenutil/osa_generated.go | 10 +++++++--- internal/zenutil/osascripts/dialog.gojs | 11 +++++++---- internal/zenutil/run_darwin.go | 1 + msg_darwin.go | 8 ++++++-- zenity.go | 6 ++++++ 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/internal/zenutil/osa_generated.go b/internal/zenutil/osa_generated.go index 1aa7ba6..92b286f 100644 --- a/internal/zenutil/osa_generated.go +++ b/internal/zenutil/osa_generated.go @@ -55,9 +55,13 @@ fmt.stringFromDate(date.dateValue) var app=Application.currentApplication() app.includeStandardAdditions=true app.activate() -ObjC.import('stdio') -ObjC.import('stdlib') -var res=app.{{.Operation}}({{json .Text}},{{json .Options}}) +ObjC.import("stdlib") +ObjC.import("stdio") +var opt={{json .Options}} +{{- if .IconPath}} +opt["withIcon"]=Path("{{.IconPath}}") +{{- end}} +var res=app.{{.Operation}}({{json .Text}},opt) if(res.gaveUp){$.exit(5)} if(res.buttonReturned==={{json .Extra}}){$.puts(res.buttonReturned) $.exit(1)} diff --git a/internal/zenutil/osascripts/dialog.gojs b/internal/zenutil/osascripts/dialog.gojs index 2142faf..9189883 100644 --- a/internal/zenutil/osascripts/dialog.gojs +++ b/internal/zenutil/osascripts/dialog.gojs @@ -2,10 +2,13 @@ var app = Application.currentApplication() app.includeStandardAdditions = true app.activate() -ObjC.import('stdio') -ObjC.import('stdlib') - -var res = app.{{.Operation}}({{json .Text}}, {{json .Options}}) +ObjC.import("stdlib") +ObjC.import("stdio") +var opt = {{json .Options}} +{{- if .IconPath}} +opt["withIcon"] = Path("{{.IconPath}}") +{{- end}} +var res = app.{{.Operation}}({{json .Text}}, opt) if (res.gaveUp) { $.exit(5) } diff --git a/internal/zenutil/run_darwin.go b/internal/zenutil/run_darwin.go index d2bbfa0..403cde1 100644 --- a/internal/zenutil/run_darwin.go +++ b/internal/zenutil/run_darwin.go @@ -130,6 +130,7 @@ type Dialog struct { Text string Extra *string Options DialogOptions + IconPath *string } // DialogOptions is internal. diff --git a/msg_darwin.go b/msg_darwin.go index 27836cf..3f2e836 100644 --- a/msg_darwin.go +++ b/msg_darwin.go @@ -11,7 +11,7 @@ func message(kind messageKind, text string, opts options) error { // dialog is more flexible, alert prettier var dialog bool - if opts.icon != 0 { // use if we want to show a specific icon + if opts.icon != 0 || opts.iconPath != nil { // use if we want to show a specific icon dialog = true } else if kind == questionKind && opts.cancelLabel == nil { // use for questions with default buttons dialog = true @@ -20,7 +20,11 @@ func message(kind messageKind, text string, opts options) error { if dialog { data.Operation = "displayDialog" data.Options.Title = opts.title - data.Options.Icon = opts.icon.String() + if opts.iconPath != nil { + data.IconPath = opts.iconPath + } else { + data.Options.Icon = opts.icon.String() + } } else { data.Operation = "displayAlert" data.Options.As = kind.String() diff --git a/zenity.go b/zenity.go index d093c8a..4364254 100644 --- a/zenity.go +++ b/zenity.go @@ -39,6 +39,7 @@ type options struct { cancelLabel *string extraButton *string icon DialogIcon + iconPath *string defaultCancel bool // Message options @@ -150,6 +151,11 @@ const ( // Deprecated: use DialogIcon directly. func Icon(icon DialogIcon) Option { return icon } +// Icon returns an Option to set an icon loaded from a file. +func CustomIcon(path string) Option { + return funcOption(func(o *options) { o.iconPath = &path }) +} + // Context returns an Option to set a Context that can dismiss the dialog. // // Dialogs dismissed by ctx return ctx.Err().