From 1b2adfd201dd8b49120f79382918b728db311f8d Mon Sep 17 00:00:00 2001 From: Melik <10296053+melyux@users.noreply.github.com> Date: Sun, 25 Dec 2022 03:40:11 -0600 Subject: [PATCH] Return to callback fudging, fix book info callback replacement --- frontend/apps/filemanager/filemanager.lua | 4 + .../filemanager/filemanagercollection.lua | 4 + .../apps/filemanager/filemanagerhistory.lua | 4 + plugins/coverbrowser.koplugin/covermenu.lua | 229 ++++++++++++------ 4 files changed, 162 insertions(+), 79 deletions(-) diff --git a/frontend/apps/filemanager/filemanager.lua b/frontend/apps/filemanager/filemanager.lua index 9fb26917c..31d4540ab 100644 --- a/frontend/apps/filemanager/filemanager.lua +++ b/frontend/apps/filemanager/filemanager.lua @@ -343,6 +343,7 @@ function FileManager:setupLayout() table.insert(buttons, { { text = _("Mark as reading"), + id = "mark_as_reading", -- used by covermenu enabled = status ~= "reading", callback = function() filemanagerutil.setStatus(file, "reading") @@ -352,6 +353,7 @@ function FileManager:setupLayout() }, { text = _("Put on hold"), + id = "put_on_hold", -- used by covermenu enabled = status ~= "abandoned", callback = function() filemanagerutil.setStatus(file, "abandoned") @@ -361,6 +363,7 @@ function FileManager:setupLayout() }, { text = _("Mark as read"), + id = "mark_as_read", -- used by covermenu enabled = status ~= "complete", callback = function() filemanagerutil.setStatus(file, "complete") @@ -373,6 +376,7 @@ function FileManager:setupLayout() table.insert(buttons, { { text = _("Reset settings"), + id = "reset_settings", -- used by covermenu enabled = DocSettings:hasSidecarFile(BaseUtil.realpath(file)), callback = function() UIManager:show(ConfirmBox:new{ diff --git a/frontend/apps/filemanager/filemanagercollection.lua b/frontend/apps/filemanager/filemanagercollection.lua index 29786b870..7207a7c6e 100644 --- a/frontend/apps/filemanager/filemanagercollection.lua +++ b/frontend/apps/filemanager/filemanagercollection.lua @@ -50,6 +50,7 @@ function FileManagerCollection:onMenuHold(item) { { text = _("Mark as reading"), + id = "mark_as_reading", -- used by covermenu enabled = status ~= "reading", callback = function() filemanagerutil.setStatus(item.file, "reading") @@ -59,6 +60,7 @@ function FileManagerCollection:onMenuHold(item) }, { text = _("Put on hold"), + id = "put_on_hold", -- used by covermenu enabled = status ~= "abandoned", callback = function() filemanagerutil.setStatus(item.file, "abandoned") @@ -68,6 +70,7 @@ function FileManagerCollection:onMenuHold(item) }, { text = _("Mark as read"), + id = "mark_as_read", -- used by covermenu enabled = status ~= "complete", callback = function() filemanagerutil.setStatus(item.file, "complete") @@ -80,6 +83,7 @@ function FileManagerCollection:onMenuHold(item) { { text = _("Reset settings"), + id = "reset_settings", -- used by covermenu enabled = DocSettings:hasSidecarFile(BaseUtil.realpath(item.file)), callback = function() UIManager:show(ConfirmBox:new{ diff --git a/frontend/apps/filemanager/filemanagerhistory.lua b/frontend/apps/filemanager/filemanagerhistory.lua index c897b9a23..e138ea252 100644 --- a/frontend/apps/filemanager/filemanagerhistory.lua +++ b/frontend/apps/filemanager/filemanagerhistory.lua @@ -95,6 +95,7 @@ function FileManagerHistory:onMenuHold(item) { { text = _("Mark as reading"), + id = "mark_as_reading", -- used by covermenu enabled = is_file and status ~= "reading", callback = function() filemanagerutil.setStatus(item.file, "reading") @@ -109,6 +110,7 @@ function FileManagerHistory:onMenuHold(item) }, { text = _("Put on hold"), + id = "put_on_hold", -- used by covermenu enabled = is_file and status ~= "abandoned", callback = function() filemanagerutil.setStatus(item.file, "abandoned") @@ -123,6 +125,7 @@ function FileManagerHistory:onMenuHold(item) }, { text = _("Mark as read"), + id = "mark_as_read", -- used by covermenu enabled = is_file and status ~= "complete", callback = function() filemanagerutil.setStatus(item.file, "complete") @@ -140,6 +143,7 @@ function FileManagerHistory:onMenuHold(item) { { text = _("Reset settings"), + id = "reset_settings", -- used by covermenu enabled = item.file ~= currently_opened_file and DocSettings:hasSidecarFile(FFIUtil.realpath(item.file)), callback = function() UIManager:show(ConfirmBox:new{ diff --git a/plugins/coverbrowser.koplugin/covermenu.lua b/plugins/coverbrowser.koplugin/covermenu.lua index a7306eeff..729047df5 100644 --- a/plugins/coverbrowser.koplugin/covermenu.lua +++ b/plugins/coverbrowser.koplugin/covermenu.lua @@ -320,29 +320,8 @@ function CoverMenu:updateItems(select_number) }, }) - -- Fudge the setting resets (e.g. "Reset settings" button) to also trash the cover_info_cache - local orig_purgeSettings = filemanagerutil.purgeSettings - filemanagerutil.purgeSettings = function(f) - -- Wipe the cache - if self.cover_info_cache and self.cover_info_cache[f] then - self.cover_info_cache[f] = nil - end - -- And then purge the sidecar folder as expected - orig_purgeSettings(f) - end - - -- Fudge status changes (e.g. "Mark as read" button) to also update the cover_info_cache - local orig_setStatus = filemanagerutil.setStatus - filemanagerutil.setStatus = function(f, status) - -- Update the cache - if self.cover_info_cache and self.cover_info_cache[f] then - self.cover_info_cache[f][3] = status - end - -- And then set the status on file as expected - orig_setStatus(f, status) - end - -- Create the new ButtonDialogTitle, and let UIManager show it + -- (all button callback fudging must be done after this block to stick) local ButtonDialogTitle = require("ui/widget/buttondialogtitle") self.file_dialog = ButtonDialogTitle:new{ title = orig_title, @@ -350,8 +329,52 @@ function CoverMenu:updateItems(select_number) buttons = orig_buttons, } + -- Fudge the "Reset settings" button callback to also trash the cover_info_cache + local button = self.file_dialog.button_table:getButtonById("reset_settings") + local orig_purge_callback = button.callback + button.callback = function() + -- Wipe the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file] = nil + end + -- And then purge the sidecar folder as expected + orig_purge_callback() + end + + -- Fudge the status change button callbacks to also update the cover_info_cache + button = self.file_dialog.button_table:getButtonById("mark_as_reading") + local orig_mark_as_reading_callback = button.callback + button.callback = function() + -- Update the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file][3] = "reading" + end + -- And then set the status on file as expected + orig_mark_as_reading_callback() + end + button = self.file_dialog.button_table:getButtonById("put_on_hold") + local orig_put_on_hold_callback = button.callback + button.callback = function() + -- Update the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file][3] = "abandoned" + end + -- And then set the status on file as expected + orig_put_on_hold_callback() + end + button = self.file_dialog.button_table:getButtonById("mark_as_read") + local orig_mark_as_read_callback = button.callback + button.callback = function() + -- Update the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file][3] = "complete" + end + -- And then set the status on file as expected + orig_mark_as_read_callback() + end + -- Replace the "Book information" button callback to use directly our bookinfo - local button = self.file_dialog.button_table:getButtonById("book_information") + button = self.file_dialog.button_table:getButtonById("book_information") button.callback = function() FileManagerBookInfo:show(file, bookinfo) UIManager:close(self.file_dialog) @@ -391,12 +414,6 @@ function CoverMenu:onHistoryMenuHold(item) UIManager:close(self.histfile_dialog) UIManager:clearRenderStack() - -- Replace Book information callback to use directly our bookinfo - self.histfile_dialog.button_table:getButtonById("book_information").callback = function() - FileManagerBookInfo:show(file, bookinfo) - UIManager:close(self.histfile_dialog) - end - -- Add some new buttons to original buttons set table.insert(orig_buttons, { { -- Allow user to view real size cover in ImageViewer @@ -480,35 +497,65 @@ function CoverMenu:onHistoryMenuHold(item) }, }) - -- Fudge the setting resets (e.g. "Reset settings" button) to also trash the cover_info_cache - local orig_purgeSettings = filemanagerutil.purgeSettings - filemanagerutil.purgeSettings = function(f) - -- Wipe the cache - if self.cover_info_cache and self.cover_info_cache[f] then - self.cover_info_cache[f] = nil - end - -- And then purge the sidecar folder as expected - orig_purgeSettings(f) - end - - -- Fudge status changes (e.g. "Mark as read" button) to also update the cover_info_cache - local orig_setStatus = filemanagerutil.setStatus - filemanagerutil.setStatus = function(f, status) - -- Update the cache - if self.cover_info_cache and self.cover_info_cache[f] then - self.cover_info_cache[f][3] = status - end - -- And then set the status on file as expected - orig_setStatus(f, status) - end - -- Create the new ButtonDialog, and let UIManager show it + -- (all button callback replacement must be done after this block to stick) local ButtonDialogTitle = require("ui/widget/buttondialogtitle") self.histfile_dialog = ButtonDialogTitle:new{ title = orig_title, title_align = orig_title_align, buttons = orig_buttons, } + + -- Fudge the "Reset settings" button callback to also trash the cover_info_cache + local button = self.histfile_dialog.button_table:getButtonById("reset_settings") + local orig_purge_callback = button.callback + button.callback = function() + -- Wipe the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file] = nil + end + -- And then purge the sidecar folder as expected + orig_purge_callback() + end + + -- Fudge the status change button callbacks to also update the cover_info_cache + button = self.histfile_dialog.button_table:getButtonById("mark_as_reading") + local orig_mark_as_reading_callback = button.callback + button.callback = function() + -- Update the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file][3] = "reading" + end + -- And then set the status on file as expected + orig_mark_as_reading_callback() + end + button = self.histfile_dialog.button_table:getButtonById("put_on_hold") + local orig_put_on_hold_callback = button.callback + button.callback = function() + -- Update the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file][3] = "abandoned" + end + -- And then set the status on file as expected + orig_put_on_hold_callback() + end + button = self.histfile_dialog.button_table:getButtonById("mark_as_read") + local orig_mark_as_read_callback = button.callback + button.callback = function() + -- Update the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file][3] = "complete" + end + -- And then set the status on file as expected + orig_mark_as_read_callback() + end + + -- Replace Book information callback to use directly our bookinfo + self.histfile_dialog.button_table:getButtonById("book_information").callback = function() + FileManagerBookInfo:show(file, bookinfo) + UIManager:close(self.histfile_dialog) + end + UIManager:show(self.histfile_dialog) return true end @@ -536,12 +583,6 @@ function CoverMenu:onCollectionsMenuHold(item) UIManager:close(self.collfile_dialog) UIManager:clearRenderStack() - -- Replace Book information callback to use directly our bookinfo - self.collfile_dialog.button_table:getButtonById("book_information").callback = function() - FileManagerBookInfo:show(file, bookinfo) - UIManager:close(self.collfile_dialog) - end - -- Add some new buttons to original buttons set table.insert(orig_buttons, { { -- Allow user to view real size cover in ImageViewer @@ -625,35 +666,65 @@ function CoverMenu:onCollectionsMenuHold(item) }, }) - -- Fudge the setting resets (e.g. "Reset settings" button) to also trash the cover_info_cache - local orig_purgeSettings = filemanagerutil.purgeSettings - filemanagerutil.purgeSettings = function(f) - -- Wipe the cache - if self.cover_info_cache and self.cover_info_cache[f] then - self.cover_info_cache[f] = nil - end - -- And then purge the sidecar folder as expected - orig_purgeSettings(f) - end - - -- Fudge status changes (e.g. "Mark as read" button) to also update the cover_info_cache - local orig_setStatus = filemanagerutil.setStatus - filemanagerutil.setStatus = function(f, status) - -- Update the cache - if self.cover_info_cache and self.cover_info_cache[f] then - self.cover_info_cache[f][3] = status - end - -- And then set the status on file as expected - orig_setStatus(f, status) - end - -- Create the new ButtonDialog, and let UIManager show it + -- (all button callback replacement must be done after this block to stick) local ButtonDialogTitle = require("ui/widget/buttondialogtitle") self.collfile_dialog = ButtonDialogTitle:new{ title = orig_title, title_align = orig_title_align, buttons = orig_buttons, } + + -- Fudge the "Reset settings" button callback to also trash the cover_info_cache + local button = self.collfile_dialog.button_table:getButtonById("reset_settings") + local orig_purge_callback = button.callback + button.callback = function() + -- Wipe the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file] = nil + end + -- And then purge the sidecar folder as expected + orig_purge_callback() + end + + -- Fudge the status change button callbacks to also update the cover_info_cache + button = self.collfile_dialog.button_table:getButtonById("mark_as_reading") + local orig_mark_as_reading_callback = button.callback + button.callback = function() + -- Update the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file][3] = "reading" + end + -- And then set the status on file as expected + orig_mark_as_reading_callback() + end + button = self.collfile_dialog.button_table:getButtonById("put_on_hold") + local orig_put_on_hold_callback = button.callback + button.callback = function() + -- Update the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file][3] = "abandoned" + end + -- And then set the status on file as expected + orig_put_on_hold_callback() + end + button = self.collfile_dialog.button_table:getButtonById("mark_as_read") + local orig_mark_as_read_callback = button.callback + button.callback = function() + -- Update the cache + if self.cover_info_cache and self.cover_info_cache[file] then + self.cover_info_cache[file][3] = "complete" + end + -- And then set the status on file as expected + orig_mark_as_read_callback() + end + + -- Replace Book information callback to use directly our bookinfo + self.collfile_dialog.button_table:getButtonById("book_information").callback = function() + FileManagerBookInfo:show(file, bookinfo) + UIManager:close(self.collfile_dialog) + end + UIManager:show(self.collfile_dialog) return true end