Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember the folder that file dialogs use #5138

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions dialog/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ const (
GridView
)

const viewLayoutKey = "fyne:fileDialogViewLayout"
const (
viewLayoutKey = "fyne:fileDialogViewLayout"
lastFolderKey = "fyne:fileDialogLastFolder"
)

type textWidget interface {
fyne.Widget
Expand Down Expand Up @@ -438,6 +441,7 @@ func (f *fileDialog) setLocation(dir fyne.URI) error {
return err
}

fyne.CurrentApp().Preferences().SetString(lastFolderKey, dir.String())
isFav := false
for i, fav := range f.favorites {
if fav.loc == nil {
Expand Down Expand Up @@ -601,6 +605,8 @@ func (f *fileDialog) getDataItem(id int) (fyne.URI, bool) {
//
// - file.startingDirectory if non-empty, os.Stat()-able, and uses the file://
// URI scheme
// - previously used file open/close folder within this app
// - the current app's document storage, if App.Storage() documents have been saved
// - os.UserHomeDir()
// - os.Getwd()
// - "/" (should be filesystem root on all supported platforms)
Expand All @@ -619,6 +625,18 @@ func (f *FileDialog) effectiveStartingDir() fyne.ListableURI {

}

// last used
dweymouth marked this conversation as resolved.
Show resolved Hide resolved
lastPath := fyne.CurrentApp().Preferences().String(lastFolderKey)
if lastPath != "" {
parsed, err := storage.ParseURI(lastPath)
if err == nil {
dir, err := storage.ListerForURI(parsed)
if err == nil {
return dir
}
}
}

// Try app storage
app := fyne.CurrentApp()
if hasAppFiles(app) {
Expand Down Expand Up @@ -895,7 +913,7 @@ func getFavoriteOrder() []string {
}

if runtime.GOOS == "darwin" {
order[4] = "Movies"
order[5] = "Movies"
}

return order
Expand Down
22 changes: 21 additions & 1 deletion dialog/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func comparePaths(t *testing.T, u1, u2 fyne.ListableURI) bool {
}

func TestEffectiveStartingDir(t *testing.T) {

homeString, err := os.UserHomeDir()
if err != nil {
t.Skipf("os.Gethome() failed, cannot run this test on this system (error stat()-ing ../) error was '%s'", err)
Expand Down Expand Up @@ -105,7 +104,28 @@ func TestEffectiveStartingDir(t *testing.T) {
t.Errorf("Expected effectiveStartingDir() to be '%s', but it was '%s'",
expect, res)
}
}

func TestFileDialogStartRemember(t *testing.T) {
testPath, err := filepath.Abs("./testdata")
assert.Nil(t, err)
start, err := storage.ListerForURI(storage.NewFileURI(testPath))
if err != nil {
t.Skipf("could not get lister for working directory: %s", err)
}

w := test.NewTempWindow(t, widget.NewLabel("Content"))
d := NewFileOpen(nil, w)
d.SetLocation(start)
d.Show()

assert.Equal(t, start.String(), d.dialog.dir.String())
d.Hide()

d2 := NewFileOpen(nil, w)
d2.Show()
assert.Equal(t, start.String(), d.dialog.dir.String())
d2.Hide()
}

func TestFileDialogResize(t *testing.T) {
Expand Down