From 5b889a0145e622f4e7a54e1b6cafed96ac2f367a Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Tue, 22 Nov 2022 14:46:44 +0200 Subject: [PATCH] Reader style tweaks: register in Dispatcher manually (#9816) Style tweaks can be applied with a gesture or added to a profile. --- .../apps/reader/modules/readerstyletweak.lua | 113 +++++++++++++----- frontend/dispatcher.lua | 40 +++---- frontend/ui/widget/touchmenu.lua | 2 +- plugins/profiles.koplugin/main.lua | 8 +- 4 files changed, 111 insertions(+), 52 deletions(-) diff --git a/frontend/apps/reader/modules/readerstyletweak.lua b/frontend/apps/reader/modules/readerstyletweak.lua index 4461d34af..50062c6a4 100644 --- a/frontend/apps/reader/modules/readerstyletweak.lua +++ b/frontend/apps/reader/modules/readerstyletweak.lua @@ -5,6 +5,7 @@ local CenterContainer = require("ui/widget/container/centercontainer") local CssTweaks = require("ui/data/css_tweaks") local DataStorage = require("datastorage") local Device = require("device") +local Dispatcher = require("dispatcher") local Event = require("ui/event") local Font = require("ui/font") local FrameContainer = require("ui/widget/container/framecontainer") @@ -123,17 +124,28 @@ function TweakInfoWidget:init() local buttons = { { - text = _("Close"), - callback = function() - UIManager:close(self) - end, + { + text = self.is_tweak_in_dispatcher and _("Don't show in action list") or _("Show in action list"), + callback = function() + self.toggle_tweak_in_dispatcher_callback() + UIManager:close(self) + end, + }, }, { - text = self.is_global_default and _("Don't use on all books") or _("Use on all books"), - callback = function() - self.toggle_global_default_callback() - UIManager:close(self) - end, + { + text = _("Close"), + callback = function() + UIManager:close(self) + end, + }, + { + text = self.is_global_default and _("Don't use on all books") or _("Use on all books"), + callback = function() + self.toggle_global_default_callback() + UIManager:close(self) + end, + }, }, } @@ -141,7 +153,7 @@ function TweakInfoWidget:init() width = content:getSize().w, button_font_face = "cfont", button_font_size = 20, - buttons = { buttons }, + buttons = buttons, zero_sep = true, show_parent = self, } @@ -427,12 +439,23 @@ function ReaderStyleTweak:onSaveSettings() end self.ui.doc_settings:saveSetting("style_tweaks", util.tableSize(self.doc_tweaks) > 0 and self.doc_tweaks or nil) G_reader_settings:saveSetting("style_tweaks", self.global_tweaks) + G_reader_settings:saveSetting("style_tweaks_in_dispatcher", self.tweaks_in_dispatcher) self.ui.doc_settings:saveSetting("book_style_tweak", self.book_style_tweak) self.ui.doc_settings:saveSetting("book_style_tweak_enabled", self.book_style_tweak_enabled) self.ui.doc_settings:saveSetting("book_style_tweak_last_edit_pos", self.book_style_tweak_last_edit_pos) end +local function dispatcherRegisterStyleTweak(tweak_id, tweak_title) + Dispatcher:registerAction("style_tweak_"..tweak_id, + {category="none", event="ToggleStyleTweak", arg=tweak_id, title=T(_("Toggle style tweak: %1"), tweak_title), rolling=true}) +end + +local function dispatcherUnregisterStyleTweak(tweak_id) + Dispatcher:removeAction("style_tweak_"..tweak_id) +end + function ReaderStyleTweak:init() + self.tweaks_in_dispatcher = G_reader_settings:readSetting("style_tweaks_in_dispatcher") or {} self.tweaks_by_id = {} self.tweaks_table = {} @@ -503,6 +526,9 @@ You can enable individual tweaks on this book with a tap, or view more details a if self.global_tweaks[item.id] then title = title .. " ★" end + if self.tweaks_in_dispatcher[item.id] then + title = title .. " \u{F144}" + end return title end, hold_callback = function(touchmenu_instance) @@ -534,27 +560,23 @@ You can enable individual tweaks on this book with a tap, or view more details a end touchmenu_instance:updateItems() self:updateCssText(true) -- apply it immediately - end + end, + is_tweak_in_dispatcher = self.tweaks_in_dispatcher[item.id], + toggle_tweak_in_dispatcher_callback = function() + if self.tweaks_in_dispatcher[item.id] then + self.tweaks_in_dispatcher[item.id] = nil + dispatcherUnregisterStyleTweak(item.id) + else + self.tweaks_in_dispatcher[item.id] = item.title + dispatcherRegisterStyleTweak(item.id, item.title) + end + touchmenu_instance:updateItems() + end, }) end, callback = function() -- enable/disable only for this book - local enabled, g_enabled = self:isTweakEnabled(item.id) - if enabled then - if g_enabled then - -- if globaly enabled, mark it as disabled - -- for this document only - self.doc_tweaks[item.id] = false - else - self.doc_tweaks[item.id] = nil - end - else - if item.conflicts_with then - self:resolveConflictsBeforeEnabling(item.id, item.conflicts_with) - end - self.doc_tweaks[item.id] = true - end - self:updateCssText(true) -- apply it immediately + self:onToggleStyleTweak(item.id, item) end, separator = item.separator, }) @@ -671,6 +693,7 @@ You can enable individual tweaks on this book with a tap, or view more details a table.insert(self.tweaks_table, book_tweak_item) self.ui.menu:registerToMainMenu(self) + self:onDispatcherRegisterActions() end function ReaderStyleTweak:addToMainMenu(menu_items) @@ -687,6 +710,42 @@ function ReaderStyleTweak:addToMainMenu(menu_items) } end +function ReaderStyleTweak:onToggleStyleTweak(tweak_id, item) + local enabled, g_enabled = self:isTweakEnabled(tweak_id) + if enabled then + if g_enabled then + -- if globaly enabled, mark it as disabled + -- for this document only + self.doc_tweaks[tweak_id] = false + else + self.doc_tweaks[tweak_id] = nil + end + else + local conflicts_with + if item then + conflicts_with = item.conflicts_with + else -- called from Dispatcher + for _, v in ipairs(CssTweaks) do + if v.id == tweak_id then + conflicts_with = v.conflicts_with + break + end + end + end + if conflicts_with then + self:resolveConflictsBeforeEnabling(tweak_id, conflicts_with) + end + self.doc_tweaks[tweak_id] = true + end + self:updateCssText(true) -- apply it immediately +end + +function ReaderStyleTweak:onDispatcherRegisterActions() + for tweak_id, tweak_title in pairs(self.tweaks_in_dispatcher) do + dispatcherRegisterStyleTweak(tweak_id, tweak_title) + end +end + local BOOK_TWEAK_SAMPLE_CSS = [[ p.someTitleClassName { text-indent: 0; } diff --git a/frontend/dispatcher.lua b/frontend/dispatcher.lua index 47fc3e7bf..8e4868bed 100644 --- a/frontend/dispatcher.lua +++ b/frontend/dispatcher.lua @@ -175,7 +175,7 @@ local settingsList = { embedded_css = {category="string", rolling=true}, embedded_fonts = {category="string", rolling=true}, smooth_scaling = {category="string", rolling=true}, - nightmode_images = {category="string", rolling=true}, + nightmode_images = {category="string", rolling=true, separator=true}, -- parsed from KoptOptions kopt_trim_page = {category="string", paging=true}, @@ -640,23 +640,23 @@ function Dispatcher:_addItem(caller, menu, location, settings, section) then if settingsList[k].category == "none" or settingsList[k].category == "arg" then table.insert(menu, { - text = settingsList[k].title, - checked_func = function() - return location[settings] ~= nil and location[settings][k] ~= nil - end, - callback = function(touchmenu_instance) - if location[settings] == nil then - location[settings] = {} - end - if location[settings][k] then - location[settings][k] = nil - Dispatcher:_removeFromOrder(location, settings, k) - else - location[settings][k] = true - Dispatcher:_addToOrder(location, settings, k) - end - caller.updated = true - if touchmenu_instance then touchmenu_instance:updateItems() end + text = settingsList[k].title, + checked_func = function() + return location[settings] ~= nil and location[settings][k] ~= nil + end, + callback = function(touchmenu_instance) + if location[settings] == nil then + location[settings] = {} + end + if location[settings][k] then + location[settings][k] = nil + Dispatcher:_removeFromOrder(location, settings, k) + else + location[settings][k] = true + Dispatcher:_addToOrder(location, settings, k) + end + caller.updated = true + if touchmenu_instance then touchmenu_instance:updateItems() end end, separator = settingsList[k].separator, }) @@ -666,7 +666,7 @@ function Dispatcher:_addItem(caller, menu, location, settings, section) return Dispatcher:getNameFromItem(k, location[settings]) end, checked_func = function() - return location[settings] ~= nil and location[settings][k] ~= nil + return location[settings] ~= nil and location[settings][k] ~= nil end, callback = function(touchmenu_instance) local SpinWidget = require("ui/widget/spinwidget") @@ -714,7 +714,7 @@ function Dispatcher:_addItem(caller, menu, location, settings, section) return Dispatcher:getNameFromItem(k, location[settings]) end, checked_func = function() - return location[settings] ~= nil and location[settings][k] ~= nil + return location[settings] ~= nil and location[settings][k] ~= nil end, callback = function(touchmenu_instance) local _ = require("gettext") diff --git a/frontend/ui/widget/touchmenu.lua b/frontend/ui/widget/touchmenu.lua index 02d8ca2c6..81e69edf8 100644 --- a/frontend/ui/widget/touchmenu.lua +++ b/frontend/ui/widget/touchmenu.lua @@ -681,7 +681,7 @@ function TouchMenu:updateItems() if item_tmp:isEnabled() then table.insert(self.layout, {[self.cur_tab] = item_tmp}) -- for the focusmanager end - if item.separator and c ~= self.perpage then + if item.separator and c ~= self.perpage and i ~= #self.item_table then -- insert split line table.insert(self.item_group, self.split_line) end diff --git a/plugins/profiles.koplugin/main.lua b/plugins/profiles.koplugin/main.lua index 1a786b9b7..75f230b59 100644 --- a/plugins/profiles.koplugin/main.lua +++ b/plugins/profiles.koplugin/main.lua @@ -59,7 +59,7 @@ local function dispatcherRegisterProfile(name) {category="none", event="ProfileExecute", arg=name, title=T(_("Profile %1"), name), general=true}) end -local function dispatcherRemoveProfile(name) +local function dispatcherUnregisterProfile(name) Dispatcher:removeAction("profile_exec_"..name) end @@ -143,7 +143,7 @@ function Profiles:getSubMenuItems() end, callback = function(touchmenu_instance) if v.settings.registered then - dispatcherRemoveProfile(k) + dispatcherUnregisterProfile(k) self.data[k].settings.registered = nil else dispatcherRegisterProfile(k) @@ -166,7 +166,7 @@ function Profiles:getSubMenuItems() callback = function(touchmenu_instance) local function editCallback(new_name) if v.settings.registered then - dispatcherRemoveProfile(k) + dispatcherUnregisterProfile(k) dispatcherRegisterProfile(new_name) end self:renameAutostart(k, new_name) @@ -205,7 +205,7 @@ function Profiles:getSubMenuItems() ok_text = _("Delete"), ok_callback = function() if v.settings.registered then - dispatcherRemoveProfile(k) + dispatcherUnregisterProfile(k) end self:renameAutostart(k) self.data[k] = nil