Skip to content

Commit a683199

Browse files
Co-authored-by: Noologos <Noologos@users.noreply.github.com>
1 parent 7bf1b98 commit a683199

File tree

2 files changed

+80
-30
lines changed

2 files changed

+80
-30
lines changed

src/Classes/FolderListControl.lua

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,38 @@ local t_insert = table.insert
99
local FolderListClass = newClass("FolderListControl", "ListControl", function(self, anchor, rect, subPath, onChange)
1010
self.ListControl(anchor, rect, 16, "VERTICAL", false, { })
1111
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
12+
self.onChangeCallback = onChange
13+
14+
self.controls.path = new("PathControl", {"BOTTOM",self,"TOP"}, {0, -2, self.width, 24}, main.buildPath, self.subPath, function(newSubPath)
15+
self.subPath = newSubPath
1416
self:BuildList()
1517
self.selIndex = nil
1618
self.selValue = nil
17-
if onChange then
18-
onChange(subPath)
19+
if self.onChangeCallback then
20+
self.onChangeCallback(newSubPath)
1921
end
2022
end)
2123
self:BuildList()
2224
end)
2325

26+
function FolderListClass:SortList()
27+
if not self.list then return end
28+
local sortMode = main.buildSortMode or "NAME"
29+
30+
table.sort(self.list, function(a, b)
31+
if sortMode == "EDITED" then
32+
local modA = a.modified or 0
33+
local modB = b.modified or 0
34+
if modA ~= modB then
35+
return modA > modB
36+
end
37+
return naturalSortCompare(a.name, b.name)
38+
else
39+
return naturalSortCompare(a.name, b.name)
40+
end
41+
end)
42+
end
43+
2444
function FolderListClass:BuildList()
2545
wipeTable(self.list)
2646
local handle = NewFileSearch(main.buildPath..self.subPath.."*", true)
@@ -29,11 +49,17 @@ function FolderListClass:BuildList()
2949
t_insert(self.list, {
3050
name = fileName,
3151
fullFileName = main.buildPath..self.subPath..fileName,
52+
modified = handle:GetFileModifiedTime()
3253
})
3354
if not handle:NextFile() then
3455
break
3556
end
3657
end
58+
if handle and handle.Close then handle:Close() end
59+
60+
self:SortList()
61+
if self.UpdateScrollbar then self:UpdateScrollbar() end
62+
if self.Redraw then self:Redraw() end
3763
end
3864

3965
function FolderListClass:OpenFolder(folderName)
@@ -61,7 +87,7 @@ function FolderListClass:OnSelDelete(index, folder)
6187
main:OpenMessagePopup("Error", "Couldn't delete '"..folder.fullFileName.."': "..msg)
6288
return
6389
end
64-
self:BuildList()
90+
self:BuildList()
6591
self.selIndex = nil
6692
self.selValue = nil
6793
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)