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")
@ -122,6 +123,16 @@ function TweakInfoWidget:init()
} }
local buttons = { local buttons = {
{
{
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 = _("Close"), text = _("Close"),
callback = function() callback = function()
@ -135,13 +146,14 @@ function TweakInfoWidget:init()
UIManager:close(self) UIManager:close(self)
end, end,
}, },
},
} }
local button_table = ButtonTable:new{ local button_table = ButtonTable:new{
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,
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 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},

@ -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