diff --git a/dialog/file_darwin.go b/dialog/file_darwin.go new file mode 100644 index 0000000000..63876f83dc --- /dev/null +++ b/dialog/file_darwin.go @@ -0,0 +1,19 @@ +package dialog + +import ( + "os" + "path/filepath" +) + +func getFavoriteLocations() (map[string]string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return nil, err + } + + return map[string]string{ + "Home": homeDir, + "Documents": filepath.Join(homeDir, "Documents"), + "Downloads": filepath.Join(homeDir, "Downloads"), + }, nil +} diff --git a/dialog/file_linux.go b/dialog/file_linux.go new file mode 100644 index 0000000000..8aa8bd39cc --- /dev/null +++ b/dialog/file_linux.go @@ -0,0 +1,65 @@ +package dialog + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "path/filepath" +) + +func getFavoriteLocationLinux(homeDir, name string) (string, error) { + cmdName := "xdg-user-dir" + fallback := filepath.Join(homeDir, name) + + if _, err := exec.LookPath(cmdName); err != nil { + return fallback, fmt.Errorf("%s not found in PATH. using fallback paths", cmdName) + } + + cmd := exec.Command(cmdName, name) + stdout := bytes.NewBufferString("") + + cmd.Stdout = stdout + err := cmd.Run() + if err != nil { + return fallback, err + } + + loc := stdout.String() + // Remove \n at the end + loc = loc[:len(loc)-1] + + if loc == homeDir { + return fallback, fmt.Errorf("this computer does not have a %s folder", name) + } + + return stdout.String(), nil +} + +func getFavoriteLocations() (map[string]string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return nil, err + } + + rv := map[string]string{ + "Home": homeDir, + } + var err error + documentsDir, err1 := getFavoriteLocationLinux(homeDir, "DOCUMENTS") + if err == nil { + rv["Documents"] = documentsDir + } else { + rv["Documents"] = filepath.Join(homeDir, "Documents") + err = err1 + } + downloadsDir, err1 := getFavoriteLocationLinux(homeDir, "DOWNLOADS") + if err == nil { + rv["Downloads"] = downloadsDir + } else { + rv["Downloads"] = filepath.Join(homeDir, "Downloads") + err = err1 + } + + return rv, err +} diff --git a/dialog/file_other.go b/dialog/file_other.go deleted file mode 100644 index 9d4da0e05b..0000000000 --- a/dialog/file_other.go +++ /dev/null @@ -1,97 +0,0 @@ -// +build !windows,!android,!ios - -package dialog - -import ( - "bytes" - "fmt" - "os" - "os/exec" - "path/filepath" - "runtime" - - "fyne.io/fyne" - "fyne.io/fyne/theme" -) - -func (f *fileDialog) loadPlaces() []fyne.CanvasObject { - return []fyne.CanvasObject{makeFavoriteButton("Computer", theme.ComputerIcon(), func() { - f.setDirectory("/") - })} -} - -func isHidden(file, _ string) bool { - return len(file) == 0 || file[0] == '.' -} - -func fileOpenOSOverride(*FileDialog) bool { - return false -} - -func fileSaveOSOverride(*FileDialog) bool { - return false -} - -func getFavoriteLocationLinux(homeDir, name string) (string, error) { - cmdName := "xdg-user-dir" - fallback := filepath.Join(homeDir, name) - - if _, err := exec.LookPath(cmdName); err != nil { - return fallback, fmt.Errorf("%s not found in PATH. using fallback paths", cmdName) - } - - cmd := exec.Command(cmdName, name) - stdout := bytes.NewBufferString("") - - cmd.Stdout = stdout - err := cmd.Run() - if err != nil { - return fallback, err - } - - loc := stdout.String() - // Remove \n at the end - loc = loc[:len(loc)-1] - - if loc == homeDir { - return fallback, fmt.Errorf("this computer does not have a %s folder", name) - } - - return stdout.String(), nil -} - -func getFavoriteLocations() (map[string]string, error) { - homeDir, err := os.UserHomeDir() - if err != nil { - return nil, err - } - - if runtime.GOOS == "linux" { - rv := map[string]string{ - "Home": homeDir, - } - var err error - documentsDir, err1 := getFavoriteLocationLinux(homeDir, "DOCUMENTS") - if err == nil { - rv["Documents"] = documentsDir - } else { - rv["Documents"] = filepath.Join(homeDir, "Documents") - err = err1 - } - downloadsDir, err1 := getFavoriteLocationLinux(homeDir, "DOWNLOADS") - if err == nil { - rv["Downloads"] = downloadsDir - } else { - rv["Downloads"] = filepath.Join(homeDir, "Downloads") - err = err1 - } - - return rv, err - } - - return map[string]string{ - "Home": homeDir, - "Documents": filepath.Join(homeDir, "Documents"), - "Downloads": filepath.Join(homeDir, "Downloads"), - }, nil -} diff --git a/dialog/file_unix.go b/dialog/file_unix.go new file mode 100644 index 0000000000..368737befd --- /dev/null +++ b/dialog/file_unix.go @@ -0,0 +1,26 @@ +// +build !windows,!android,!ios + +package dialog + +import ( + "fyne.io/fyne" + "fyne.io/fyne/theme" +) + +func (f *fileDialog) loadPlaces() []fyne.CanvasObject { + return []fyne.CanvasObject{makeFavoriteButton("Computer", theme.ComputerIcon(), func() { + f.setDirectory("/") + })} +} + +func isHidden(file, _ string) bool { + return len(file) == 0 || file[0] == '.' +} + +func fileOpenOSOverride(*FileDialog) bool { + return false +} + +func fileSaveOSOverride(*FileDialog) bool { + return false +}