[Screensaver] Add an adjustable cap (default 256) to the number of random images processed (#14521)
This commit is contained in:
1 parent
95b2edb758
commit
393837b919
4 files changed
+15
-11
No files matched your search
@@ -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)]
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+6
-1
@@ -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
|
||||
|
||||
Reference in new issue
Block a user