Skip to content

Commit

Permalink
Seperate plat code of dialog into different files
Browse files Browse the repository at this point in the history
  • Loading branch information
PucklaJ committed Oct 2, 2020
1 parent 1a8ee43 commit 943c82a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 97 deletions.
19 changes: 19 additions & 0 deletions 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
}
65 changes: 65 additions & 0 deletions 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
}
97 changes: 0 additions & 97 deletions dialog/file_other.go

This file was deleted.

26 changes: 26 additions & 0 deletions 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
}

0 comments on commit 943c82a

Please sign in to comment.