Skip to content

Commit 805d657

Browse files
authored
Enable Sorting of folders by date and inherit the sort order in the build save dialog
1 parent f7cc078 commit 805d657

2 files changed

Lines changed: 87 additions & 30 deletions

File tree

src/Classes/FolderListControl.lua

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,48 @@
66
local ipairs = ipairs
77
local t_insert = table.insert
88

9+
local function naturalSortCompare(a, b)
10+
if _G.naturalSortCompare then return _G.naturalSortCompare(a,b)
11+
elseif common and common.naturalSortCompare then return common.naturalSortCompare(a,b)
12+
else return a < b
13+
end
14+
end
15+
916
local FolderListClass = newClass("FolderListControl", "ListControl", function(self, anchor, rect, subPath, onChange)
1017
self.ListControl(anchor, rect, 16, "VERTICAL", false, { })
1118
self.subPath = subPath or ""
12-
self.controls.path = new("PathControl", {"BOTTOM",self,"TOP"}, {0, -2, self.width, 24}, main.buildPath, self.subPath, function(subPath)
13-
self.subPath = subPath
19+
self.onChangeCallback = onChange
20+
21+
self.controls.path = new("PathControl", {"BOTTOM",self,"TOP"}, {0, -2, self.width, 24}, main.buildPath, self.subPath, function(newSubPath)
22+
self.subPath = newSubPath
1423
self:BuildList()
1524
self.selIndex = nil
1625
self.selValue = nil
17-
if onChange then
18-
onChange(subPath)
26+
if self.onChangeCallback then
27+
self.onChangeCallback(newSubPath)
1928
end
2029
end)
2130
self:BuildList()
2231
end)
2332

33+
function FolderListClass:SortList()
34+
if not self.list then return end
35+
local sortMode = main.buildSortMode or "NAME"
36+
37+
table.sort(self.list, function(a, b)
38+
if sortMode == "EDITED" then
39+
local modA = a.modified or 0
40+
local modB = b.modified or 0
41+
if modA ~= modB then
42+
return modA > modB
43+
end
44+
return naturalSortCompare(a.name, b.name)
45+
else
46+
return naturalSortCompare(a.name, b.name)
47+
end
48+
end)
49+
end
50+
2451
function FolderListClass:BuildList()
2552
wipeTable(self.list)
2653
local handle = NewFileSearch(main.buildPath..self.subPath.."*", true)
@@ -29,11 +56,17 @@ function FolderListClass:BuildList()
2956
t_insert(self.list, {
3057
name = fileName,
3158
fullFileName = main.buildPath..self.subPath..fileName,
59+
modified = handle:GetFileModifiedTime()
3260
})
3361
if not handle:NextFile() then
3462
break
3563
end
3664
end
65+
if handle and handle.Close then handle:Close() end
66+
67+
self:SortList()
68+
if self.UpdateScrollbar then self:UpdateScrollbar() end
69+
if self.Redraw then self:Redraw() end
3770
end
3871

3972
function FolderListClass:OpenFolder(folderName)
@@ -61,7 +94,7 @@ function FolderListClass:OnSelDelete(index, folder)
6194
main:OpenMessagePopup("Error", "Couldn't delete '"..folder.fullFileName.."': "..msg)
6295
return
6396
end
64-
self:BuildList()
97+
self:BuildList()
6598
self.selIndex = nil
6699
self.selValue = nil
67100
end

src/Modules/BuildList.lua

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,14 @@ function listMode:BuildList()
231231
break
232232
end
233233
end
234-
handle = NewFileSearch(main.buildPath..self.subPath.."*", true)
234+
handle = NewFileSearch(main.buildPath..self.subPath.."*", true)
235235
while handle do
236236
local folderName = handle:GetFileName()
237237
t_insert(self.list, {
238238
folderName = folderName,
239239
subPath = self.subPath,
240240
fullFileName = main.buildPath..self.subPath..folderName,
241+
modified = handle:GetFileModifiedTime()
241242
})
242243
if not handle:NextFile() then
243244
break
@@ -249,35 +250,58 @@ end
249250
function listMode:SortList()
250251
local oldSelFileName = self.controls.buildList.selValue and self.controls.buildList.selValue.fileName
251252
table.sort(self.list, function(a, b)
252-
if a.folderName and b.folderName then
253-
return naturalSortCompare(a.folderName, b.folderName)
254-
elseif a.folderName and not b.folderName then
255-
return true
256-
elseif not a.folderName and b.folderName then
257-
return false
258-
end
253+
local a_is_folder = a.folderName ~= nil
254+
local b_is_folder = b.folderName ~= nil
255+
256+
if a_is_folder and not b_is_folder then return true end
257+
if not a_is_folder and b_is_folder then return false end
258+
259+
259260
if main.buildSortMode == "EDITED" then
260-
return a.modified > b.modified
261-
elseif main.buildSortMode == "CLASS" then
262-
if a.className and not b.className then
263-
return false
264-
elseif not a.className and b.className then
265-
return true
266-
elseif a.className ~= b.className then
267-
return a.className < b.className
268-
elseif a.ascendClassName ~= b.ascendClassName then
269-
return a.ascendClassName < b.ascendClassName
261+
local modA = a.modified or 0 -- Use 0 as fallback if modified time is nil
262+
local modB = b.modified or 0
263+
if modA ~= modB then
264+
return modA > modB -- Newest first maybe allow for inverting of order?
265+
end
266+
-- If modified times are the same or both 0 fall back to name sort
267+
if a_is_folder then
268+
return naturalSortCompare(a.folderName, b.folderName)
269+
else
270+
return naturalSortCompare(a.fileName, b.fileName)
270271
end
271-
elseif main.buildSortMode == "LEVEL" then
272-
if a.level and not b.level then
273-
return false
274-
elseif not a.level and b.level then
275-
return true
272+
end
273+
274+
if a_is_folder then
275+
return naturalSortCompare(a.folderName, b.folderName)
276+
else
277+
if main.buildSortMode == "CLASS" then
278+
local a_has_class = a.className ~= nil
279+
local b_has_class = b.className ~= nil
280+
if not a_has_class and b_has_class then return true
281+
elseif a_has_class and not b_has_class then return false
282+
elseif a_has_class and b_has_class and a.className ~= b.className then
283+
return a.className < b.className
284+
end
285+
286+
local a_has_asc = a.ascendClassName ~= nil
287+
local b_has_asc = b.ascendClassName ~= nil
288+
if not a_has_asc and b_has_asc then return true
289+
elseif a_has_asc and not b_has_asc then return false
290+
elseif a_has_asc and b_has_asc and a.ascendClassName ~= b.ascendClassName then
291+
return a.ascendClassName < b.ascendClassName
292+
end
293+
return naturalSortCompare(a.fileName, b.fileName)
294+
elseif main.buildSortMode == "LEVEL" then
295+
if a.level and not b.level then return false
296+
elseif not a.level and b.level then return true
297+
elseif a.level and b.level then
298+
if a.level ~= b.level then return a.level < b.level end
299+
end
300+
return naturalSortCompare(a.fileName, b.fileName)
276301
else
277-
return a.level < b.level
302+
return naturalSortCompare(a.fileName, b.fileName)
278303
end
279304
end
280-
return naturalSortCompare(a.fileName, b.fileName)
281305
end)
282306
if oldSelFileName then
283307
self.controls.buildList:SelByFileName(oldSelFileName)

0 commit comments

Comments
 (0)