Skip to content

Commit

Permalink
Merge pull request #2358 from Jacalz/fileicon-caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Aug 7, 2021
2 parents bf2ad69 + 6114e50 commit 632b687
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
34 changes: 14 additions & 20 deletions widget/fileicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type FileIcon struct {

resource fyne.Resource
extension string
cachedURI fyne.URI
}

// NewFileIcon takes a filepath and creates an icon with an overlaid label using the detected mimetype and extension
Expand All @@ -40,10 +39,8 @@ func NewFileIcon(uri fyne.URI) *FileIcon {

// SetURI changes the URI and makes the icon reflect a different file
func (i *FileIcon) SetURI(uri fyne.URI) {
if uri != i.URI {
i.URI = uri
i.Refresh()
}
i.URI = uri
i.Refresh()
}

func (i *FileIcon) setURI(uri fyne.URI) {
Expand All @@ -53,7 +50,6 @@ func (i *FileIcon) setURI(uri fyne.URI) {
}

i.URI = uri
i.cachedURI = nil
i.resource = i.lookupIcon(i.URI)
i.extension = trimmedExtension(uri)
}
Expand Down Expand Up @@ -85,7 +81,6 @@ func (i *FileIcon) CreateRenderer() fyne.WidgetRenderer {
s.ext.Alignment = fyne.TextAlignCenter

s.SetObjects([]fyne.CanvasObject{s.background, s.img, s.ext})
i.cachedURI = i.URI

return s
}
Expand Down Expand Up @@ -124,11 +119,13 @@ func (i *FileIcon) isDir(uri fyne.URI) bool {
return true
}

can, err := storage.CanList(uri)
listable, err := storage.ListerForURI(uri)
if err != nil {
return false
}
return can

i.URI = listable // Avoid having to call storage.ListerForURI(uri) the next time.
return true
}

type fileIconRenderer struct {
Expand Down Expand Up @@ -170,17 +167,14 @@ func (s *fileIconRenderer) Layout(size fyne.Size) {
}

func (s *fileIconRenderer) Refresh() {
if s.file.URI != s.file.cachedURI {
s.file.propertyLock.Lock()
s.file.setURI(s.file.URI)
s.file.propertyLock.Unlock()

s.file.propertyLock.RLock()
s.file.cachedURI = s.file.URI
s.img.Resource = s.file.resource
s.ext.Text = s.file.extension
s.file.propertyLock.RUnlock()
}
s.file.propertyLock.Lock()
s.file.setURI(s.file.URI)
s.file.propertyLock.Unlock()

s.file.propertyLock.RLock()
s.img.Resource = s.file.resource
s.ext.Text = s.file.extension
s.file.propertyLock.RUnlock()

if s.file.Selected {
s.background.Show()
Expand Down
24 changes: 24 additions & 0 deletions widget/fileicon_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,27 @@ func TestFileIcon_SetURI_WithFolder(t *testing.T) {
assert.Empty(t, item.extension)
assert.Equal(t, theme.FolderIcon(), item.resource)
}

func TestFileIcon_DirURIUpdated(t *testing.T) {
workingDir, err := os.Getwd()
if err != nil {
fyne.LogError("Could not get current working directory", err)
t.FailNow()
}
testDir := filepath.Join(workingDir, "testdata", "notCreatedYet")

uri := storage.NewFileURI(testDir)
item := newRenderedFileIcon(uri)

// The directory has not been created. It can not be listed yet.
assert.Equal(t, theme.FileTextIcon(), item.resource)

err = os.Mkdir(testDir, 0755)
if err != nil {
t.Fatal(err)
}
defer os.Remove(testDir)

item.Refresh()
assert.Equal(t, theme.FolderIcon(), item.resource)
}

0 comments on commit 632b687

Please sign in to comment.