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

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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