Skip to content

Commit

Permalink
Merge pull request #5439 from andydotxyz/fix/5113
Browse files Browse the repository at this point in the history
Fix file order to be case insensitive
  • Loading branch information
andydotxyz authored Jan 21, 2025
2 parents 730f36e + 532bcab commit 0d08945
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
13 changes: 13 additions & 0 deletions dialog/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
"sort"
"strings"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -422,6 +423,18 @@ func (f *fileDialog) refreshDir(dir fyne.ListableURI) {
}
}

toSort := icons
if parent != nil {
toSort = icons[1:]
}
sort.Slice(toSort, func(i, j int) bool {
if parent != nil { // avoiding the parent in [0]
i++
j++
}

return strings.ToLower(icons[i].Name()) < strings.ToLower(icons[j].Name())
})
f.data = icons

f.files.Refresh()
Expand Down
45 changes: 43 additions & 2 deletions dialog/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func TestHiddenFiles(t *testing.T) {
t.Error("Failed to open testdata dir", err)
}

// git does not preserve windows hidden flag so we have to set it.
// git does not preserve windows hidden flag, so we have to set it.
// just an empty function for non windows builds
if err := hideFile(filepath.Join(testDataPath, ".hidden")); err != nil {
t.Error("Failed to hide .hidden", err)
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestHiddenFiles(t *testing.T) {
target = item
}
}
assert.NotNil(t, target, "Failed,.hidden not found in testdata")
assert.NotNil(t, target, "Failed, .hidden not found in testdata")
}

func TestShowFileSave(t *testing.T) {
Expand Down Expand Up @@ -471,6 +471,47 @@ func TestFileFilters(t *testing.T) {
assert.Equal(t, 11, count)
}

func TestFileSort(t *testing.T) {
testDataPath, _ := filepath.Abs("testdata")
testData := storage.NewFileURI(testDataPath)
dir, err := storage.ListerForURI(testData)
if err != nil {
t.Error("Failed to open testdata dir", err)
}

win := test.NewTempWindow(t, widget.NewLabel("Content"))
d := NewFileOpen(func(file fyne.URIReadCloser, err error) {
}, win)
d.SetLocation(dir)
d.Show()

popup := win.Canvas().Overlays().Top().(*widget.PopUp)
defer win.Canvas().Overlays().Remove(popup)
assert.NotNil(t, popup)

ui := popup.Content.(*fyne.Container)

files := ui.Objects[0].(*container.Split).Trailing.(*fyne.Container).Objects[1].(*container.Scroll).Content.(*fyne.Container).Objects[0].(*widget.GridWrap)
objects := test.TempWidgetRenderer(t, files).Objects()[0].(*container.Scroll).Content.(*fyne.Container).Objects
assert.NotEmpty(t, objects)

binPos := -1
capitalPos := -1
for i, icon := range objects {
item := test.TempWidgetRenderer(t, icon.(fyne.Widget)).Objects()[1].(*fileDialogItem)
switch item.name {
case "bin":
binPos = i
case "Capitalised":
capitalPos = i
}
}

assert.NotEqual(t, -1, binPos, "bin file not found")
assert.NotEqual(t, -1, capitalPos, "Capitalised.txt file not found")
assert.Less(t, binPos, capitalPos)
}

func TestView(t *testing.T) {
win := test.NewTempWindow(t, widget.NewLabel("Content"))

Expand Down
1 change: 1 addition & 0 deletions dialog/testdata/Capitalised.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
adsfasdfasdfasdfdsfasdfasdfafdafdasf

0 comments on commit 0d08945

Please sign in to comment.