Improved file selection (windows).

This commit is contained in:
Nuno Cruces 2020-01-09 17:05:43 +00:00
parent 77f78ba34b
commit c212fdeab7
2 changed files with 31 additions and 9 deletions

15
file.go Normal file
View file

@ -0,0 +1,15 @@
package zenity
import (
"os"
"path/filepath"
)
func splitDirAndName(path string) (dir, name string) {
path = filepath.Clean(path)
fi, err := os.Stat(path)
if err == nil && fi.IsDir() {
return path, ""
}
return filepath.Split(path)
}

View file

@ -28,9 +28,6 @@ func SelectFile(options ...Option) (string, error) {
if opts.title != "" { if opts.title != "" {
args.Title = syscall.StringToUTF16Ptr(opts.title) args.Title = syscall.StringToUTF16Ptr(opts.title)
} }
if opts.filename != "" {
args.InitialDir = syscall.StringToUTF16Ptr(opts.filename)
}
if opts.filters != nil { if opts.filters != nil {
args.Filter = &windowsFilters(opts.filters)[0] args.Filter = &windowsFilters(opts.filters)[0]
} }
@ -38,6 +35,7 @@ func SelectFile(options ...Option) (string, error) {
res := [32768]uint16{} res := [32768]uint16{}
args.File = &res[0] args.File = &res[0]
args.MaxFile = uint32(len(res)) args.MaxFile = uint32(len(res))
args.InitialDir = initDirAndName(opts.filename, res[:])
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
@ -58,9 +56,6 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
if opts.title != "" { if opts.title != "" {
args.Title = syscall.StringToUTF16Ptr(opts.title) args.Title = syscall.StringToUTF16Ptr(opts.title)
} }
if opts.filename != "" {
args.InitialDir = syscall.StringToUTF16Ptr(opts.filename)
}
if opts.filters != nil { if opts.filters != nil {
args.Filter = &windowsFilters(opts.filters)[0] args.Filter = &windowsFilters(opts.filters)[0]
} }
@ -68,6 +63,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
res := [32768 + 1024*256]uint16{} res := [32768 + 1024*256]uint16{}
args.File = &res[0] args.File = &res[0]
args.MaxFile = uint32(len(res)) args.MaxFile = uint32(len(res))
args.InitialDir = initDirAndName(opts.filename, res[:])
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
@ -113,9 +109,6 @@ func SelectFileSave(options ...Option) (string, error) {
if opts.title != "" { if opts.title != "" {
args.Title = syscall.StringToUTF16Ptr(opts.title) args.Title = syscall.StringToUTF16Ptr(opts.title)
} }
if opts.filename != "" {
args.InitialDir = syscall.StringToUTF16Ptr(opts.filename)
}
if opts.overwrite { if opts.overwrite {
args.Flags |= 0x2 // OFN_OVERWRITEPROMPT args.Flags |= 0x2 // OFN_OVERWRITEPROMPT
} }
@ -126,6 +119,7 @@ func SelectFileSave(options ...Option) (string, error) {
res := [32768]uint16{} res := [32768]uint16{}
args.File = &res[0] args.File = &res[0]
args.MaxFile = uint32(len(res)) args.MaxFile = uint32(len(res))
args.InitialDir = initDirAndName(opts.filename, res[:])
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
@ -237,6 +231,19 @@ func browseForFolder(title string) (string, error) {
return syscall.UTF16ToString(res[:]), nil return syscall.UTF16ToString(res[:]), nil
} }
func initDirAndName(filename string, name []uint16) (dir *uint16) {
if filename != "" {
d, n := splitDirAndName(filename)
if n != "" {
copy(name, syscall.StringToUTF16(n))
}
if d != "" {
return syscall.StringToUTF16Ptr(d)
}
}
return nil
}
func windowsFilters(filters []FileFilter) []uint16 { func windowsFilters(filters []FileFilter) []uint16 {
var res []uint16 var res []uint16
for _, f := range filters { for _, f := range filters {