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

Fix #4260: Synchronize access to fileDialog.data file list #4279

Merged
Merged
Changes from 3 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
34 changes: 32 additions & 2 deletions dialog/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
Expand Down Expand Up @@ -54,7 +55,9 @@ type fileDialog struct {
showHidden bool

view viewLayout
data []fyne.URI

data []fyne.URI
dataLock sync.RWMutex

win *widget.PopUp
selected fyne.URI
Expand Down Expand Up @@ -334,7 +337,9 @@ func (f *fileDialog) loadFavorites() {
}

func (f *fileDialog) refreshDir(dir fyne.ListableURI) {
f.dataLock.Lock()
f.data = nil
f.dataLock.Unlock()

files, err := dir.List()
if err != nil {
Expand Down Expand Up @@ -366,7 +371,10 @@ func (f *fileDialog) refreshDir(dir fyne.ListableURI) {
icons = append(icons, file)
}
}

f.dataLock.Lock()
f.data = icons
f.dataLock.Unlock()

f.files.Refresh()
f.filesScroll.Offset = fyne.NewPos(0, 0)
Expand Down Expand Up @@ -480,20 +488,42 @@ func (f *fileDialog) setSelected(file fyne.URI, id int) {
func (f *fileDialog) setView(view viewLayout) {
f.view = view
count := func() int {
f.dataLock.RLock()
defer f.dataLock.RUnlock()

return len(f.data)
}
template := func() fyne.CanvasObject {
return f.newFileItem(storage.NewFileURI("./tempfile"), true, false)
}
update := func(id widget.GridWrapItemID, o fyne.CanvasObject) {
f.dataLock.RLock()

if id >= len(f.data) {
f.dataLock.RUnlock()
return
}
JordanGoulder marked this conversation as resolved.
Show resolved Hide resolved

dir := f.data[id]
f.dataLock.RUnlock()

parent := id == 0 && len(dir.Path()) < len(f.dir.Path())
_, isDir := dir.(fyne.ListableURI)
o.(*fileDialogItem).setLocation(dir, isDir || parent, parent)
}
choose := func(id int) {
f.dataLock.RLock()
JordanGoulder marked this conversation as resolved.
Show resolved Hide resolved

if id >= len(f.data) {
f.dataLock.RUnlock()
return
}

file := f.data[id]
f.dataLock.RUnlock()

f.selectedID = id
f.setSelected(f.data[id], id)
f.setSelected(file, id)
}
if f.view == gridView {
grid := widget.NewGridWrap(count, template, update)
Expand Down