File selection improvements (macos).

This commit is contained in:
Nuno Cruces 2020-01-12 23:09:11 +00:00
parent f68fa5b0ba
commit 099c0c4851
9 changed files with 42 additions and 30 deletions

View file

@ -13,12 +13,13 @@ func SelectFile(options ...Option) (string, error) {
data := osa.File{ data := osa.File{
Prompt: opts.title, Prompt: opts.title,
Invisibles: opts.hidden,
} }
if opts.directory { if opts.directory {
data.Operation = "chooseFolder" data.Operation = "chooseFolder"
} else { } else {
data.Operation = "chooseFile" data.Operation = "chooseFile"
data.Type = appleFilters(opts.filters) data.Type = initFilters(opts.filters)
} }
data.Location, _ = splitDirAndName(opts.filename) data.Location, _ = splitDirAndName(opts.filename)
@ -39,15 +40,16 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options) opts := optsParse(options)
data := osa.File{ data := osa.File{
Multiple: true,
Prompt: opts.title, Prompt: opts.title,
Invisibles: opts.hidden,
Multiple: true,
Separator: cmd.Separator, Separator: cmd.Separator,
} }
if opts.directory { if opts.directory {
data.Operation = "chooseFolder" data.Operation = "chooseFolder"
} else { } else {
data.Operation = "chooseFile" data.Operation = "chooseFile"
data.Type = appleFilters(opts.filters) data.Type = initFilters(opts.filters)
} }
data.Location, _ = splitDirAndName(opts.filename) data.Location, _ = splitDirAndName(opts.filename)
@ -71,9 +73,13 @@ func SelectFileSave(options ...Option) (string, error) {
opts := optsParse(options) opts := optsParse(options)
data := osa.File{ data := osa.File{
Operation: "chooseFileName",
Prompt: opts.title, Prompt: opts.title,
Type: appleFilters(opts.filters), }
if opts.directory {
data.Operation = "chooseFolder"
} else {
data.Operation = "chooseFileName"
data.Type = initFilters(opts.filters)
} }
data.Location, data.Name = splitDirAndName(opts.filename) data.Location, data.Name = splitDirAndName(opts.filename)
@ -90,7 +96,7 @@ func SelectFileSave(options ...Option) (string, error) {
return string(out), nil return string(out), nil
} }
func appleFilters(filters []FileFilter) []string { func initFilters(filters []FileFilter) []string {
var filter []string var filter []string
for _, f := range filters { for _, f := range filters {
for _, p := range f.Patterns { for _, p := range f.Patterns {

View file

@ -28,7 +28,7 @@ func SelectFile(options ...Option) (string, error) {
if opts.filename != "" { if opts.filename != "" {
args = append(args, "--filename", 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) out, err := zen.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
@ -61,7 +61,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
if opts.filename != "" { if opts.filename != "" {
args = append(args, "--filename", 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) out, err := zen.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
@ -85,6 +85,9 @@ func SelectFileSave(options ...Option) (string, error) {
opts := optsParse(options) opts := optsParse(options)
args := []string{"--file-selection", "--save"} args := []string{"--file-selection", "--save"}
if opts.directory {
args = append(args, "--directory")
}
if opts.title != "" { if opts.title != "" {
args = append(args, "--title", opts.title) args = append(args, "--title", opts.title)
} }
@ -94,7 +97,7 @@ func SelectFileSave(options ...Option) (string, error) {
if opts.overwrite { if opts.overwrite {
args = append(args, "--confirm-overwrite") args = append(args, "--confirm-overwrite")
} }
args = append(args, zenityFilters(opts.filters)...) args = append(args, initFilters(opts.filters)...)
out, err := zen.Run(args) out, err := zen.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
@ -109,7 +112,7 @@ func SelectFileSave(options ...Option) (string, error) {
return string(out), nil return string(out), nil
} }
func zenityFilters(filters []FileFilter) []string { func initFilters(filters []FileFilter) []string {
var res []string var res []string
for _, f := range filters { for _, f := range filters {
var buf strings.Builder var buf strings.Builder

View file

@ -35,7 +35,7 @@ func SelectFile(options ...Option) (string, error) {
args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN
} }
if opts.filters != nil { if opts.filters != nil {
args.Filter = &windowsFilters(opts.filters)[0] args.Filter = &initFilters(opts.filters)[0]
} }
res := [32768]uint16{} res := [32768]uint16{}
@ -71,7 +71,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN
} }
if opts.filters != nil { if opts.filters != nil {
args.Filter = &windowsFilters(opts.filters)[0] args.Filter = &initFilters(opts.filters)[0]
} }
res := [32768 + 1024*256]uint16{} res := [32768 + 1024*256]uint16{}
@ -138,7 +138,7 @@ func SelectFileSave(options ...Option) (string, error) {
args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN
} }
if opts.filters != nil { if opts.filters != nil {
args.Filter = &windowsFilters(opts.filters)[0] args.Filter = &initFilters(opts.filters)[0]
} }
res := [32768]uint16{} res := [32768]uint16{}
@ -313,7 +313,7 @@ func initDirNameExt(filename string, name []uint16) (dir *uint16, ext *uint16) {
return return
} }
func windowsFilters(filters []FileFilter) []uint16 { func initFilters(filters []FileFilter) []uint16 {
var res []uint16 var res []uint16
for _, f := range filters { for _, f := range filters {
res = append(res, utf16.Encode([]rune(f.Name))...) res = append(res, utf16.Encode([]rune(f.Name))...)

View file

@ -11,6 +11,7 @@ app.includeStandardAdditions = true
app.activate() app.activate()
var opts = {} var opts = {}
opts.withPrompt = {{.Prompt}} opts.withPrompt = {{.Prompt}}
opts.invisibles = {{.Invisibles}}
opts.multipleSelectionsAllowed = {{.Multiple}} opts.multipleSelectionsAllowed = {{.Multiple}}
{{if .Type -}} {{if .Type -}}
opts.ofType = {{.Type}} opts.ofType = {{.Type}}

View file

@ -40,6 +40,7 @@ type File struct {
Location string Location string
Separator string Separator string
Type []string Type []string
Invisibles bool
Multiple bool Multiple bool
} }

View file

@ -4,6 +4,7 @@ app.activate()
var opts = {} var opts = {}
opts.withPrompt = {{.Prompt}} opts.withPrompt = {{.Prompt}}
opts.invisibles = {{.Invisibles}}
opts.multipleSelectionsAllowed = {{.Multiple}} opts.multipleSelectionsAllowed = {{.Multiple}}
{{if .Type -}} {{if .Type -}}

View file

@ -34,10 +34,10 @@ func message(typ int, text string, options []Option) (bool, error) {
switch opts.icon { switch opts.icon {
case ErrorIcon: case ErrorIcon:
data.Icon = "stop" data.Icon = "stop"
case InfoIcon, QuestionIcon:
data.Icon = "note"
case WarningIcon: case WarningIcon:
data.Icon = "caution" data.Icon = "caution"
case InfoIcon, QuestionIcon:
data.Icon = "note"
} }
} else { } else {
data.Operation = "displayAlert" data.Operation = "displayAlert"

View file

@ -76,12 +76,12 @@ func message(arg, text string, options []Option) (bool, error) {
switch opts.icon { switch opts.icon {
case ErrorIcon: case ErrorIcon:
args = append(args, "--icon-name=dialog-error") args = append(args, "--icon-name=dialog-error")
case WarningIcon:
args = append(args, "--icon-name=dialog-warning")
case InfoIcon: case InfoIcon:
args = append(args, "--icon-name=dialog-information") args = append(args, "--icon-name=dialog-information")
case QuestionIcon: case QuestionIcon:
args = append(args, "--icon-name=dialog-question") args = append(args, "--icon-name=dialog-question")
case WarningIcon:
args = append(args, "--icon-name=dialog-warning")
} }
out, err := zen.Run(args) out, err := zen.Run(args)

View file

@ -111,9 +111,9 @@ type MessageIcon int
const ( const (
ErrorIcon MessageIcon = iota + 1 ErrorIcon MessageIcon = iota + 1
WarningIcon
InfoIcon InfoIcon
QuestionIcon QuestionIcon
WarningIcon
) )
// Option to set the dialog icon. // Option to set the dialog icon.