From ab7621bec9d6e92447c313d13d2818c7f5ef45ab Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Thu, 2 Jun 2022 00:00:08 +0100 Subject: [PATCH] Window icons (macOS). --- color_darwin.go | 12 +++++-- date_darwin.go | 19 ++++++----- entry_darwin.go | 3 ++ file_darwin.go | 9 ++++++ internal/zenutil/osa_generated.go | 39 ++++++++++++----------- internal/zenutil/osascripts/color.gojs | 7 ++-- internal/zenutil/osascripts/common.gojs | 14 ++++++++ internal/zenutil/osascripts/date.gojs | 1 + internal/zenutil/osascripts/dialog.gojs | 5 +-- internal/zenutil/osascripts/file.gojs | 5 +-- internal/zenutil/osascripts/list.gojs | 5 +-- internal/zenutil/osascripts/progress.gojs | 5 +-- internal/zenutil/osascripts/pwd.gojs | 5 +-- internal/zenutil/run_darwin.go | 24 +++++++++++--- list_darwin.go | 3 ++ msg_darwin.go | 3 ++ progress_darwin.go | 3 ++ pwd_darwin.go | 3 ++ 18 files changed, 106 insertions(+), 59 deletions(-) create mode 100644 internal/zenutil/osascripts/common.gojs diff --git a/color_darwin.go b/color_darwin.go index 1abc599..a63e856 100644 --- a/color_darwin.go +++ b/color_darwin.go @@ -7,6 +7,11 @@ import ( ) func selectColor(opts options) (color.Color, error) { + var data zenutil.Color + if i, ok := opts.windowIcon.(string); ok { + data.WindowIcon = i + } + var col color.Color if opts.color != nil { col = opts.color @@ -14,12 +19,13 @@ func selectColor(opts options) (color.Color, error) { col = color.White } r, g, b, _ := col.RGBA() - - out, err := zenutil.Run(opts.ctx, "color", []float32{ + data.Color = [3]float32{ float32(r) / 0xffff, float32(g) / 0xffff, float32(b) / 0xffff, - }) + } + + out, err := zenutil.Run(opts.ctx, "color", data) str, err := strResult(opts, out, err) if err != nil { return nil, err diff --git a/date_darwin.go b/date_darwin.go index a97f11e..2c9954e 100644 --- a/date_darwin.go +++ b/date_darwin.go @@ -7,26 +7,29 @@ import ( ) func calendar(text string, opts options) (t time.Time, err error) { - var date zenutil.Date + var data zenutil.Date - date.OK, date.Cancel, date.Extra = getAlertButtons(opts) - date.Format, err = zenutil.DateUTS35() + data.OK, data.Cancel, data.Extra = getAlertButtons(opts) + data.Format, err = zenutil.DateUTS35() if err != nil { return } if opts.time != nil { unix := opts.time.Unix() - date.Date = &unix + data.Date = &unix } if opts.title != nil { - date.Text = *opts.title - date.Info = text + data.Text = *opts.title + data.Info = text } else { - date.Text = text + data.Text = text + } + if i, ok := opts.windowIcon.(string); ok { + data.WindowIcon = i } - out, err := zenutil.Run(opts.ctx, "date", date) + out, err := zenutil.Run(opts.ctx, "date", data) str, err := strResult(opts, out, err) if err != nil { return diff --git a/entry_darwin.go b/entry_darwin.go index 952cb6d..53a69a8 100644 --- a/entry_darwin.go +++ b/entry_darwin.go @@ -14,6 +14,9 @@ func entry(text string, opts options) (string, error) { data.Options.Answer = &opts.entryText data.Options.Hidden = opts.hideText data.Options.Timeout = zenutil.Timeout + if i, ok := opts.windowIcon.(string); ok { + data.WindowIcon = i + } switch i := opts.icon.(type) { case string: _, err := os.Stat(i) diff --git a/file_darwin.go b/file_darwin.go index 0b6edb8..62a2cbb 100644 --- a/file_darwin.go +++ b/file_darwin.go @@ -7,6 +7,9 @@ func selectFile(opts options) (string, error) { data.Options.Prompt = opts.title data.Options.Invisibles = opts.showHidden data.Options.Location, _ = splitDirAndName(opts.filename) + if i, ok := opts.windowIcon.(string); ok { + data.WindowIcon = i + } if opts.directory { data.Operation = "chooseFolder" @@ -26,6 +29,9 @@ func selectFileMultiple(opts options) ([]string, error) { data.Options.Location, _ = splitDirAndName(opts.filename) data.Options.Multiple = true data.Separator = zenutil.Separator + if i, ok := opts.windowIcon.(string); ok { + data.WindowIcon = i + } if opts.directory { data.Operation = "chooseFolder" @@ -43,6 +49,9 @@ func selectFileSave(opts options) (string, error) { data.Options.Prompt = opts.title data.Options.Invisibles = opts.showHidden data.Options.Location, data.Options.Name = splitDirAndName(opts.filename) + if i, ok := opts.windowIcon.(string); ok { + data.WindowIcon = i + } if opts.directory { data.Operation = "chooseFolder" diff --git a/internal/zenutil/osa_generated.go b/internal/zenutil/osa_generated.go index 598bd94..705847a 100644 --- a/internal/zenutil/osa_generated.go +++ b/internal/zenutil/osa_generated.go @@ -13,20 +13,31 @@ var scripts = template.Must(template.New("").Funcs(template.FuncMap{"json": func return string(b), err }}).Parse(` {{define "color" -}} -var app=Application.currentApplication() -app.includeStandardAdditions=true -app.activate() ObjC.import('stdio') ObjC.import('stdlib') -try{var res=app.chooseColor({defaultColor:{{json .}}})}catch(e){if(e.errorNumber===-128)$.exit(1) +{{template "common" .}} +try{var res=app.chooseColor({defaultColor:{{json .Color}}})}catch(e){if(e.errorNumber===-128)$.exit(1) $.dprintf(2,e) $.exit(-1)} {'rgb('+res.map(x=>Math.round(x*255))+')'} {{- end}} +{{define "common" -}} +{var app=Application.currentApplication() +app.includeStandardAdditions=true +{{- if .WindowIcon}} +ObjC.import('Cocoa') +let nsapp=$.NSApplication.sharedApplication +let nsimg=$.NSImage.alloc.initWithContentsOfFile({{json .WindowIcon}}) +nsapp.setActivationPolicy($.NSApplicationActivationPolicyRegular) +nsapp.setApplicationIconImage(nsimg) +{{- end}} +app.activate()} +{{- end}} {{define "date" -}} ObjC.import('Cocoa') ObjC.import('stdio') ObjC.import('stdlib') +{{template "common" .}} var date=$.NSDatePicker.alloc.init date.setDatePickerStyle($.NSDatePickerStyleClockAndCalendar) date.setDatePickerElements($.NSDatePickerElementFlagYearMonthDay) @@ -56,11 +67,9 @@ fmt.dateFormat={{json .Format}} fmt.stringFromDate(date.dateValue) {{- end}} {{define "dialog" -}} -var app=Application.currentApplication() -app.includeStandardAdditions=true -app.activate() ObjC.import('stdio') ObjC.import('stdlib') +{{template "common" .}} var opts={{json .Options}} {{- if .IconPath}} opts.withIcon=Path({{json .IconPath}}) @@ -74,9 +83,7 @@ $.exit(1)} res.textReturned {{- end}} {{define "file" -}} -var app=Application.currentApplication() -app.includeStandardAdditions=true -app.activate() +{{template "common" .}} ObjC.import('stdio') ObjC.import('stdlib') try{var res=app.{{.Operation}}({{json .Options}})}catch(e){if(e.errorNumber===-128)$.exit(1) @@ -85,9 +92,7 @@ $.exit(-1)} if(Array.isArray(res)){res.join({{json .Separator}})}else{res.toString()} {{- end}} {{define "list" -}} -var app=Application.currentApplication() -app.includeStandardAdditions=true -app.activate() +{{template "common" .}} ObjC.import('stdio') ObjC.import('stdlib') try{var res=app.chooseFromList({{json .Items}},{{json .Options}})}catch(e){$.dprintf(2,e) @@ -101,9 +106,7 @@ app.includeStandardAdditions=true void app.displayNotification({{json .Text}},{{json .Options}}) {{- end}} {{define "progress" -}} -var app=Application.currentApplication() -app.includeStandardAdditions=true -app.activate() +{{template "common" .}} ObjC.import('stdlib') ObjC.import('readline') {{- if .Total}} @@ -120,9 +123,7 @@ var i=parseInt(s) if(i>=0&&Progress.totalUnitCount>0){Progress.completedUnitCount=i}} {{- end}} {{define "pwd" -}} -var app=Application.currentApplication() -app.includeStandardAdditions=true -app.activate() +{{template "common" .}} ObjC.import('stdio') ObjC.import('stdlib') var opts={{json .Options}} diff --git a/internal/zenutil/osascripts/color.gojs b/internal/zenutil/osascripts/color.gojs index 4a54623..d0d3762 100644 --- a/internal/zenutil/osascripts/color.gojs +++ b/internal/zenutil/osascripts/color.gojs @@ -1,12 +1,9 @@ -var app = Application.currentApplication() -app.includeStandardAdditions = true -app.activate() - ObjC.import('stdio') ObjC.import('stdlib') +{{template "common" .}} try { - var res = app.chooseColor({defaultColor: {{json .}}}) + var res = app.chooseColor({defaultColor: {{json .Color}}}) } catch (e) { if (e.errorNumber === -128) $.exit(1) $.dprintf(2, e) diff --git a/internal/zenutil/osascripts/common.gojs b/internal/zenutil/osascripts/common.gojs new file mode 100644 index 0000000..2aa9e84 --- /dev/null +++ b/internal/zenutil/osascripts/common.gojs @@ -0,0 +1,14 @@ +{ + var app = Application.currentApplication() + app.includeStandardAdditions = true + + {{- if .WindowIcon}} + ObjC.import('Cocoa') + let nsapp = $.NSApplication.sharedApplication + let nsimg = $.NSImage.alloc.initWithContentsOfFile({{json .WindowIcon}}) + nsapp.setActivationPolicy($.NSApplicationActivationPolicyRegular) + nsapp.setApplicationIconImage(nsimg) + {{- end}} + + app.activate() +} \ No newline at end of file diff --git a/internal/zenutil/osascripts/date.gojs b/internal/zenutil/osascripts/date.gojs index de791c0..4cf2f5a 100644 --- a/internal/zenutil/osascripts/date.gojs +++ b/internal/zenutil/osascripts/date.gojs @@ -1,6 +1,7 @@ ObjC.import('Cocoa') ObjC.import('stdio') ObjC.import('stdlib') +{{template "common" .}} var date = $.NSDatePicker.alloc.init date.setDatePickerStyle($.NSDatePickerStyleClockAndCalendar) diff --git a/internal/zenutil/osascripts/dialog.gojs b/internal/zenutil/osascripts/dialog.gojs index 8ad51df..179b6e8 100644 --- a/internal/zenutil/osascripts/dialog.gojs +++ b/internal/zenutil/osascripts/dialog.gojs @@ -1,9 +1,6 @@ -var app = Application.currentApplication() -app.includeStandardAdditions = true -app.activate() - ObjC.import('stdio') ObjC.import('stdlib') +{{template "common" .}} var opts = {{json .Options}} {{- if .IconPath}} diff --git a/internal/zenutil/osascripts/file.gojs b/internal/zenutil/osascripts/file.gojs index 6cbbf18..283e1ca 100644 --- a/internal/zenutil/osascripts/file.gojs +++ b/internal/zenutil/osascripts/file.gojs @@ -1,7 +1,4 @@ -var app = Application.currentApplication() -app.includeStandardAdditions = true -app.activate() - +{{template "common" .}} ObjC.import('stdio') ObjC.import('stdlib') diff --git a/internal/zenutil/osascripts/list.gojs b/internal/zenutil/osascripts/list.gojs index b53948b..af89887 100644 --- a/internal/zenutil/osascripts/list.gojs +++ b/internal/zenutil/osascripts/list.gojs @@ -1,7 +1,4 @@ -var app = Application.currentApplication() -app.includeStandardAdditions = true -app.activate() - +{{template "common" .}} ObjC.import('stdio') ObjC.import('stdlib') diff --git a/internal/zenutil/osascripts/progress.gojs b/internal/zenutil/osascripts/progress.gojs index 59e42e1..aaacdb1 100644 --- a/internal/zenutil/osascripts/progress.gojs +++ b/internal/zenutil/osascripts/progress.gojs @@ -1,7 +1,4 @@ -var app = Application.currentApplication() -app.includeStandardAdditions = true -app.activate() - +{{template "common" .}} ObjC.import('stdlib') ObjC.import('readline') diff --git a/internal/zenutil/osascripts/pwd.gojs b/internal/zenutil/osascripts/pwd.gojs index 7a8fef9..d5d596e 100644 --- a/internal/zenutil/osascripts/pwd.gojs +++ b/internal/zenutil/osascripts/pwd.gojs @@ -1,7 +1,4 @@ -var app = Application.currentApplication() -app.includeStandardAdditions = true -app.activate() - +{{template "common" .}} ObjC.import('stdio') ObjC.import('stdlib') diff --git a/internal/zenutil/run_darwin.go b/internal/zenutil/run_darwin.go index 4d68525..ece8dc5 100644 --- a/internal/zenutil/run_darwin.go +++ b/internal/zenutil/run_darwin.go @@ -123,6 +123,10 @@ func RunProgress(ctx context.Context, max int, data Progress) (dlg *progressDial return dlg, nil } +type common struct { + WindowIcon string +} + // Dialog is internal. type Dialog struct { Operation string @@ -130,6 +134,7 @@ type Dialog struct { Extra *string Options DialogOptions IconPath string + common } // DialogOptions is internal. @@ -152,6 +157,7 @@ type Password struct { Extra *string Options PasswordOptions IconPath string + common } // PasswordOptions is internal. @@ -199,6 +205,7 @@ type List struct { Items []string Separator string Options ListOptions + common } // ListOptions is internal. @@ -217,6 +224,7 @@ type File struct { Operation string Separator string Options FileOptions + common } // FileOptions is internal. @@ -241,10 +249,10 @@ type NotifyOptions struct { Subtitle string `json:"subtitle,omitempty"` } -// Progress is internal. -type Progress struct { - Description *string - Total *int +// Color is internal. +type Color struct { + Color [3]float32 + common } // Date is internal. @@ -256,4 +264,12 @@ type Date struct { OK string Cancel string Extra *string + common +} + +// Progress is internal. +type Progress struct { + Description *string + Total *int + common } diff --git a/list_darwin.go b/list_darwin.go index e4069c8..cd89708 100644 --- a/list_darwin.go +++ b/list_darwin.go @@ -22,6 +22,9 @@ func list(text string, items []string, opts options) (string, error) { data.Options.Cancel = opts.cancelLabel data.Options.Default = opts.defaultItems data.Options.Empty = !opts.disallowEmpty + if i, ok := opts.windowIcon.(string); ok { + data.WindowIcon = i + } out, err := zenutil.Run(opts.ctx, "list", data) return strResult(opts, out, err) diff --git a/msg_darwin.go b/msg_darwin.go index 22b7628..3e0edd2 100644 --- a/msg_darwin.go +++ b/msg_darwin.go @@ -10,6 +10,9 @@ func message(kind messageKind, text string, opts options) error { var data zenutil.Dialog data.Text = text data.Options.Timeout = zenutil.Timeout + if i, ok := opts.windowIcon.(string); ok { + data.WindowIcon = i + } // dialog is more flexible, alert prettier var dialog bool diff --git a/progress_darwin.go b/progress_darwin.go index c8e3b82..0c16700 100644 --- a/progress_darwin.go +++ b/progress_darwin.go @@ -19,6 +19,9 @@ func progress(opts options) (ProgressDialog, error) { if opts.maxValue >= 0 { data.Total = &opts.maxValue } + if i, ok := opts.windowIcon.(string); ok { + data.WindowIcon = i + } return zenutil.RunProgress(opts.ctx, opts.maxValue, data) } diff --git a/pwd_darwin.go b/pwd_darwin.go index c9ce289..cb5688c 100644 --- a/pwd_darwin.go +++ b/pwd_darwin.go @@ -18,6 +18,9 @@ func password(opts options) (string, string, error) { data.Separator = zenutil.Separator data.Options.Title = opts.title data.Options.Timeout = zenutil.Timeout + if i, ok := opts.windowIcon.(string); ok { + data.WindowIcon = i + } switch i := opts.icon.(type) { case string: _, err := os.Stat(i)