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
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 33 additions & 3 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
JordanGoulder marked this conversation as resolved.
Show resolved Hide resolved

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 Expand Up @@ -574,7 +604,7 @@ func (f *FileDialog) effectiveStartingDir() fyne.ListableURI {
}

func showFile(file *FileDialog) *fileDialog {
d := &fileDialog{file: file, initialFileName: file.initialFileName}
d := &fileDialog{file: file, initialFileName: file.initialFileName, dataLock: &sync.RWMutex{}}
ui := d.makeUI()
size := ui.MinSize().Add(fyne.NewSize(fileIconCellWidth*2+theme.Padding()*6+theme.Padding(),
(fileIconSize+fileTextSize)+theme.Padding()*6))
Expand Down
11 changes: 6 additions & 5 deletions dialog/fileitem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dialog

import (
"path/filepath"
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -10,7 +11,7 @@ import (
)

func TestFileItem_Name(t *testing.T) {
f := &fileDialog{file: &FileDialog{}}
f := &fileDialog{file: &FileDialog{}, dataLock: &sync.RWMutex{}}
_ = f.makeUI()

item := f.newFileItem(storage.NewFileURI("/path/to/filename.txt"), false, false)
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestFileItem_Name(t *testing.T) {
}

func TestFileItem_FolderName(t *testing.T) {
f := &fileDialog{file: &FileDialog{}}
f := &fileDialog{file: &FileDialog{}, dataLock: &sync.RWMutex{}}
_ = f.makeUI()

item := f.newFileItem(storage.NewFileURI("/path/to/foldername/"), true, false)
Expand All @@ -61,15 +62,15 @@ func TestFileItem_FolderName(t *testing.T) {
}

func TestNewFileItem(t *testing.T) {
f := &fileDialog{file: &FileDialog{}}
f := &fileDialog{file: &FileDialog{}, dataLock: &sync.RWMutex{}}
_ = f.makeUI()
item := f.newFileItem(storage.NewFileURI("/path/to/filename.txt"), false, false)

assert.Equal(t, "filename", item.name)
}

func TestNewFileItem_Folder(t *testing.T) {
f := &fileDialog{file: &FileDialog{}}
f := &fileDialog{file: &FileDialog{}, dataLock: &sync.RWMutex{}}
_ = f.makeUI()
currentDir, _ := filepath.Abs(".")
currentLister, err := storage.ListerForURI(storage.NewFileURI(currentDir))
Expand All @@ -90,7 +91,7 @@ func TestNewFileItem_Folder(t *testing.T) {
}

func TestNewFileItem_ParentFolder(t *testing.T) {
f := &fileDialog{file: &FileDialog{}}
f := &fileDialog{file: &FileDialog{}, dataLock: &sync.RWMutex{}}
_ = f.makeUI()
currentDir, _ := filepath.Abs(".")
currentLister, err := storage.ListerForURI(storage.NewFileURI(currentDir))
Expand Down