From 5406fdf2e038da4eb2e510732bfa1b195971a6c5 Mon Sep 17 00:00:00 2001 From: poire-z Date: Sun, 8 Oct 2023 17:51:52 +0200 Subject: [PATCH] BookMap/PageBrowser: update upper instance when leaving lower one BookMap and PageBrowser instances can be stacked; when toggling a bookmark in a lower PageBrowser and closing it, make sure the bookmark is shown in the BookMap we get back to. Also consider TOC and hidden flows as a thing that can be edited and needs refreshing, so we're ready for next commits about custom TOC and custom hidden flows. Also update Reader footer when closing the last BookMap or PageBrowser. --- frontend/ui/widget/bookmapwidget.lua | 51 ++++++++++++++------- frontend/ui/widget/pagebrowserwidget.lua | 58 ++++++++++++++++-------- 2 files changed, 74 insertions(+), 35 deletions(-) diff --git a/frontend/ui/widget/bookmapwidget.lua b/frontend/ui/widget/bookmapwidget.lua index 7391dc9d0..6e1f4645c 100644 --- a/frontend/ui/widget/bookmapwidget.lua +++ b/frontend/ui/widget/bookmapwidget.lua @@ -760,16 +760,34 @@ function BookMapWidget:init() -- on some events (ie. TocUpdated, PageUpdate, AddHhighlight...) -- Get some info that shouldn't change across calls to update() self.nb_pages = self.ui.document:getPageCount() - self.ui.toc:fillToc() self.cur_page = self.ui.toc.pageno - self.max_toc_depth = self.ui.toc.toc_depth - -- Get bookmarks and highlights from ReaderBookmark - self.bookmarked_pages = self.ui.bookmark:getBookmarkedPages() -- Get read page from the statistics plugin if enabled self.statistics_enabled = self.ui.statistics and self.ui.statistics:isEnabled() self.read_pages = self.ui.statistics and self.ui.statistics:getCurrentBookReadPages() self.current_session_duration = self.ui.statistics and (os.time() - self.ui.statistics.start_current_period) - -- Hidden flows, for first page display, and to draw them gray + -- Reference page numbers, for first row page display + self.page_labels = nil + if self.ui.pagemap and self.ui.pagemap:wantsPageLabels() then + self.page_labels = self.ui.document:getPageMap() + end + -- Location stack + self.previous_locations = self.ui.link:getPreviousLocationPages() + + -- Update stuff that may be updated by the user while in PageBrowser + self:updateEditableStuff() + self.editable_stuff_edited = false -- reset this + + -- Compute settings-dependant sizes and options, and build the inner widgets + self:update() +end + +function BookMapWidget:updateEditableStuff(update_view) + -- Toc, bookmarks and hidden flows may be edited + self.ui.toc:fillToc() + self.max_toc_depth = self.ui.toc.toc_depth + -- Get bookmarks and highlights from ReaderBookmark + self.bookmarked_pages = self.ui.bookmark:getBookmarkedPages() + self.hidden_flows = nil self.has_hidden_flows = self.ui.document:hasHiddenFlows() if self.has_hidden_flows and #self.ui.document.flows > 0 then self.hidden_flows = {} @@ -779,16 +797,13 @@ function BookMapWidget:init() table.insert(self.hidden_flows, { tab[1], tab[1]+tab[2]-1 }) end end - -- Reference page numbers, for first row page display - self.page_labels = nil - if self.ui.pagemap and self.ui.pagemap:wantsPageLabels() then - self.page_labels = self.ui.document:getPageMap() + -- Keep a flag so we can propagate the fact that editable stuff + -- has been updated to our parent/launcher when we will close, + -- so they can update themselves too. + self.editable_stuff_edited = true + if update_view then + self:update() end - -- Location stack - self.previous_locations = self.ui.link:getPreviousLocationPages() - - -- Compute settings-dependant sizes and options, and build the inner widgets - self:update() end function BookMapWidget:update() @@ -1362,6 +1377,9 @@ function BookMapWidget:onClose(close_all_parents) -- will do the cleanup below. self.launcher:onClose(true) else + if self.editable_stuff_edited then + self.launcher:updateEditableStuff(true) + end UIManager:setDirty(self.launcher, "ui") end else @@ -1377,8 +1395,9 @@ function BookMapWidget:onClose(close_all_parents) collectgarbage() collectgarbage() end) - -- As we're getting back to Reader, do a full flashing refresh to remove - -- any ghost trace of thumbnails or black page slots + -- As we're getting back to Reader, update the footer and do a full flashing + -- refresh to remove any ghost trace of thumbnails or black page slots + UIManager:broadcastEvent(Event:new("UpdateFooter")) UIManager:setDirty(self.ui.dialog, "full") end return true diff --git a/frontend/ui/widget/pagebrowserwidget.lua b/frontend/ui/widget/pagebrowserwidget.lua index a229f0dcb..bc38326ba 100644 --- a/frontend/ui/widget/pagebrowserwidget.lua +++ b/frontend/ui/widget/pagebrowserwidget.lua @@ -163,16 +163,38 @@ function PageBrowserWidget:init() self.max_nb_cols = 6 -- Get some info that shouldn't change across calls to update() and updateLayout() - self.ui.toc:fillToc() - self.max_toc_depth = self.ui.toc.toc_depth self.nb_pages = self.ui.document:getPageCount() self.cur_page = self.ui.toc.pageno - -- Get bookmarks and highlights from ReaderBookmark - self.bookmarked_pages = self.ui.bookmark:getBookmarkedPages() -- Get read page from the statistics plugin if enabled self.read_pages = self.ui.statistics and self.ui.statistics:getCurrentBookReadPages() self.current_session_duration = self.ui.statistics and (os.time() - self.ui.statistics.start_current_period) + -- Reference page numbers, for first row page display + self.page_labels = nil + if self.ui.pagemap and self.ui.pagemap:wantsPageLabels() then + self.page_labels = self.ui.document:getPageMap() + end + -- Location stack + self.previous_locations = self.ui.link:getPreviousLocationPages() + + -- Update stuff that may be updated by the user while in PageBrowser + self:updateEditableStuff() + self.editable_stuff_edited = false -- reset this + + -- Compute settings-dependant sizes and options, and build the inner widgets + -- (this will call self:update()) + self:updateLayout() +end + +function PageBrowserWidget:updateEditableStuff(update_view) + -- Toc, bookmarks and hidden flows may be edited + -- Note: we update everything to keep things simpler, but we could provide flags to + -- let us know what stuff has been updated and only do their related work. + self.ui.toc:fillToc() + self.max_toc_depth = self.ui.toc.toc_depth + -- Get bookmarks and highlights from ReaderBookmark + self.bookmarked_pages = self.ui.bookmark:getBookmarkedPages() -- Hidden flows, for first page display, and to draw them gray + self.hidden_flows = nil self.has_hidden_flows = self.ui.document:hasHiddenFlows() if self.has_hidden_flows and #self.ui.document.flows > 0 then self.hidden_flows = {} @@ -182,17 +204,13 @@ function PageBrowserWidget:init() table.insert(self.hidden_flows, { tab[1], tab[1]+tab[2]-1 }) end end - -- Reference page numbers, for first row page display - self.page_labels = nil - if self.ui.pagemap and self.ui.pagemap:wantsPageLabels() then - self.page_labels = self.ui.document:getPageMap() + -- Keep a flag so we can propagate the fact that editable stuff + -- has been updated to our parent/launcher when we will close, + -- so they can update themselves too. + self.editable_stuff_edited = true + if update_view then + self:updateLayout() end - -- Location stack - self.previous_locations = self.ui.link:getPreviousLocationPages() - - -- Compute settings-dependant sizes and options, and build the inner widgets - -- (this will call self:update()) - self:updateLayout() end function PageBrowserWidget:updateLayout() @@ -1053,6 +1071,9 @@ function PageBrowserWidget:onClose(close_all_parents) -- will do the cleanup below. self.launcher:onClose(true) else + if self.editable_stuff_edited then + self.launcher:updateEditableStuff(true) + end UIManager:setDirty(self.launcher, "ui") end else @@ -1068,8 +1089,9 @@ function PageBrowserWidget:onClose(close_all_parents) collectgarbage() collectgarbage() end) - -- As we're getting back to Reader, do a full flashing refresh to remove - -- any ghost trace of thumbnails or black page slots + -- As we're getting back to Reader, update the footer and do a full flashing + -- refresh to remove any ghost trace of thumbnails or black page slots + UIManager:broadcastEvent(Event:new("UpdateFooter")) UIManager:setDirty(self.ui.dialog, "full") end return true @@ -1468,9 +1490,7 @@ function PageBrowserWidget:onHold(arg, ges) -- we may get (and cache) a thumbnail showing the wrong -- bookmark state... self.ui.bookmark:toggleBookmark(page) - -- Update our cached bookmarks info and ensure the bottom ribbon is redrawn - self.bookmarked_pages = self.ui.bookmark:getBookmarkedPages() - self:updateLayout() + self:updateEditableStuff(true) return true end break