Skip to content

Commit

Permalink
file dialog: break out effectiveStartingDir
Browse files Browse the repository at this point in the history
Per @andydotxyz I have moved the logic to calculate the starting
directory for the file dialog widget to it's own function.
  • Loading branch information
charlesdaniels committed Jun 15, 2020
1 parent c5561ca commit db857e1
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions dialog/file.go
Expand Up @@ -42,7 +42,7 @@ type FileDialog struct {
parent fyne.Window
dialog *fileDialog
dismissText string
startingDirectory string
StartingDirectory string
}

// Declare conformity to Dialog interface
Expand Down Expand Up @@ -263,44 +263,52 @@ func (f *fileDialog) setSelected(file *fileDialogItem) {
}
}

func showFile(file *FileDialog) *fileDialog {
d := &fileDialog{file: file}
ui := d.makeUI()

dir := ""

if file.startingDirectory != "" {
// effectiveStartingDir calculates the directory at which the file dialog should
// open, based on the values of StartingDirectory, CWD, home, and any error
// conditions which occur.
//
// Order of precedence is:
//
// * file.StartingDirectory if non-empty and os.Stat()-able
// * os.Getwd()
// * os.UserHomeDir()
// * "/" (should be filesystem root on all supported platforms)
func (file *FileDialog) effectiveStartingDir() string {
if file.StartingDirectory != "" {
// the starting directory is set explicitly
if _, err := os.Stat(file.startingDirectory); err != nil {
fyne.LogError("Error with startingDirectory", err)
if _, err := os.Stat(file.StartingDirectory); err != nil {
fyne.LogError("Error with StartingDirectory", err)
} else {
dir = file.startingDirectory
return file.StartingDirectory
}
}

if dir == "" {
// Either no custom starting directory was set, or there
// was an error with it...
//
// Try to use CWD
var err error = nil
dir, err = os.Getwd()
// Either no custom starting directory was set, or there
// was an error with it...
//
// Try to use CWD
var err error = nil
dir, err := os.Getwd()
if err != nil {
// if that doesn't work, fail-over to ~/
fyne.LogError("Could not load CWD", err)
dir, err = os.UserHomeDir()
if err != nil {

// if that doesn't work, fail-over to ~/
fyne.LogError("Could not load CWD", err)
dir, err = os.UserHomeDir()
if err != nil {

// if that dosen't work, fail over to /
fyne.LogError("Could not load user home dir", err)
dir = "/"
}
// if that dosen't work, fail over to /
fyne.LogError("Could not load user home dir", err)
dir = "/"
}

}

d.setDirectory(dir)
return dir
}

func showFile(file *FileDialog) *fileDialog {
d := &fileDialog{file: file}
ui := d.makeUI()

d.setDirectory(file.effectiveStartingDir())

size := ui.MinSize().Add(fyne.NewSize(fileIconCellWidth*2+theme.Padding()*4,
(fileIconSize+fileTextSize)+theme.Padding()*4))
Expand Down Expand Up @@ -375,12 +383,6 @@ func (f *FileDialog) SetFilter(filter storage.FileFilter) {
}
}

// SetStartingDirectory configures the directory that the file dialog should
// show by default. If set to the empty string ./ (CWD) is assumed.
func (f *FileDialog) SetStartingDirectory(path string) {
f.startingDirectory = path
}

// NewFileOpen creates a file dialog allowing the user to choose a file to open.
// The dialog will appear over the window specified when Show() is called.
func NewFileOpen(callback func(fyne.URIReadCloser, error), parent fyne.Window) *FileDialog {
Expand All @@ -404,12 +406,12 @@ func ShowFileOpen(callback func(fyne.URIReadCloser, error), parent fyne.Window)

// ShowFileOpenAt works similarly to ShowFileOpen(), but with a custom starting
// directory.
func ShowFileOpenAt(callback func(fyne.URIReadCloser, error), parent fyne.Window, where string) {
func ShowFileOpenAt(callback func(fyne.URIReadCloser, error), parent fyne.Window, startdir string) {
dialog := NewFileOpen(callback, parent)
if fileOpenOSOverride(dialog) {
return
}
dialog.SetStartingDirectory(where)
dialog.StartingDirectory = startdir
dialog.Show()
}

Expand All @@ -422,11 +424,11 @@ func ShowFileSave(callback func(fyne.URIWriteCloser, error), parent fyne.Window)

// ShowFileSaveAt works simialrly to ShowFileSave(), but with a custom starting
// directory.
func ShowFileSaveAt(callback func(fyne.URIWriteCloser, error), parent fyne.Window, where string) {
func ShowFileSaveAt(callback func(fyne.URIWriteCloser, error), parent fyne.Window, startdir string) {
dialog := NewFileSave(callback, parent)
if fileSaveOSOverride(dialog) {
return
}
dialog.SetStartingDirectory(where)
dialog.StartingDirectory = startdir
dialog.Show()
}

0 comments on commit db857e1

Please sign in to comment.