Window icons (macOS).

This commit is contained in:
Nuno Cruces 2022-06-02 00:00:08 +01:00
parent 550da1f8ed
commit ab7621bec9
18 changed files with 106 additions and 59 deletions

View file

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

View file

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

View file

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

View file

@ -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"

View file

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

View file

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

View file

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

View file

@ -1,6 +1,7 @@
ObjC.import('Cocoa')
ObjC.import('stdio')
ObjC.import('stdlib')
{{template "common" .}}
var date = $.NSDatePicker.alloc.init
date.setDatePickerStyle($.NSDatePickerStyleClockAndCalendar)

View file

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

View file

@ -1,7 +1,4 @@
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
{{template "common" .}}
ObjC.import('stdio')
ObjC.import('stdlib')

View file

@ -1,7 +1,4 @@
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
{{template "common" .}}
ObjC.import('stdio')
ObjC.import('stdlib')

View file

@ -1,7 +1,4 @@
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
{{template "common" .}}
ObjC.import('stdlib')
ObjC.import('readline')

View file

@ -1,7 +1,4 @@
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
{{template "common" .}}
ObjC.import('stdio')
ObjC.import('stdlib')

View file

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

View file

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

View file

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

View file

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

View file

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