From 07713be4299e15a1e52c091d25581cf52fd6ef38 Mon Sep 17 00:00:00 2001 From: poire-z Date: Mon, 21 May 2018 01:19:36 +0200 Subject: [PATCH] Adds "Better rendering of list items" to Style tweaks (#3968) Alternate rendering of list items as block elements (instead of inline/final). Check the built DOM is still coherent with styles on stylesheet change, and propose to reload the document if not. Also invalidate cache on close in that case so a new DOM is built at next opening. ReaderStyleTweak: fix tweak css duplication; when globally enabled, when disabling and re-enabling locally, the css would be added twice to the final CSS. Also adds !important to "sub_sup_smaller" tweak, and increase its priority so it can override "font_size_all_inherit". Adds CreDocument:getCacheFilePath() for futur features. --- base | 2 +- frontend/apps/reader/modules/readermenu.lua | 8 ++- .../apps/reader/modules/readerrolling.lua | 31 +++++++++ .../apps/reader/modules/readerstyletweak.lua | 6 +- frontend/apps/reader/readerui.lua | 13 ++++ frontend/document/credocument.lua | 16 +++++ frontend/ui/data/css_tweaks.lua | 64 +++++++++++-------- 7 files changed, 107 insertions(+), 33 deletions(-) diff --git a/base b/base index c047556ae..354d71a97 160000 --- a/base +++ b/base @@ -1 +1 @@ -Subproject commit c047556aef960d71ef2ef1b2fb5b85df0326bf27 +Subproject commit 354d71a97837708e4f91a69a8ceaf47077ce2cf9 diff --git a/frontend/apps/reader/modules/readermenu.lua b/frontend/apps/reader/modules/readermenu.lua index 9d5628f41..b7263ee30 100644 --- a/frontend/apps/reader/modules/readermenu.lua +++ b/frontend/apps/reader/modules/readermenu.lua @@ -274,9 +274,11 @@ function ReaderMenu:onShowReaderMenu(tab_index) end function ReaderMenu:onCloseReaderMenu() - self.last_tab_index = self.menu_container[1].last_index - self:onSaveSettings() - UIManager:close(self.menu_container) + if self.menu_container then + self.last_tab_index = self.menu_container[1].last_index + self:onSaveSettings() + UIManager:close(self.menu_container) + end return true end diff --git a/frontend/apps/reader/modules/readerrolling.lua b/frontend/apps/reader/modules/readerrolling.lua index 54813f235..d61eb106b 100644 --- a/frontend/apps/reader/modules/readerrolling.lua +++ b/frontend/apps/reader/modules/readerrolling.lua @@ -1,4 +1,5 @@ local Blitbuffer = require("ffi/blitbuffer") +local ConfirmBox = require("ui/widget/confirmbox") local Device = require("device") local InputContainer = require("ui/widget/container/inputcontainer") local Event = require("ui/event") @@ -179,6 +180,30 @@ end -- we cannot do it in onSaveSettings() because getLastPercent() uses self.ui.document function ReaderRolling:onCloseDocument() self.ui.doc_settings:saveSetting("percent_finished", self:getLastPercent()) + -- also checks if DOM is coherent with styles; if not, invalidate the + -- cache, so a new DOM is built on next opening + if self.ui.document:isBuiltDomStale() then + if self.ui.document:hasCacheFile() then + logger.dbg("cre DOM may not be in sync with styles, invalidating cache file for a full reload at next opening") + self.ui.document:invalidateCacheFile() + end + end + logger.dbg("cre cache used:", self.ui.document:getCacheFilePath() or "none") +end + +function ReaderRolling:onCheckDomStyleCoherence() + if self.ui.document:isBuiltDomStale() then + UIManager:show(ConfirmBox:new{ + text = _("Styles have changed in such a way that fully reloading the document may be needed for a correct rendering.\nDo you want to reload the document?"), + ok_callback = function() + -- Allow for ConfirmBox to be closed before showing + -- "Opening file" InfoMessage + UIManager:scheduleIn(0.5, function () + self.ui:reloadDocument() + end) + end, + }) + end end function ReaderRolling:onSaveSettings() @@ -566,6 +591,12 @@ function ReaderRolling:updatePos() self.view.footer:updateFooter() end UIManager:setDirty(self.view.dialog, "partial") + -- Allow for the new rendering to be shown before possibly showing + -- the "Styles have changes..." ConfirmBox so the user can decide + -- if it is really needed + UIManager:scheduleIn(0.1, function () + self:onCheckDomStyleCoherence() + end) end --[[ diff --git a/frontend/apps/reader/modules/readerstyletweak.lua b/frontend/apps/reader/modules/readerstyletweak.lua index 748120be2..b997f8247 100644 --- a/frontend/apps/reader/modules/readerstyletweak.lua +++ b/frontend/apps/reader/modules/readerstyletweak.lua @@ -263,8 +263,10 @@ function ReaderStyleTweak:updateCssText(apply) local tweaks = {} for id, enabled in pairs(self.global_tweaks) do -- there are only enabled tweaks in global_tweaks, but we don't - -- use them if they are disabled in doc_tweaks - if self.doc_tweaks[id] ~= false then + -- add them here if they appear in doc_tweaks (if enabled in + -- doc_tweaks, they'll be added below; if disabled, they should + -- not be added) + if self.doc_tweaks[id] == nil then table.insert(tweaks, self.tweaks_by_id[id]) end end diff --git a/frontend/apps/reader/readerui.lua b/frontend/apps/reader/readerui.lua index 03aae8795..c0e74049c 100644 --- a/frontend/apps/reader/readerui.lua +++ b/frontend/apps/reader/readerui.lua @@ -650,4 +650,17 @@ function ReaderUI:onHome() return true end +function ReaderUI:reloadDocument(after_close_callback) + local file = self.document.file + local provider = getmetatable(self.document).__index + self:handleEvent(Event:new("CloseReaderMenu")) + self:handleEvent(Event:new("CloseConfigMenu")) + self:onClose() + if after_close_callback then + -- allow caller to do stuff between close an re-open + after_close_callback(file, provider) + end + self:showReader(file, provider) +end + return ReaderUI diff --git a/frontend/document/credocument.lua b/frontend/document/credocument.lua index d7589599f..b015a49d3 100644 --- a/frontend/document/credocument.lua +++ b/frontend/document/credocument.lua @@ -589,6 +589,22 @@ function CreDocument:enableInternalHistory(toggle) self._document:setIntProperty("crengine.highlight.bookmarks", toggle and 2 or 0) end +function CreDocument:isBuiltDomStale() + return self._document:isBuiltDomStale() +end + +function CreDocument:hasCacheFile() + return self._document:hasCacheFile() +end + +function CreDocument:invalidateCacheFile() + self._document:invalidateCacheFile() +end + +function CreDocument:getCacheFilePath() + return self._document:getCacheFilePath() +end + function CreDocument:register(registry) registry:addProvider("azw", "application/vnd.amazon.mobi8-ebook", self, 90) registry:addProvider("chm", "application/vnd.ms-htmlhelp", self, 90) diff --git a/frontend/ui/data/css_tweaks.lua b/frontend/ui/data/css_tweaks.lua index 65c96dce8..b09bcac82 100644 --- a/frontend/ui/data/css_tweaks.lua +++ b/frontend/ui/data/css_tweaks.lua @@ -34,15 +34,40 @@ local CssTweaks = { }, { title = _("Text"), + { + title = _("Links color and weight"), + { + id = "a_black"; + title = _("Links always black"), + css = [[a { color: black !important; }]], + }, + { + id = "a_blue"; + title = _("Links always blue"), + css = [[a { color: blue !important; }]], + separator = true, + }, + { + id = "a_bold"; + title = _("Links always bold"), + css = [[a { font-weight: bold !important; }]], + }, + { + id = "a_not_bold"; + title = _("Links never bold"), + css = [[a { font-weight: normal !important; }]], + }, + }, { id = "sub_sup_smaller"; title = _("Smaller sub- and superscript"), description = _("Prevent sub- and superscript from affecting line-height."), + priority = 5, -- so we can override "font_size_all_inherit" -- https://friendsofepub.github.io/eBookTricks/ -- https://github.com/koreader/koreader/issues/3923#issuecomment-386510294 css = [[ -sup { font-size: 50%; vertical-align: super; } -sub { font-size: 50%; vertical-align: middle; } +sup { font-size: 50% !important; vertical-align: super !important; } +sub { font-size: 50% !important; vertical-align: middle !important; } ]], separator = true, }, @@ -66,30 +91,6 @@ sub { font-size: 50%; vertical-align: middle; } css = [[* { font-size: inherit !important; }]], separator = true, }, - { - title = _("Links color and weight"), - { - id = "a_black"; - title = _("Links always black"), - css = [[a { color: black !important; }]], - }, - { - id = "a_blue"; - title = _("Links always blue"), - css = [[a { color: blue !important; }]], - separator = true, - }, - { - id = "a_bold"; - title = _("Links always bold"), - css = [[a { font-weight: bold !important; }]], - }, - { - id = "a_not_bold"; - title = _("Links never bold"), - css = [[a { font-weight: normal !important; }]], - }, - }, }, { title = _("Miscellaneous"), @@ -129,12 +130,20 @@ img { title = _("Workarounds"), { id = "html_tags_fix"; - title = _("Fix some HTML elements"), + title = _("Correct handling of some HTML elements"), description = _("Make some HTML elements (eg: ) behave as they should (inline/block).\nThis may break past bookmarks and highlights."), css = [[ cite { display: inline; font-style: italic; } ]], }, + { + id = "list_item_block"; + title = _("Better rendering of list items"), + description = _("Correctly render list items as block elements.\nThis may break past bookmarks and highlights."), + css = [[ +li {display: -cr-list-item-block; } + ]], + }, { id = "list_items_fix"; title = _("Fix some list items issues"), @@ -143,6 +152,7 @@ cite { display: inline; font-style: italic; } li > p:first-child { display: inline !important; } li > div:first-child { display: inline !important; } ]], + separator = true, }, { id = "border_all_none";