Reader style tweaks: register in Dispatcher manually (#9816)

Style tweaks can be applied with a gesture or added to a profile.
reviewable/pr9822/r1
hius07 2 years ago committed by GitHub
parent 486d7071c7
commit 5b889a0145
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,7 @@ local CenterContainer = require("ui/widget/container/centercontainer")
local CssTweaks = require("ui/data/css_tweaks") local CssTweaks = require("ui/data/css_tweaks")
local DataStorage = require("datastorage") local DataStorage = require("datastorage")
local Device = require("device") local Device = require("device")
local Dispatcher = require("dispatcher")
local Event = require("ui/event") local Event = require("ui/event")
local Font = require("ui/font") local Font = require("ui/font")
local FrameContainer = require("ui/widget/container/framecontainer") local FrameContainer = require("ui/widget/container/framecontainer")
@ -123,17 +124,28 @@ function TweakInfoWidget:init()
local buttons = { local buttons = {
{ {
text = _("Close"), {
callback = function() text = self.is_tweak_in_dispatcher and _("Don't show in action list") or _("Show in action list"),
UIManager:close(self) callback = function()
end, 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() text = _("Close"),
self.toggle_global_default_callback() callback = function()
UIManager:close(self) UIManager:close(self)
end, 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, width = content:getSize().w,
button_font_face = "cfont", button_font_face = "cfont",
button_font_size = 20, button_font_size = 20,
buttons = { buttons }, buttons = buttons,
zero_sep = true, zero_sep = true,
show_parent = self, show_parent = self,
} }
@ -427,12 +439,23 @@ function ReaderStyleTweak:onSaveSettings()
end end
self.ui.doc_settings:saveSetting("style_tweaks", util.tableSize(self.doc_tweaks) > 0 and self.doc_tweaks or nil) 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", 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", 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_enabled", self.book_style_tweak_enabled)
self.ui.doc_settings:saveSetting("book_style_tweak_last_edit_pos", self.book_style_tweak_last_edit_pos) self.ui.doc_settings:saveSetting("book_style_tweak_last_edit_pos", self.book_style_tweak_last_edit_pos)
end 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() function ReaderStyleTweak:init()
self.tweaks_in_dispatcher = G_reader_settings:readSetting("style_tweaks_in_dispatcher") or {}
self.tweaks_by_id = {} self.tweaks_by_id = {}
self.tweaks_table = {} 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 if self.global_tweaks[item.id] then
title = title .. "" title = title .. ""
end end
if self.tweaks_in_dispatcher[item.id] then
title = title .. " \u{F144}"
end
return title return title
end, end,
hold_callback = function(touchmenu_instance) 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 end
touchmenu_instance:updateItems() touchmenu_instance:updateItems()
self:updateCssText(true) -- apply it immediately 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, end,
callback = function() callback = function()
-- enable/disable only for this book -- enable/disable only for this book
local enabled, g_enabled = self:isTweakEnabled(item.id) self:onToggleStyleTweak(item.id, item)
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
end, end,
separator = item.separator, 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) table.insert(self.tweaks_table, book_tweak_item)
self.ui.menu:registerToMainMenu(self) self.ui.menu:registerToMainMenu(self)
self:onDispatcherRegisterActions()
end end
function ReaderStyleTweak:addToMainMenu(menu_items) function ReaderStyleTweak:addToMainMenu(menu_items)
@ -687,6 +710,42 @@ function ReaderStyleTweak:addToMainMenu(menu_items)
} }
end 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 = [[ local BOOK_TWEAK_SAMPLE_CSS = [[
p.someTitleClassName { text-indent: 0; } p.someTitleClassName { text-indent: 0; }

@ -175,7 +175,7 @@ local settingsList = {
embedded_css = {category="string", rolling=true}, embedded_css = {category="string", rolling=true},
embedded_fonts = {category="string", rolling=true}, embedded_fonts = {category="string", rolling=true},
smooth_scaling = {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 -- parsed from KoptOptions
kopt_trim_page = {category="string", paging=true}, kopt_trim_page = {category="string", paging=true},
@ -640,23 +640,23 @@ function Dispatcher:_addItem(caller, menu, location, settings, section)
then then
if settingsList[k].category == "none" or settingsList[k].category == "arg" then if settingsList[k].category == "none" or settingsList[k].category == "arg" then
table.insert(menu, { table.insert(menu, {
text = settingsList[k].title, text = settingsList[k].title,
checked_func = function() checked_func = function()
return location[settings] ~= nil and location[settings][k] ~= nil return location[settings] ~= nil and location[settings][k] ~= nil
end, end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
if location[settings] == nil then if location[settings] == nil then
location[settings] = {} location[settings] = {}
end end
if location[settings][k] then if location[settings][k] then
location[settings][k] = nil location[settings][k] = nil
Dispatcher:_removeFromOrder(location, settings, k) Dispatcher:_removeFromOrder(location, settings, k)
else else
location[settings][k] = true location[settings][k] = true
Dispatcher:_addToOrder(location, settings, k) Dispatcher:_addToOrder(location, settings, k)
end end
caller.updated = true caller.updated = true
if touchmenu_instance then touchmenu_instance:updateItems() end if touchmenu_instance then touchmenu_instance:updateItems() end
end, end,
separator = settingsList[k].separator, separator = settingsList[k].separator,
}) })
@ -666,7 +666,7 @@ function Dispatcher:_addItem(caller, menu, location, settings, section)
return Dispatcher:getNameFromItem(k, location[settings]) return Dispatcher:getNameFromItem(k, location[settings])
end, end,
checked_func = function() checked_func = function()
return location[settings] ~= nil and location[settings][k] ~= nil return location[settings] ~= nil and location[settings][k] ~= nil
end, end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
local SpinWidget = require("ui/widget/spinwidget") local SpinWidget = require("ui/widget/spinwidget")
@ -714,7 +714,7 @@ function Dispatcher:_addItem(caller, menu, location, settings, section)
return Dispatcher:getNameFromItem(k, location[settings]) return Dispatcher:getNameFromItem(k, location[settings])
end, end,
checked_func = function() checked_func = function()
return location[settings] ~= nil and location[settings][k] ~= nil return location[settings] ~= nil and location[settings][k] ~= nil
end, end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
local _ = require("gettext") local _ = require("gettext")

@ -681,7 +681,7 @@ function TouchMenu:updateItems()
if item_tmp:isEnabled() then if item_tmp:isEnabled() then
table.insert(self.layout, {[self.cur_tab] = item_tmp}) -- for the focusmanager table.insert(self.layout, {[self.cur_tab] = item_tmp}) -- for the focusmanager
end 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 -- insert split line
table.insert(self.item_group, self.split_line) table.insert(self.item_group, self.split_line)
end end

@ -59,7 +59,7 @@ local function dispatcherRegisterProfile(name)
{category="none", event="ProfileExecute", arg=name, title=T(_("Profile %1"), name), general=true}) {category="none", event="ProfileExecute", arg=name, title=T(_("Profile %1"), name), general=true})
end end
local function dispatcherRemoveProfile(name) local function dispatcherUnregisterProfile(name)
Dispatcher:removeAction("profile_exec_"..name) Dispatcher:removeAction("profile_exec_"..name)
end end
@ -143,7 +143,7 @@ function Profiles:getSubMenuItems()
end, end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
if v.settings.registered then if v.settings.registered then
dispatcherRemoveProfile(k) dispatcherUnregisterProfile(k)
self.data[k].settings.registered = nil self.data[k].settings.registered = nil
else else
dispatcherRegisterProfile(k) dispatcherRegisterProfile(k)
@ -166,7 +166,7 @@ function Profiles:getSubMenuItems()
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
local function editCallback(new_name) local function editCallback(new_name)
if v.settings.registered then if v.settings.registered then
dispatcherRemoveProfile(k) dispatcherUnregisterProfile(k)
dispatcherRegisterProfile(new_name) dispatcherRegisterProfile(new_name)
end end
self:renameAutostart(k, new_name) self:renameAutostart(k, new_name)
@ -205,7 +205,7 @@ function Profiles:getSubMenuItems()
ok_text = _("Delete"), ok_text = _("Delete"),
ok_callback = function() ok_callback = function()
if v.settings.registered then if v.settings.registered then
dispatcherRemoveProfile(k) dispatcherUnregisterProfile(k)
end end
self:renameAutostart(k) self:renameAutostart(k)
self.data[k] = nil self.data[k] = nil

Loading…
Cancel
Save