Skip to content

Commit

Permalink
Add getFavoriteLocations function
Browse files Browse the repository at this point in the history
  • Loading branch information
PucklaJ committed Sep 30, 2020
1 parent 07a9231 commit 8751553
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 11 deletions.
32 changes: 21 additions & 11 deletions dialog/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,27 @@ func (f *fileDialog) makeUI() fyne.CanvasObject {
}

func (f *fileDialog) loadFavorites() []fyne.CanvasObject {
home, _ := os.UserHomeDir()
places := []fyne.CanvasObject{
makeFavoriteButton("Home", theme.HomeIcon(), func() {
f.setDirectory(home)
}),
makeFavoriteButton("Documents", theme.DocumentIcon(), func() {
f.setDirectory(filepath.Join(home, "Documents"))
}),
makeFavoriteButton("Downloads", theme.DownloadIcon(), func() {
f.setDirectory(filepath.Join(home, "Downloads"))
}),
favoriteLocations, err := getFavoriteLocations()
if err != nil {
fyne.LogError("Getting Favorite locations", err)
}

var places []fyne.CanvasObject

if loc, ok := favoriteLocations["Home"]; ok {
places = append(places, makeFavoriteButton("Home", theme.HomeIcon(), func() {
f.setDirectory(loc)
}))
}
if loc, ok := favoriteLocations["Documents"]; ok {
places = append(places, makeFavoriteButton("Documents", theme.DocumentIcon(), func() {
f.setDirectory(loc)
}))
}
if loc, ok := favoriteLocations["Downloads"]; ok {
places = append(places, makeFavoriteButton("Downloads", theme.DownloadIcon(), func() {
f.setDirectory(loc)
}))
}

places = append(places, f.loadPlaces()...)
Expand Down
16 changes: 16 additions & 0 deletions dialog/file_mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
package dialog

import (
"os"
"path/filepath"

"fyne.io/fyne"
"fyne.io/fyne/internal/driver/gomobile"
)
Expand Down Expand Up @@ -30,3 +33,16 @@ func fileSaveOSOverride(f *FileDialog) bool {

return true
}

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
}
55 changes: 55 additions & 0 deletions dialog/file_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
package dialog

import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"

"fyne.io/fyne"
"fyne.io/fyne/theme"
)
Expand All @@ -24,3 +30,52 @@ func fileOpenOSOverride(*FileDialog) bool {
func fileSaveOSOverride(*FileDialog) bool {
return false
}

func getFavoriteLocationLinux(name string) (string, error) {
cmd := exec.Command("xdg-user-dir", name)
stdout := bytes.NewBufferString("")

cmd.Stdout = stdout
err := cmd.Run()
if err != nil {
return "", err
}

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("DOCUMENTS")
if err == nil {
rv["Documents"] = documentsDir
} else {
rv["Documents"] = filepath.Join(homeDir, "Documents")
err = err1
}
downloadsDir, err1 := getFavoriteLocationLinux("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
}
13 changes: 13 additions & 0 deletions dialog/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,16 @@ func fileOpenOSOverride(*FileDialog) bool {
func fileSaveOSOverride(*FileDialog) bool {
return false
}

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
}

0 comments on commit 8751553

Please sign in to comment.