Skip to content

Commit

Permalink
Merge pull request #1 from PucklaMotzer09/file_dialog_locations_fixes
Browse files Browse the repository at this point in the history
Fix some issues related to ListableURI and dialog
  • Loading branch information
charlesdaniels committed Sep 21, 2020
2 parents d46f9ff + 9c055c7 commit f3e0660
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
32 changes: 29 additions & 3 deletions dialog/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"fyne.io/fyne"
Expand Down Expand Up @@ -83,7 +84,16 @@ func (f *fileDialog) makeUI() fyne.CanvasObject {
name := f.fileName.(*widget.Entry).Text
path, _ := storage.Child(f.dir, name)

// On windows replace '\\' with '/'
if runtime.GOOS == "windows" {
pathString := path.String()
pathString = strings.ReplaceAll(pathString, "\\\\", "/")
pathString = strings.ReplaceAll(pathString, "\\", "/")
path = storage.NewURI(pathString)
}

exists, _ := storage.Exists(path)

_, err := storage.ListerForURI(path)

if !exists {
Expand Down Expand Up @@ -119,7 +129,15 @@ func (f *fileDialog) makeUI() fyne.CanvasObject {
if f.file.onClosedCallback != nil {
f.file.onClosedCallback(true)
}
callback(storage.OpenFileFromURI(f.selected.location))
path := f.selected.location
// On windows replace '\\' with '/'
if runtime.GOOS == "windows" {
pathString := path.String()
pathString = strings.ReplaceAll(pathString, "\\\\", "/")
pathString = strings.ReplaceAll(pathString, "\\", "/")
path = storage.NewURI(pathString)
}
callback(storage.OpenFileFromURI(path))
}
})
f.open.Style = widget.PrimaryButton
Expand Down Expand Up @@ -230,11 +248,11 @@ func (f *fileDialog) refreshDir(dir fyne.ListableURI) {

var icons []fyne.CanvasObject
parent, err := storage.Parent(dir)
if err != nil {
if err != nil && err != storage.URIRootError {
fyne.LogError("Unable to get parent of "+dir.String(), err)
return
}
if parent.String() != dir.String() {
if parent != nil && parent.String() != dir.String() {
fi := &fileDialogItem{picker: f,
name: "(Parent)", location: parent, dir: true}
fi.ExtendBaseWidget(fi)
Expand Down Expand Up @@ -262,6 +280,10 @@ func (f *fileDialog) refreshDir(dir fyne.ListableURI) {
}

func (f *fileDialog) setDirectory(dir fyne.ListableURI) error {
if dir == nil {
return fmt.Errorf("failed to open nil directory")
}

f.setSelected(nil)
f.dir = dir

Expand All @@ -272,6 +294,10 @@ func (f *fileDialog) setDirectory(dir fyne.ListableURI) error {
}

localdir := dir.String()[len(dir.Scheme())+3:]
if runtime.GOOS == "windows" {
localdir = strings.ReplaceAll(localdir, "/", "\\")
}

buildDir := filepath.VolumeName(localdir)
for i, d := range strings.Split(localdir, string(filepath.Separator)) {
if d == "" {
Expand Down
11 changes: 10 additions & 1 deletion dialog/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -208,7 +209,15 @@ func TestShowFileOpen(t *testing.T) {
test.Tap(open)
assert.Nil(t, win.Canvas().Overlays().Top())
assert.Nil(t, openErr)
assert.Equal(t, target.location.String(), chosen.URI().String())

targetLocationString := target.location.String()
// Replace all '\\' with '/' on windows since the dialog
// will return a URI with only '/'
if runtime.GOOS == "windows" {
targetLocationString = strings.ReplaceAll(targetLocationString, "\\\\", "/")
targetLocationString = strings.ReplaceAll(targetLocationString, "\\", "/")
}
assert.Equal(t, targetLocationString, chosen.URI().String())

err := chosen.Close()
assert.Nil(t, err)
Expand Down
5 changes: 5 additions & 0 deletions storage/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ func Parent(u fyne.URI) (fyne.URI, error) {
s := u.String()

// trim trailing slash
if runtime.GOOS == "windows" {
if s[len(s)-1] == '\\' {
s = s[0 : len(s)-1]
}
}
if s[len(s)-1] == '/' {
s = s[0 : len(s)-1]
}
Expand Down

0 comments on commit f3e0660

Please sign in to comment.