From ef30f2131237f3615e8817be70703774b251663f Mon Sep 17 00:00:00 2001 From: poire-z Date: Sun, 5 May 2024 20:59:18 +0200 Subject: [PATCH] ReaderSearch: delay adding to location stack until we change page Search may highlight matches on the current page, and if there are no others in the book, we would stay there and resume reading; but we would get a spurious previous location (ie. in Book map) that we would not expect keeping, and we may accidently go back to previous location, losing our reading position. Avoid this by only adding current page to location stack when actually leaving the current page. Because of complications with other reading modes, this is currently only enabled for CRE document when in page mode. --- frontend/apps/reader/modules/readerlink.lua | 4 ++++ frontend/apps/reader/modules/readersearch.lua | 23 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/frontend/apps/reader/modules/readerlink.lua b/frontend/apps/reader/modules/readerlink.lua index 88ce4b29f..8722c9ebc 100644 --- a/frontend/apps/reader/modules/readerlink.lua +++ b/frontend/apps/reader/modules/readerlink.lua @@ -712,6 +712,10 @@ function ReaderLink:addCurrentLocationToStack(loc) table.insert(self.location_stack, location) end +function ReaderLink:popFromLocationStack() + return table.remove(self.location_stack) +end + function ReaderLink:onClearLocationStack(show_notification) self.location_stack = {} self:onClearForwardLocationStack() diff --git a/frontend/apps/reader/modules/readersearch.lua b/frontend/apps/reader/modules/readersearch.lua index acd0a5c9f..59b70435d 100644 --- a/frontend/apps/reader/modules/readersearch.lua +++ b/frontend/apps/reader/modules/readersearch.lua @@ -289,6 +289,9 @@ function ReaderSearch:onShowSearchDialog(text, direction, regex, case_insensitiv local res = search_func(self, search_term, param, regex, case_insensitive) if res then if self.ui.paging then + if not current_page then -- initial search + current_page = self.ui.paging.current_page + end no_results = false self.ui.link:onGotoLink({page = res.page - 1}, neglect_current_location) self.view.highlight.temp[res.page] = res @@ -357,8 +360,24 @@ function ReaderSearch:onShowSearchDialog(text, direction, regex, case_insensitiv self.ui.link:onGotoLink({xpointer=valid_link}, neglect_current_location) end end - -- Don't add result pages to location ("Go back") stack - neglect_current_location = true + if not neglect_current_location then + -- Initial search: onGotoLink() has added the current page to the location stack, + -- and we don't want this to be done when showing further pages with results. + -- But if this initial search is showing results on the current page, we don't want + -- the original page added: we will do it when we jump to a different page. + -- For now, only do this with CreDocument. With PDF, whether in single page mode or + -- in scroll mode, the view can scroll a bit when showing results, and we want to + -- allow "go back" to restore the original viewport. + if self.ui.rolling and self.view.view_mode == "page" then + if current_page == (self.ui.rolling and self.ui.document:getCurrentPage() or self.ui.paging.current_page) then + self.ui.link:popFromLocationStack() + neglect_current_location = false + else + -- We won't add further result pages to the location stack ("Go back"). + neglect_current_location = true + end + end + end end if no_results then local notification_text