diff --git a/frontend/apps/filemanager/filemanagerutil.lua b/frontend/apps/filemanager/filemanagerutil.lua index adaa26566..db64aec34 100644 --- a/frontend/apps/filemanager/filemanagerutil.lua +++ b/frontend/apps/filemanager/filemanagerutil.lua @@ -53,13 +53,13 @@ function filemanagerutil.splitFileNameType(filepath) return filename_without_suffix, filetype end -function filemanagerutil.getRandomFile(dir, match_func) +function filemanagerutil.getRandomFile(dir, match_func, file_cap) local files = {} util.findFiles(dir, function(file) if match_func(file) then table.insert(files, file) end - end, false) + end, false, file_cap) if #files > 0 then math.randomseed(os.time()) return files[math.random(#files)] diff --git a/frontend/ui/elements/screensaver_menu.lua b/frontend/ui/elements/screensaver_menu.lua index bdaac4a6c..d6eac8919 100644 --- a/frontend/ui/elements/screensaver_menu.lua +++ b/frontend/ui/elements/screensaver_menu.lua @@ -168,7 +168,7 @@ return { }, { text = _("Cycle through images in order"), - help_text = _("When enabled, all images (up to 128) will be displayed at least once on the sleep screen in sequence before repeating the cycle."), + help_text = _("When enabled, all images (up to 256) will be displayed at least once on the sleep screen in sequence before repeating the cycle."), enabled_func = allowRandomImageFolder, checked_func = function() return G_reader_settings:isTrue("screensaver_cycle_images_alphabetically") diff --git a/frontend/ui/screensaver.lua b/frontend/ui/screensaver.lua index 707e2ce17..0729484bb 100644 --- a/frontend/ui/screensaver.lua +++ b/frontend/ui/screensaver.lua @@ -87,20 +87,19 @@ local function _getRandomImage(dir) local match_func = function(file) -- images, ignore macOS resource forks return not util.stringStartsWith(ffiUtil.basename(file), "._") and DocumentRegistry:isImageFile(file) end + -- Slippery slope ahead! Ensure the number of files does not become unmanageable, otherwise we'll have performance issues. + -- Power users can increase this cap if needed. Beware though, this grows at O(n * c) where c increases with the number of files. + -- NOTE: empirically, a kindle 4 found and sorted 128 files in 0.274828 seconds. + local file_cap = G_reader_settings:readSetting("screensaver_file_cap") or 256 -- If the user has set the option to cycle images alphabetically, we sort the files instead of picking a random one. if G_reader_settings:isTrue("screensaver_cycle_images_alphabetically") then local start_time = time.now() local files = {} - local num_files = 0 util.findFiles(dir, function(file) - -- Slippery slope ahead! Ensure the number of files does not become unmanageable, otherwise we'll have performance issues. - -- NOTE: empirically, a kindle 4 found and sorted 128 files in 0.274828 seconds. - if num_files >= 128 then return end -- this seems like a reasonable [yet arbitrary] limit if match_func(file) then table.insert(files, file) - num_files = num_files + 1 end - end, false) + end, false, file_cap) if #files == 0 then return end -- we have files, sort them in natural order, i.e z2 < z11 < z20 local sort = require("sort") @@ -117,7 +116,7 @@ local function _getRandomImage(dir) G_reader_settings:saveSetting("screensaver_cycle_index", index) return files[index] else -- Pick a random file (default behavior) - return filemanagerutil.getRandomFile(dir, match_func) + return filemanagerutil.getRandomFile(dir, match_func, file_cap) end end diff --git a/frontend/util.lua b/frontend/util.lua index 482fe8ef6..0e5850ae3 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -783,12 +783,16 @@ end --- Recursively scan directory for files inside -- @string path -- @func callback(fullpath, name, attr) -function util.findFiles(dir, cb, recursive) +-- @bool recursive +-- @int file_cap (maximum number of files to find) +function util.findFiles(dir, cb, recursive, file_cap) recursive = recursive ~= false + local count = 0 local function scan(current) local ok, iter, dir_obj = pcall(lfs.dir, current) if not ok then return end for f in iter, dir_obj do + if file_cap and count >= file_cap then return end local path = current.."/"..f -- lfs can return nil here, as it will follow symlinks! local attr = lfs.attributes(path) or {} @@ -798,6 +802,7 @@ function util.findFiles(dir, cb, recursive) end elseif attr.mode == "file" or attr.mode == "link" then cb(path, f, attr) + count = count + 1 end end end