Add an option for using custom dialog icons on MacOS

This commit is contained in:
Serge Belov 2021-10-07 11:39:36 +13:00 committed by Nuno Cruces
parent 8d981fce7e
commit 17827d3b70
5 changed files with 27 additions and 9 deletions

View file

@ -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)}

View file

@ -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)
}

View file

@ -130,6 +130,7 @@ type Dialog struct {
Text string
Extra *string
Options DialogOptions
IconPath *string
}
// DialogOptions is internal.

View file

@ -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
if opts.iconPath != nil {
data.IconPath = opts.iconPath
} else {
data.Options.Icon = opts.icon.String()
}
} else {
data.Operation = "displayAlert"
data.Options.As = kind.String()

View file

@ -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().