From 099c0c485132841b99f905840521df9dc4ecc618 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sun, 12 Jan 2020 23:09:11 +0000 Subject: [PATCH] File selection improvements (macos). --- file_darwin.go | 26 ++++++++++++++++---------- file_unix.go | 11 +++++++---- file_windows.go | 8 ++++---- internal/osa/generated.go | 1 + internal/osa/osa_darwin.go | 15 ++++++++------- internal/osa/scripts/file.gots | 1 + msg_darwin.go | 4 ++-- msg_unix.go | 4 ++-- zenity.go | 2 +- 9 files changed, 42 insertions(+), 30 deletions(-) diff --git a/file_darwin.go b/file_darwin.go index e2bc9cb..e8463f8 100644 --- a/file_darwin.go +++ b/file_darwin.go @@ -12,13 +12,14 @@ func SelectFile(options ...Option) (string, error) { opts := optsParse(options) data := osa.File{ - Prompt: opts.title, + Prompt: opts.title, + Invisibles: opts.hidden, } if opts.directory { data.Operation = "chooseFolder" } else { data.Operation = "chooseFile" - data.Type = appleFilters(opts.filters) + data.Type = initFilters(opts.filters) } data.Location, _ = splitDirAndName(opts.filename) @@ -39,15 +40,16 @@ func SelectFileMutiple(options ...Option) ([]string, error) { opts := optsParse(options) data := osa.File{ - Multiple: true, - Prompt: opts.title, - Separator: cmd.Separator, + Prompt: opts.title, + Invisibles: opts.hidden, + Multiple: true, + Separator: cmd.Separator, } if opts.directory { data.Operation = "chooseFolder" } else { data.Operation = "chooseFile" - data.Type = appleFilters(opts.filters) + data.Type = initFilters(opts.filters) } data.Location, _ = splitDirAndName(opts.filename) @@ -71,9 +73,13 @@ func SelectFileSave(options ...Option) (string, error) { opts := optsParse(options) data := osa.File{ - Operation: "chooseFileName", - Prompt: opts.title, - Type: appleFilters(opts.filters), + Prompt: opts.title, + } + if opts.directory { + data.Operation = "chooseFolder" + } else { + data.Operation = "chooseFileName" + data.Type = initFilters(opts.filters) } data.Location, data.Name = splitDirAndName(opts.filename) @@ -90,7 +96,7 @@ func SelectFileSave(options ...Option) (string, error) { return string(out), nil } -func appleFilters(filters []FileFilter) []string { +func initFilters(filters []FileFilter) []string { var filter []string for _, f := range filters { for _, p := range f.Patterns { diff --git a/file_unix.go b/file_unix.go index 0c10f80..5498814 100644 --- a/file_unix.go +++ b/file_unix.go @@ -28,7 +28,7 @@ func SelectFile(options ...Option) (string, error) { if opts.filename != "" { args = append(args, "--filename", opts.filename) } - args = append(args, zenityFilters(opts.filters)...) + args = append(args, initFilters(opts.filters)...) out, err := zen.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { @@ -61,7 +61,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) { if opts.filename != "" { args = append(args, "--filename", opts.filename) } - args = append(args, zenityFilters(opts.filters)...) + args = append(args, initFilters(opts.filters)...) out, err := zen.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { @@ -85,6 +85,9 @@ func SelectFileSave(options ...Option) (string, error) { opts := optsParse(options) args := []string{"--file-selection", "--save"} + if opts.directory { + args = append(args, "--directory") + } if opts.title != "" { args = append(args, "--title", opts.title) } @@ -94,7 +97,7 @@ func SelectFileSave(options ...Option) (string, error) { if opts.overwrite { args = append(args, "--confirm-overwrite") } - args = append(args, zenityFilters(opts.filters)...) + args = append(args, initFilters(opts.filters)...) out, err := zen.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { @@ -109,7 +112,7 @@ func SelectFileSave(options ...Option) (string, error) { return string(out), nil } -func zenityFilters(filters []FileFilter) []string { +func initFilters(filters []FileFilter) []string { var res []string for _, f := range filters { var buf strings.Builder diff --git a/file_windows.go b/file_windows.go index b5c5596..25a406f 100644 --- a/file_windows.go +++ b/file_windows.go @@ -35,7 +35,7 @@ func SelectFile(options ...Option) (string, error) { args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN } if opts.filters != nil { - args.Filter = &windowsFilters(opts.filters)[0] + args.Filter = &initFilters(opts.filters)[0] } res := [32768]uint16{} @@ -71,7 +71,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) { args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN } if opts.filters != nil { - args.Filter = &windowsFilters(opts.filters)[0] + args.Filter = &initFilters(opts.filters)[0] } res := [32768 + 1024*256]uint16{} @@ -138,7 +138,7 @@ func SelectFileSave(options ...Option) (string, error) { args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN } if opts.filters != nil { - args.Filter = &windowsFilters(opts.filters)[0] + args.Filter = &initFilters(opts.filters)[0] } res := [32768]uint16{} @@ -313,7 +313,7 @@ func initDirNameExt(filename string, name []uint16) (dir *uint16, ext *uint16) { return } -func windowsFilters(filters []FileFilter) []uint16 { +func initFilters(filters []FileFilter) []uint16 { var res []uint16 for _, f := range filters { res = append(res, utf16.Encode([]rune(f.Name))...) diff --git a/internal/osa/generated.go b/internal/osa/generated.go index 1ab6337..bd622e7 100644 --- a/internal/osa/generated.go +++ b/internal/osa/generated.go @@ -11,6 +11,7 @@ app.includeStandardAdditions = true app.activate() var opts = {} opts.withPrompt = {{.Prompt}} +opts.invisibles = {{.Invisibles}} opts.multipleSelectionsAllowed = {{.Multiple}} {{if .Type -}} opts.ofType = {{.Type}} diff --git a/internal/osa/osa_darwin.go b/internal/osa/osa_darwin.go index f7a3439..148b0e7 100644 --- a/internal/osa/osa_darwin.go +++ b/internal/osa/osa_darwin.go @@ -34,13 +34,14 @@ func Run(script string, data interface{}) ([]byte, error) { } type File struct { - Operation string - Prompt string - Name string - Location string - Separator string - Type []string - Multiple bool + Operation string + Prompt string + Name string + Location string + Separator string + Type []string + Invisibles bool + Multiple bool } type Msg struct { diff --git a/internal/osa/scripts/file.gots b/internal/osa/scripts/file.gots index b0430fe..afb141c 100644 --- a/internal/osa/scripts/file.gots +++ b/internal/osa/scripts/file.gots @@ -4,6 +4,7 @@ app.activate() var opts = {} opts.withPrompt = {{.Prompt}} +opts.invisibles = {{.Invisibles}} opts.multipleSelectionsAllowed = {{.Multiple}} {{if .Type -}} diff --git a/msg_darwin.go b/msg_darwin.go index 837ef51..f7195ee 100644 --- a/msg_darwin.go +++ b/msg_darwin.go @@ -34,10 +34,10 @@ func message(typ int, text string, options []Option) (bool, error) { switch opts.icon { case ErrorIcon: data.Icon = "stop" - case InfoIcon, QuestionIcon: - data.Icon = "note" case WarningIcon: data.Icon = "caution" + case InfoIcon, QuestionIcon: + data.Icon = "note" } } else { data.Operation = "displayAlert" diff --git a/msg_unix.go b/msg_unix.go index e125efd..b9071dc 100644 --- a/msg_unix.go +++ b/msg_unix.go @@ -76,12 +76,12 @@ func message(arg, text string, options []Option) (bool, error) { switch opts.icon { case ErrorIcon: args = append(args, "--icon-name=dialog-error") + case WarningIcon: + args = append(args, "--icon-name=dialog-warning") case InfoIcon: args = append(args, "--icon-name=dialog-information") case QuestionIcon: args = append(args, "--icon-name=dialog-question") - case WarningIcon: - args = append(args, "--icon-name=dialog-warning") } out, err := zen.Run(args) diff --git a/zenity.go b/zenity.go index 40a9608..f319cd9 100644 --- a/zenity.go +++ b/zenity.go @@ -111,9 +111,9 @@ type MessageIcon int const ( ErrorIcon MessageIcon = iota + 1 + WarningIcon InfoIcon QuestionIcon - WarningIcon ) // Option to set the dialog icon.