From 5d4747c5935d4be6e0617332eba56edd897d7b90 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Sat, 17 Feb 2024 02:17:59 +0200 Subject: [PATCH] FileManager: less lfs calls (#11452) Use item info provided by FileChooser. Also fix showing PathChooser with invalid path, and fix issue when opening non-supported files. --- frontend/apps/filemanager/filemanager.lua | 18 ++++++++---------- frontend/document/documentregistry.lua | 2 +- frontend/ui/widget/filechooser.lua | 7 +++++-- plugins/coverbrowser.koplugin/covermenu.lua | 5 +++-- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/frontend/apps/filemanager/filemanager.lua b/frontend/apps/filemanager/filemanager.lua index 2646ea1f2..a5956feea 100644 --- a/frontend/apps/filemanager/filemanager.lua +++ b/frontend/apps/filemanager/filemanager.lua @@ -185,18 +185,18 @@ function FileManager:setupLayout() return true end - function file_chooser:onFileHold(file) + function file_chooser:onFileHold(item) if file_manager.select_mode then file_manager:tapPlus() else - self:showFileDialog(file) + self:showFileDialog(item) end end - function file_chooser:showFileDialog(file) -- luacheck: ignore - local is_file = isFile(file) - local is_folder = lfs.attributes(file, "mode") == "directory" - local is_not_parent_folder = BaseUtil.basename(file) ~= ".." + function file_chooser:showFileDialog(item) -- luacheck: ignore + local file = item.path + local is_file = item.is_file + local is_not_parent_folder = not item.is_go_up local function close_dialog_callback() UIManager:close(self.file_dialog) @@ -324,9 +324,7 @@ function FileManager:setupLayout() }, }) end - end - - if is_folder then + else -- folder local folder = BaseUtil.realpath(file) table.insert(buttons, { { @@ -1495,7 +1493,7 @@ function FileManager:openFile(file, provider, doc_caller_callback, aux_caller_ca if provider == nil then provider = DocumentRegistry:getProvider(file, true) -- include auxiliary end - if provider.order then -- auxiliary + if provider and provider.order then -- auxiliary if aux_caller_callback then aux_caller_callback() end diff --git a/frontend/document/documentregistry.lua b/frontend/document/documentregistry.lua index 117b3100e..860148805 100644 --- a/frontend/document/documentregistry.lua +++ b/frontend/document/documentregistry.lua @@ -98,7 +98,7 @@ function DocumentRegistry:getProvider(file, include_aux) return provider end -- highest weighted provider - return providers[1].provider + return providers and providers[1].provider end return self:getFallbackProvider() end diff --git a/frontend/ui/widget/filechooser.lua b/frontend/ui/widget/filechooser.lua index ec57e588a..66875e5d2 100644 --- a/frontend/ui/widget/filechooser.lua +++ b/frontend/ui/widget/filechooser.lua @@ -232,6 +232,9 @@ end function FileChooser:init() self.path_items = {} + if lfs.attributes(self.path, "mode") ~= "directory" then + self.path = G_reader_settings:readSetting("home_dir") or filemanagerutil.getDefaultDir() + end self.item_table = self:genItemTableFromPath(self.path) Menu.init(self) -- call parent's init() end @@ -540,7 +543,7 @@ function FileChooser:onMenuSelect(item) end function FileChooser:onMenuHold(item) - self:onFileHold(item.path) + self:onFileHold(item) return true end @@ -549,7 +552,7 @@ function FileChooser:onFileSelect(file) return true end -function FileChooser:onFileHold(file) +function FileChooser:onFileHold(item) return true end diff --git a/plugins/coverbrowser.koplugin/covermenu.lua b/plugins/coverbrowser.koplugin/covermenu.lua index 6b1bfb7d0..579c74b4c 100644 --- a/plugins/coverbrowser.koplugin/covermenu.lua +++ b/plugins/coverbrowser.koplugin/covermenu.lua @@ -242,10 +242,11 @@ function CoverMenu:updateItems(select_number) -- Replace it with ours -- This causes luacheck warning: "shadowing upvalue argument 'self' on line 34". -- Ignoring it (as done in filemanager.lua for the same showFileDialog) - self.showFileDialog = function(self, file) -- luacheck: ignore + self.showFileDialog = function(self, item) -- luacheck: ignore + local file = item.path -- Call original function: it will create a ButtonDialog -- and store it as self.file_dialog, and UIManager:show() it. - self.showFileDialog_orig(self, file) + self.showFileDialog_orig(self, item) local bookinfo = self.book_props -- getBookInfo(file) called by FileManager if not bookinfo or bookinfo._is_directory then