mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
fix(readerlink): restore reader view context on go back
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -8,7 +8,7 @@ lua-*
|
||||
.vimrc
|
||||
*.o
|
||||
tags
|
||||
test/*
|
||||
test/*.sdr
|
||||
*.tar
|
||||
*.log
|
||||
spec/unit/data
|
||||
|
||||
@@ -9,7 +9,7 @@ local Event = require("ui/event")
|
||||
local _ = require("gettext")
|
||||
|
||||
local ReaderLink = InputContainer:new{
|
||||
link_states = {}
|
||||
location_stack = {}
|
||||
}
|
||||
|
||||
function ReaderLink:init()
|
||||
@@ -23,7 +23,7 @@ end
|
||||
|
||||
function ReaderLink:onReadSettings(config)
|
||||
-- called when loading new document
|
||||
self.link_states = {}
|
||||
self.location_stack = {}
|
||||
end
|
||||
|
||||
function ReaderLink:initGesListener()
|
||||
@@ -77,7 +77,7 @@ function ReaderLink:addToMainMenu(tab_item_table)
|
||||
},
|
||||
{
|
||||
text = _("Go back"),
|
||||
enabled_func = function() return #self.link_states > 0 end,
|
||||
enabled_func = function() return #self.location_stack > 0 end,
|
||||
callback = function() self:onGoBackLink() end,
|
||||
},
|
||||
{
|
||||
@@ -99,7 +99,7 @@ function ReaderLink:onSetDimensions(dimen)
|
||||
end
|
||||
end
|
||||
|
||||
function ReaderLink:onTap(arg, ges)
|
||||
function ReaderLink:onTap(_, ges)
|
||||
if not is_follow_links_on() then return end
|
||||
if self.ui.document.info.has_pages then
|
||||
local pos = self.view:screenToPageTransform(ges.pos)
|
||||
@@ -130,25 +130,24 @@ end
|
||||
|
||||
function ReaderLink:onGotoLink(link)
|
||||
if self.ui.document.info.has_pages then
|
||||
table.insert(self.link_states, self.view.state.page)
|
||||
table.insert(self.location_stack, self.ui.paging:getBookLocation())
|
||||
self.ui:handleEvent(Event:new("GotoPage", link.page + 1))
|
||||
else
|
||||
table.insert(self.link_states, self.ui.document:getXPointer())
|
||||
table.insert(self.location_stack, self.ui.rolling:getBookLocation())
|
||||
self.ui:handleEvent(Event:new("GotoXPointer", link))
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderLink:onGoBackLink()
|
||||
local last_page_or_xp = table.remove(self.link_states)
|
||||
if last_page_or_xp then
|
||||
local event = self.ui.document.info.has_pages and "GotoPage" or "GotoXPointer"
|
||||
self.ui:handleEvent(Event:new(event, last_page_or_xp))
|
||||
return true
|
||||
local saved_location = table.remove(self.location_stack)
|
||||
if saved_location then
|
||||
self.ui:handleEvent(Event:new('RestoreBookLocation', saved_location))
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderLink:onSwipe(arg, ges)
|
||||
function ReaderLink:onSwipe(_, ges)
|
||||
if ges.direction == "east" and swipe_to_go_back() then
|
||||
return self:onGoBackLink()
|
||||
end
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
local InputContainer = require("ui/widget/container/inputcontainer")
|
||||
local Screen = require("device").screen
|
||||
local Geom = require("ui/geometry")
|
||||
local Input = require("device").input
|
||||
local GestureRange = require("ui/gesturerange")
|
||||
local Device = require("device")
|
||||
local Screen = Device.screen
|
||||
local Event = require("ui/event")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local Math = require("optmath")
|
||||
@@ -121,7 +121,7 @@ end
|
||||
|
||||
function ReaderPaging:onReadSettings(config)
|
||||
self.page_positions = config:readSetting("page_positions") or {}
|
||||
self:gotoPage(config:readSetting("last_page") or 1)
|
||||
self:_gotoPage(config:readSetting("last_page") or 1)
|
||||
self.show_overlap_enable = config:readSetting("show_overlap_enable")
|
||||
if self.show_overlap_enable == nil then
|
||||
self.show_overlap_enable = DSHOWOVERLAP
|
||||
@@ -131,6 +131,7 @@ function ReaderPaging:onReadSettings(config)
|
||||
end
|
||||
|
||||
function ReaderPaging:onSaveSettings()
|
||||
-- TODO: only save current_page page position
|
||||
self.ui.doc_settings:saveSetting("page_positions", self.page_positions)
|
||||
self.ui.doc_settings:saveSetting("last_page", self:getTopPage())
|
||||
self.ui.doc_settings:saveSetting("percent_finished", self:getLastPercent())
|
||||
@@ -181,9 +182,10 @@ end
|
||||
|
||||
--[[
|
||||
Set reading position on certain page
|
||||
Page position is a fractional number ranging from 0 to 1, indicating the read percentage on
|
||||
certain page. With the position information on each page whenever users change font size,
|
||||
page margin or line spacing or close and reopen the book, the page view will be roughly the same.
|
||||
Page position is a fractional number ranging from 0 to 1, indicating the read
|
||||
percentage on certain page. With the position information on each page whenever
|
||||
users change font size, page margin or line spacing or close and reopen the
|
||||
book, the page view will be roughly the same.
|
||||
--]]
|
||||
function ReaderPaging:setPagePosition(page, pos)
|
||||
DEBUG("set page position", pos)
|
||||
@@ -244,7 +246,7 @@ function ReaderPaging:onToggleBookmarkFlipping()
|
||||
self.view.flipping_visible = self.orig_flipping_mode
|
||||
self.view.dogear_visible = self.orig_dogear_mode
|
||||
self:exitFlippingMode()
|
||||
self:gotoPage(self.bm_flipping_orig_page)
|
||||
self:_gotoPage(self.bm_flipping_orig_page)
|
||||
end
|
||||
self.ui:handleEvent(Event:new("SetHinting", not self.bookmark_flipping_mode))
|
||||
self.ui:handleEvent(Event:new("ReZoom"))
|
||||
@@ -292,13 +294,13 @@ function ReaderPaging:pageFlipping(flipping_page, flipping_ges)
|
||||
local stp_proportion = flipping_ges.distance / Screen:getWidth()
|
||||
local abs_proportion = flipping_ges.distance / Screen:getHeight()
|
||||
if flipping_ges.direction == "east" then
|
||||
self:gotoPage(flipping_page - self.flip_steps[math.ceil(steps*stp_proportion)])
|
||||
self:_gotoPage(flipping_page - self.flip_steps[math.ceil(steps*stp_proportion)])
|
||||
elseif flipping_ges.direction == "west" then
|
||||
self:gotoPage(flipping_page + self.flip_steps[math.ceil(steps*stp_proportion)])
|
||||
self:_gotoPage(flipping_page + self.flip_steps[math.ceil(steps*stp_proportion)])
|
||||
elseif flipping_ges.direction == "south" then
|
||||
self:gotoPage(flipping_page - math.floor(whole*abs_proportion))
|
||||
self:_gotoPage(flipping_page - math.floor(whole*abs_proportion))
|
||||
elseif flipping_ges.direction == "north" then
|
||||
self:gotoPage(flipping_page + math.floor(whole*abs_proportion))
|
||||
self:_gotoPage(flipping_page + math.floor(whole*abs_proportion))
|
||||
end
|
||||
UIManager:setDirty(self.view.dialog, "partial")
|
||||
end
|
||||
@@ -316,7 +318,7 @@ function ReaderPaging:onSwipe(arg, ges)
|
||||
if self.bookmark_flipping_mode then
|
||||
self:bookmarkFlipping(self.current_page, ges)
|
||||
elseif self.page_flipping_mode and self.original_page then
|
||||
self:gotoPage(self.original_page)
|
||||
self:_gotoPage(self.original_page)
|
||||
elseif ges.direction == "west" then
|
||||
self:onPagingRel(1)
|
||||
elseif ges.direction == "east" then
|
||||
@@ -362,10 +364,10 @@ function ReaderPaging:onZoomModeUpdate(new_mode)
|
||||
self.zoom_mode = new_mode
|
||||
end
|
||||
|
||||
function ReaderPaging:onPageUpdate(new_page_no, orig)
|
||||
function ReaderPaging:onPageUpdate(new_page_no, orig_mode)
|
||||
self.current_page = new_page_no
|
||||
if orig ~= "scrolling" then
|
||||
self.ui:handleEvent(Event:new("InitScrollPageStates", orig))
|
||||
if self.view.page_scroll and orig_mode ~= "scrolling" then
|
||||
self.ui:handleEvent(Event:new("InitScrollPageStates", orig_mode))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -382,7 +384,7 @@ function ReaderPaging:onGotoPercent(percent)
|
||||
if dest > self.number_of_pages then
|
||||
dest = self.number_of_pages
|
||||
end
|
||||
self:gotoPage(dest)
|
||||
self:_gotoPage(dest)
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -404,6 +406,26 @@ function ReaderPaging:onPanningRel(diff)
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderPaging:getBookLocation()
|
||||
return self.view:getViewContext()
|
||||
end
|
||||
|
||||
function ReaderPaging:onRestoreBookLocation(saved_location)
|
||||
if self.view.page_scroll then
|
||||
self.view:restoreViewContext(saved_location)
|
||||
self:_gotoPage(self.view.page_states[#self.view.page_states].page,
|
||||
"scrolling")
|
||||
else
|
||||
-- gotoPage will emit PageUpdate event, which will trigger recalculate
|
||||
-- in ReaderView and resets the view context. So we need to call
|
||||
-- restoreViewContext after gotoPage
|
||||
self:_gotoPage(saved_location[1].page)
|
||||
self.view:restoreViewContext(saved_location)
|
||||
end
|
||||
self:setPagePosition(self:getTopPage(), self:getTopPosition())
|
||||
return true
|
||||
end
|
||||
|
||||
--[[
|
||||
Get read percentage on current page.
|
||||
--]]
|
||||
@@ -428,8 +450,8 @@ function ReaderPaging:getTopPage()
|
||||
end
|
||||
end
|
||||
|
||||
function ReaderPaging:onInitScrollPageStates(orig)
|
||||
DEBUG("init scroll page states", orig)
|
||||
function ReaderPaging:onInitScrollPageStates(orig_mode)
|
||||
DEBUG("init scroll page states", orig_mode)
|
||||
if self.view.page_scroll and self.view.state.page then
|
||||
self.orig_page = self.current_page
|
||||
self.view.page_states = {}
|
||||
@@ -451,10 +473,10 @@ function ReaderPaging:onInitScrollPageStates(orig)
|
||||
blank_area.h = blank_area.h - self.view.page_gap.height
|
||||
end
|
||||
if blank_area.h > 0 then
|
||||
self:gotoPage(self.current_page + 1, "scrolling")
|
||||
self:_gotoPage(self.current_page + 1, "scrolling")
|
||||
end
|
||||
end
|
||||
self:gotoPage(self.orig_page, "scrolling")
|
||||
self:_gotoPage(self.orig_page, "scrolling")
|
||||
end
|
||||
return true
|
||||
end
|
||||
@@ -566,7 +588,7 @@ function ReaderPaging:genPageStatesFromTop(top_page_state, blank_area, offset)
|
||||
blank_area.h = blank_area.h - self.view.page_gap.height
|
||||
if blank_area.h > 0 then
|
||||
if self.current_page == self.number_of_pages then break end
|
||||
self:gotoPage(current_page + 1, "scrolling")
|
||||
self:_gotoPage(current_page + 1, "scrolling")
|
||||
current_page = current_page + 1
|
||||
local state = self:getNextPageState(blank_area, Geom:new{})
|
||||
table.insert(page_states, state)
|
||||
@@ -588,7 +610,7 @@ function ReaderPaging:genPageStatesFromBottom(bottom_page_state, blank_area, off
|
||||
blank_area.h = blank_area.h - self.view.page_gap.height
|
||||
if blank_area.h > 0 then
|
||||
if self.current_page == 1 then break end
|
||||
self:gotoPage(current_page - 1, "scrolling")
|
||||
self:_gotoPage(current_page - 1, "scrolling")
|
||||
current_page = current_page - 1
|
||||
local state = self:getPrevPageState(blank_area, Geom:new{})
|
||||
table.insert(page_states, 1, state)
|
||||
@@ -618,7 +640,7 @@ function ReaderPaging:onScrollPanRel(diff)
|
||||
self.view.page_states = self:genPageStatesFromBottom(last_page_state, blank_area, offset)
|
||||
end
|
||||
-- update current pageno to the very last part in current view
|
||||
self:gotoPage(self.view.page_states[#self.view.page_states].page, "scrolling")
|
||||
self:_gotoPage(self.view.page_states[#self.view.page_states].page, "scrolling")
|
||||
|
||||
UIManager:setDirty(self.view.dialog, "fast")
|
||||
end
|
||||
@@ -637,16 +659,16 @@ function ReaderPaging:onScrollPageRel(diff)
|
||||
table.insert(self.view.page_states, last_page_state)
|
||||
self.ui:handleEvent(Event:new("EndOfBook"))
|
||||
return true
|
||||
else
|
||||
local blank_area = Geom:new{}
|
||||
blank_area:setSizeTo(self.view.dimen)
|
||||
local overlap = self:calculateOverlap()
|
||||
local offset = Geom:new{
|
||||
x = 0,
|
||||
y = last_visible_area.h - overlap
|
||||
}
|
||||
self.view.page_states = self:genPageStatesFromTop(last_page_state, blank_area, offset)
|
||||
end
|
||||
|
||||
local blank_area = Geom:new{}
|
||||
blank_area:setSizeTo(self.view.dimen)
|
||||
local overlap = self:calculateOverlap()
|
||||
local offset = Geom:new{
|
||||
x = 0,
|
||||
y = last_visible_area.h - overlap
|
||||
}
|
||||
self.view.page_states = self:genPageStatesFromTop(last_page_state, blank_area, offset)
|
||||
elseif diff < 0 then
|
||||
local blank_area = Geom:new{}
|
||||
blank_area:setSizeTo(self.view.dimen)
|
||||
@@ -661,7 +683,7 @@ function ReaderPaging:onScrollPageRel(diff)
|
||||
return true
|
||||
end
|
||||
-- update current pageno to the very last part in current view
|
||||
self:gotoPage(self.view.page_states[#self.view.page_states].page, "scrolling")
|
||||
self:_gotoPage(self.view.page_states[#self.view.page_states].page, "scrolling")
|
||||
UIManager:setDirty(self.view.dialog, "partial")
|
||||
return true
|
||||
end
|
||||
@@ -701,7 +723,7 @@ function ReaderPaging:onGotoPageRel(diff)
|
||||
if diff > 0 and new_page == self.number_of_pages + 1 then
|
||||
self.ui:handleEvent(Event:new("EndOfBook"))
|
||||
else
|
||||
self:gotoPage(new_page)
|
||||
self:_gotoPage(new_page)
|
||||
end
|
||||
-- if we are going back to previous page, reset
|
||||
-- view area to bottom of previous page
|
||||
@@ -777,7 +799,7 @@ function ReaderPaging:onSetDimensions()
|
||||
end
|
||||
|
||||
-- wrapper for bounds checking
|
||||
function ReaderPaging:gotoPage(number, orig)
|
||||
function ReaderPaging:_gotoPage(number, orig_mode)
|
||||
if number == self.current_page or not number then
|
||||
return true
|
||||
end
|
||||
@@ -786,24 +808,24 @@ function ReaderPaging:gotoPage(number, orig)
|
||||
return false
|
||||
end
|
||||
-- this is an event to allow other controllers to be aware of this change
|
||||
self.ui:handleEvent(Event:new("PageUpdate", number, orig))
|
||||
self.ui:handleEvent(Event:new("PageUpdate", number, orig_mode))
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderPaging:onGotoPage(number)
|
||||
self:gotoPage(number)
|
||||
self:_gotoPage(number)
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderPaging:onGotoRelativePage(number)
|
||||
self:gotoPage(self.current_page + number)
|
||||
self:_gotoPage(self.current_page + number)
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderPaging:onGotoPercentage(percentage)
|
||||
if percentage < 0 then percentage = 0 end
|
||||
if percentage > 1 then percentage = 1 end
|
||||
self:gotoPage(math.floor(percentage*self.number_of_pages))
|
||||
self:_gotoPage(math.floor(percentage*self.number_of_pages))
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
@@ -174,7 +174,7 @@ function ReaderRolling:onReadSettings(config)
|
||||
if last_xp then
|
||||
table.insert(self.ui.postInitCallback, function()
|
||||
self.xpointer = last_xp
|
||||
self:gotoXPointer(self.xpointer)
|
||||
self:_gotoXPointer(self.xpointer)
|
||||
-- we have to do a real jump in self.ui.document._document to
|
||||
-- update status information in CREngine.
|
||||
self.ui.document:gotoXPointer(self.xpointer)
|
||||
@@ -182,7 +182,7 @@ function ReaderRolling:onReadSettings(config)
|
||||
-- we read last_percent just for backward compatibility
|
||||
elseif last_per then
|
||||
table.insert(self.ui.postInitCallback, function()
|
||||
self:gotoPercent(last_per)
|
||||
self:_gotoPercent(last_per)
|
||||
-- we have to do a real pos change in self.ui.document._document
|
||||
-- to update status information in CREngine.
|
||||
self.ui.document:gotoPos(self.current_pos)
|
||||
@@ -270,9 +270,9 @@ end
|
||||
function ReaderRolling:onPan(arg, ges)
|
||||
if self.view.view_mode == "scroll" then
|
||||
if ges.direction == "north" then
|
||||
self:gotoPos(self.current_pos + ges.distance)
|
||||
self:_gotoPos(self.current_pos + ges.distance)
|
||||
elseif ges.direction == "south" then
|
||||
self:gotoPos(self.current_pos - ges.distance)
|
||||
self:_gotoPos(self.current_pos - ges.distance)
|
||||
end
|
||||
end
|
||||
return true
|
||||
@@ -311,13 +311,14 @@ end
|
||||
|
||||
function ReaderRolling:onGotoPercent(percent)
|
||||
DEBUG("goto document offset in percent:", percent)
|
||||
self:gotoPercent(percent)
|
||||
self:_gotoPercent(percent)
|
||||
self.xpointer = self.ui.document:getXPointer()
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderRolling:onGotoPage(number)
|
||||
if number then
|
||||
self:gotoPage(number)
|
||||
self:_gotoPage(number)
|
||||
end
|
||||
self.xpointer = self.ui.document:getXPointer()
|
||||
return true
|
||||
@@ -325,18 +326,26 @@ end
|
||||
|
||||
function ReaderRolling:onGotoRelativePage(number)
|
||||
if number then
|
||||
self:gotoPage(self.current_page + number)
|
||||
self:_gotoPage(self.current_page + number)
|
||||
end
|
||||
self.xpointer = self.ui.document:getXPointer()
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderRolling:onGotoXPointer(xp)
|
||||
self:gotoXPointer(xp)
|
||||
self:_gotoXPointer(xp)
|
||||
self.xpointer = xp
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderRolling:getBookLocation()
|
||||
return self.xpointer
|
||||
end
|
||||
|
||||
function ReaderRolling:onRestoreBookLocation(saved_location)
|
||||
return self:onGotoXPointer(saved_location)
|
||||
end
|
||||
|
||||
function ReaderRolling:onGotoViewRel(diff)
|
||||
DEBUG("goto relative screen:", diff, ", in mode: ", self.view.view_mode)
|
||||
local prev_xp
|
||||
@@ -353,10 +362,10 @@ function ReaderRolling:onGotoViewRel(diff)
|
||||
pan_diff = pan_diff + self.overlap
|
||||
end
|
||||
end
|
||||
self:gotoPos(self.current_pos + pan_diff)
|
||||
self:_gotoPos(self.current_pos + pan_diff)
|
||||
elseif self.view.view_mode == "page" then
|
||||
local page_count = self.ui.document:getVisiblePageCount()
|
||||
self:gotoPage(self.current_page + diff*page_count)
|
||||
self:_gotoPage(self.current_page + diff*page_count)
|
||||
end
|
||||
self.xpointer = self.ui.document:getXPointer()
|
||||
if self.xpointer == prev_xp then
|
||||
@@ -369,7 +378,7 @@ function ReaderRolling:onPanning(args, key)
|
||||
--@TODO disable panning in page view_mode? 22.12 2012 (houqp)
|
||||
local _, dy = unpack(args)
|
||||
DEBUG("key =", key)
|
||||
self:gotoPos(self.current_pos + dy * self.panning_steps.normal)
|
||||
self:_gotoPos(self.current_pos + dy * self.panning_steps.normal)
|
||||
self.xpointer = self.ui.document:getXPointer()
|
||||
return true
|
||||
end
|
||||
@@ -396,7 +405,7 @@ function ReaderRolling:updatePos()
|
||||
local new_height = self.ui.document.info.doc_height
|
||||
local new_page = self.ui.document.info.number_of_pages
|
||||
if self.old_doc_height ~= new_height or self.old_page ~= new_page then
|
||||
self:gotoXPointer(self.xpointer)
|
||||
self:_gotoXPointer(self.xpointer)
|
||||
self.old_doc_height = new_height
|
||||
self.old_page = new_page
|
||||
self.ui:handleEvent(Event:new("UpdateToc"))
|
||||
@@ -413,10 +422,10 @@ function ReaderRolling:onChangeViewMode()
|
||||
self.old_page = self.ui.document.info.number_of_pages
|
||||
self.ui:handleEvent(Event:new("UpdateToc"))
|
||||
if self.xpointer then
|
||||
self:gotoXPointer(self.xpointer)
|
||||
self:_gotoXPointer(self.xpointer)
|
||||
else
|
||||
table.insert(self.ui.postInitCallback, function()
|
||||
self:gotoXPointer(self.xpointer)
|
||||
self:_gotoXPointer(self.xpointer)
|
||||
end)
|
||||
end
|
||||
return true
|
||||
@@ -449,7 +458,7 @@ end
|
||||
--[[
|
||||
PosUpdate event is used to signal other widgets that pos has been changed.
|
||||
--]]
|
||||
function ReaderRolling:gotoPos(new_pos)
|
||||
function ReaderRolling:_gotoPos(new_pos)
|
||||
if new_pos == self.current_pos then return end
|
||||
if new_pos < 0 then new_pos = 0 end
|
||||
if new_pos > self.doc_height then new_pos = self.doc_height end
|
||||
@@ -468,20 +477,20 @@ function ReaderRolling:gotoPos(new_pos)
|
||||
self.ui:handleEvent(Event:new("PosUpdate", new_pos))
|
||||
end
|
||||
|
||||
function ReaderRolling:gotoPercent(new_percent)
|
||||
self:gotoPos(new_percent * self.doc_height / 10000)
|
||||
function ReaderRolling:_gotoPercent(new_percent)
|
||||
self:_gotoPos(new_percent * self.doc_height / 10000)
|
||||
end
|
||||
|
||||
function ReaderRolling:gotoPage(new_page)
|
||||
function ReaderRolling:_gotoPage(new_page)
|
||||
self.ui.document:gotoPage(new_page)
|
||||
self.ui:handleEvent(Event:new("PageUpdate", self.ui.document:getCurrentPage()))
|
||||
end
|
||||
|
||||
function ReaderRolling:gotoXPointer(xpointer)
|
||||
function ReaderRolling:_gotoXPointer(xpointer)
|
||||
if self.view.view_mode == "page" then
|
||||
self:gotoPage(self.ui.document:getPageFromXPointer(xpointer))
|
||||
self:_gotoPage(self.ui.document:getPageFromXPointer(xpointer))
|
||||
else
|
||||
self:gotoPos(self.ui.document:getPosFromXPointer(xpointer))
|
||||
self:_gotoPos(self.ui.document:getPosFromXPointer(xpointer))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -623,7 +623,7 @@ function ReaderView:onSetFullScreen(full_screen)
|
||||
self.ui:handleEvent(Event:new("SetDimensions", Screen:getSize()))
|
||||
end
|
||||
|
||||
function ReaderView:onToggleScrollMode(page_scroll)
|
||||
function ReaderView:onSetScrollMode(page_scroll)
|
||||
self.page_scroll = page_scroll
|
||||
self:recalculate()
|
||||
self.ui:handleEvent(Event:new("InitScrollPageStates"))
|
||||
|
||||
@@ -56,7 +56,7 @@ local KoptOptions = {
|
||||
toggle = {S.ON, S.OFF},
|
||||
values = {1, 0},
|
||||
default_value = DSCROLL_MODE,
|
||||
event = "ToggleScrollMode",
|
||||
event = "SetScrollMode",
|
||||
args = {true, false},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
require("commonrequire")
|
||||
local DocumentRegistry = require("document/documentregistry")
|
||||
local ReaderUI = require("apps/reader/readerui")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local Screen = require("device").screen
|
||||
local Geom = require("ui/geometry")
|
||||
local DEBUG = require("dbg")
|
||||
describe("ReaderBookmark module #ok", function()
|
||||
local DocumentRegistry, ReaderUI, UIManager, Screen, Geom, DEBUG
|
||||
local sample_epub, sample_pdf
|
||||
|
||||
local sample_epub = "spec/front/unit/data/juliet.epub"
|
||||
local sample_pdf = "spec/front/unit/data/sample.pdf"
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
DocumentRegistry = require("document/documentregistry")
|
||||
ReaderUI = require("apps/reader/readerui")
|
||||
UIManager = require("ui/uimanager")
|
||||
Screen = require("device").screen
|
||||
Geom = require("ui/geometry")
|
||||
DEBUG = require("dbg")
|
||||
|
||||
sample_epub = "spec/front/unit/data/juliet.epub"
|
||||
sample_pdf = "spec/front/unit/data/sample.pdf"
|
||||
end)
|
||||
|
||||
describe("ReaderBookmark module", function()
|
||||
local function highlight_text(readerui, pos0, pos1)
|
||||
readerui.highlight:onHold(nil, { pos = pos0 })
|
||||
readerui.highlight:onHoldPan(nil, { pos = pos1 })
|
||||
@@ -49,7 +54,7 @@ describe("ReaderBookmark module", function()
|
||||
before_each(function()
|
||||
UIManager:quit()
|
||||
UIManager:show(readerui)
|
||||
readerui.rolling:gotoPage(10)
|
||||
readerui.rolling:onGotoPage(10)
|
||||
end)
|
||||
it("should does bookmark comparison properly", function()
|
||||
assert.truthy(readerui.bookmark:isBookmarkSame(
|
||||
@@ -75,7 +80,7 @@ describe("ReaderBookmark module", function()
|
||||
it("should sort bookmarks with descending page numbers", function()
|
||||
local pages = {1, 20, 5, 30, 10, 40, 15, 25, 35, 45}
|
||||
for _, page in ipairs(pages) do
|
||||
readerui.rolling:gotoPage(page)
|
||||
readerui.rolling:onGotoPage(page)
|
||||
toggler_dogear(readerui)
|
||||
end
|
||||
readerui.bookmark:onShowBookmark()
|
||||
@@ -86,7 +91,7 @@ describe("ReaderBookmark module", function()
|
||||
it("should keep descending page numbers after removing bookmarks", function()
|
||||
local pages = {1, 30, 10, 40, 20}
|
||||
for _, page in ipairs(pages) do
|
||||
readerui.rolling:gotoPage(page)
|
||||
readerui.rolling:onGotoPage(page)
|
||||
toggler_dogear(readerui)
|
||||
end
|
||||
readerui.bookmark:onShowBookmark()
|
||||
@@ -123,7 +128,7 @@ describe("ReaderBookmark module", function()
|
||||
before_each(function()
|
||||
UIManager:quit()
|
||||
UIManager:show(readerui)
|
||||
readerui.paging:gotoPage(10)
|
||||
readerui.paging:onGotoPage(10)
|
||||
end)
|
||||
it("should does bookmark comparison properly", function()
|
||||
assert.truthy(readerui.bookmark:isBookmarkSame(
|
||||
@@ -166,7 +171,7 @@ describe("ReaderBookmark module", function()
|
||||
it("should sort bookmarks with descending page numbers", function()
|
||||
local pages = {1, 20, 5, 30, 10, 40, 15, 25, 35, 45}
|
||||
for _, page in ipairs(pages) do
|
||||
readerui.paging:gotoPage(page)
|
||||
readerui.paging:onGotoPage(page)
|
||||
toggler_dogear(readerui)
|
||||
end
|
||||
readerui.bookmark:onShowBookmark()
|
||||
@@ -177,7 +182,7 @@ describe("ReaderBookmark module", function()
|
||||
it("should keep descending page numbers after removing bookmarks", function()
|
||||
local pages = {1, 30, 10, 40, 20}
|
||||
for _, page in ipairs(pages) do
|
||||
readerui.paging:gotoPage(page)
|
||||
readerui.paging:onGotoPage(page)
|
||||
toggler_dogear(readerui)
|
||||
end
|
||||
readerui.bookmark:onShowBookmark()
|
||||
|
||||
@@ -21,7 +21,7 @@ describe("Readerdictionary module", function()
|
||||
local name = "screenshots/reader_dictionary.png"
|
||||
UIManager:quit()
|
||||
UIManager:show(readerui)
|
||||
rolling:gotoPage(100)
|
||||
rolling:onGotoPage(100)
|
||||
dictionary:onLookupWord("test")
|
||||
UIManager:scheduleIn(1, function()
|
||||
UIManager:close(dictionary.dict_window)
|
||||
|
||||
@@ -58,7 +58,7 @@ describe("Readerhighlight module", function()
|
||||
before_each(function()
|
||||
UIManager:quit()
|
||||
UIManager:show(readerui)
|
||||
readerui.rolling:gotoPage(page)
|
||||
readerui.rolling:onGotoPage(page)
|
||||
end)
|
||||
after_each(function()
|
||||
readerui.highlight:clear()
|
||||
@@ -89,7 +89,7 @@ describe("Readerhighlight module", function()
|
||||
before_each(function()
|
||||
UIManager:quit()
|
||||
UIManager:show(readerui)
|
||||
readerui.paging:gotoPage(10)
|
||||
readerui.paging:onGotoPage(10)
|
||||
end)
|
||||
after_each(function()
|
||||
readerui.highlight:clear()
|
||||
@@ -111,7 +111,7 @@ describe("Readerhighlight module", function()
|
||||
before_each(function()
|
||||
UIManager:quit()
|
||||
UIManager:show(readerui)
|
||||
readerui.paging:gotoPage(28)
|
||||
readerui.paging:onGotoPage(28)
|
||||
end)
|
||||
after_each(function()
|
||||
readerui.highlight:clear()
|
||||
@@ -134,7 +134,7 @@ describe("Readerhighlight module", function()
|
||||
UIManager:quit()
|
||||
readerui.document.configurable.text_wrap = 1
|
||||
UIManager:show(readerui)
|
||||
readerui.paging:gotoPage(31)
|
||||
readerui.paging:onGotoPage(31)
|
||||
end)
|
||||
after_each(function()
|
||||
readerui.highlight:clear()
|
||||
|
||||
108
spec/unit/readerlink_spec.lua
Normal file
108
spec/unit/readerlink_spec.lua
Normal file
@@ -0,0 +1,108 @@
|
||||
describe("ReaderLink module", function()
|
||||
local DocumentRegistry, ReaderUI, UIManager, sample_epub, sample_pdf
|
||||
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
DocumentRegistry = require("document/documentregistry")
|
||||
ReaderUI = require("apps/reader/readerui")
|
||||
UIManager = require("ui/uimanager")
|
||||
sample_epub = "spec/front/unit/data/leaves.epub"
|
||||
sample_pdf = "spec/front/unit/data/Adaptively.Scaling.The.Metropolis.Algorithm.Using.Expected.Squared.Jumped.Distance.pdf"
|
||||
end)
|
||||
|
||||
it("should jump to links in epub", function()
|
||||
local readerui = ReaderUI:new{
|
||||
document = DocumentRegistry:openDocument(sample_epub),
|
||||
}
|
||||
readerui.rolling:onGotoPage(4)
|
||||
readerui.link:onTap(nil, {pos = {x = 171, y = 27}})
|
||||
assert.is.same(36, readerui.rolling.current_page)
|
||||
end)
|
||||
|
||||
it("should jump to links in pdf", function()
|
||||
UIManager:quit()
|
||||
local readerui = ReaderUI:new{
|
||||
document = DocumentRegistry:openDocument(sample_pdf),
|
||||
}
|
||||
readerui.paging:onGotoPage(1)
|
||||
readerui.link:onTap(nil, {pos = {x = 363, y = 585}})
|
||||
UIManager:run()
|
||||
assert.is.same(22, readerui.paging.current_page)
|
||||
end)
|
||||
|
||||
it("should be able to go back after link jump in epub", function()
|
||||
local readerui = ReaderUI:new{
|
||||
document = DocumentRegistry:openDocument(sample_epub),
|
||||
}
|
||||
readerui.rolling:onGotoPage(4)
|
||||
readerui.link:onTap(nil, {pos = {x = 171, y = 27}})
|
||||
assert.is.same(36, readerui.rolling.current_page)
|
||||
readerui.link:onGoBackLink()
|
||||
assert.is.same(4, readerui.rolling.current_page)
|
||||
end)
|
||||
|
||||
it("should be able to go back after link jump in pdf", function()
|
||||
UIManager:quit()
|
||||
local readerui = ReaderUI:new{
|
||||
document = DocumentRegistry:openDocument(sample_pdf),
|
||||
}
|
||||
readerui.paging:onGotoPage(1)
|
||||
readerui.link:onTap(nil, {pos = {x = 363, y = 585}})
|
||||
UIManager:run()
|
||||
assert.is.same(22, readerui.paging.current_page)
|
||||
readerui.link:onGoBackLink()
|
||||
assert.is.same(1, readerui.paging.current_page)
|
||||
end)
|
||||
|
||||
it("should be able to go back after link jump in pdf in scroll mode", function()
|
||||
UIManager:quit()
|
||||
local expected_page_states = {
|
||||
{
|
||||
gamma = 1,
|
||||
offset = {x = 17, y = 0},
|
||||
page = 3,
|
||||
page_area = {
|
||||
x = 0, y = 0,
|
||||
h = 800, w = 566,
|
||||
},
|
||||
rotation = 0,
|
||||
visible_area = {
|
||||
x = 0, y = 694,
|
||||
h = 106, w = 566,
|
||||
},
|
||||
zoom = 0.9501187648456056456,
|
||||
},
|
||||
{
|
||||
gamma = 1,
|
||||
offset = {x = 17, y = 0},
|
||||
page = 4,
|
||||
page_area = {
|
||||
h = 800, w = 566,
|
||||
x = 0, y = 0,
|
||||
},
|
||||
rotation = 0,
|
||||
visible_area = {
|
||||
h = 686, w = 566,
|
||||
x = 0, y = 0,
|
||||
},
|
||||
zoom = 0.9501187648456056456,
|
||||
},
|
||||
}
|
||||
local readerui = ReaderUI:new{
|
||||
document = DocumentRegistry:openDocument(sample_pdf),
|
||||
}
|
||||
readerui.view:onSetScrollMode(true)
|
||||
assert.is.same(true, readerui.view.page_scroll)
|
||||
readerui.paging:onTapForward()
|
||||
readerui.paging:onTapForward()
|
||||
readerui.paging:onTapForward()
|
||||
assert.is.same(4, readerui.paging.current_page)
|
||||
assert.are.same(expected_page_states, readerui.view.page_states)
|
||||
readerui.link:onTap(nil, {pos = {x = 181, y = 366}})
|
||||
UIManager:run()
|
||||
assert.is.same(22, readerui.paging.current_page)
|
||||
readerui.link:onGoBackLink()
|
||||
assert.is.same(4, readerui.paging.current_page)
|
||||
assert.are.same(expected_page_states, readerui.view.page_states)
|
||||
end)
|
||||
end)
|
||||
@@ -15,7 +15,7 @@ describe("Readerpaging module", function()
|
||||
|
||||
it("should emit EndOfBook event at the end", function()
|
||||
readerui.zooming:setZoomMode("pageheight")
|
||||
paging:gotoPage(readerui.document:getPageCount())
|
||||
paging:onGotoPage(readerui.document:getPageCount())
|
||||
local called = false
|
||||
readerui.onEndOfBook = function()
|
||||
called = true
|
||||
@@ -35,9 +35,9 @@ describe("Readerpaging module", function()
|
||||
end)
|
||||
|
||||
it("should emit EndOfBook event at the end", function()
|
||||
paging:gotoPage(readerui.document:getPageCount())
|
||||
paging:onGotoPage(readerui.document:getPageCount())
|
||||
readerui.zooming:setZoomMode("pageheight")
|
||||
readerui.view:onToggleScrollMode(true)
|
||||
readerui.view:onSetScrollMode(true)
|
||||
local called = false
|
||||
readerui.onEndOfBook = function()
|
||||
called = true
|
||||
|
||||
@@ -16,13 +16,13 @@ describe("Readerrolling module", function()
|
||||
end)
|
||||
it("should goto certain page", function()
|
||||
for i = 1, 10, 5 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
assert.are.same(i, rolling.current_page)
|
||||
end
|
||||
end)
|
||||
it("should goto relative page", function()
|
||||
for i = 20, 40, 5 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
rolling:onGotoViewRel(1)
|
||||
assert.are.same(i + 1, rolling.current_page)
|
||||
rolling:onGotoViewRel(-1)
|
||||
@@ -32,7 +32,7 @@ describe("Readerrolling module", function()
|
||||
it("should goto next chapter", function()
|
||||
local toc = readerui.toc
|
||||
for i = 30, 50, 5 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
rolling:onDoubleTapForward()
|
||||
assert.are.same(toc:getNextChapter(i, 0), rolling.current_page)
|
||||
end
|
||||
@@ -40,13 +40,13 @@ describe("Readerrolling module", function()
|
||||
it("should goto previous chapter", function()
|
||||
local toc = readerui.toc
|
||||
for i = 60, 80, 5 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
rolling:onDoubleTapBackward()
|
||||
assert.are.same(toc:getPreviousChapter(i, 0), rolling.current_page)
|
||||
end
|
||||
end)
|
||||
it("should emit EndOfBook event at the end", function()
|
||||
rolling:gotoPage(readerui.document:getPageCount())
|
||||
rolling:onGotoPage(readerui.document:getPageCount())
|
||||
local called = false
|
||||
readerui.onEndOfBook = function()
|
||||
called = true
|
||||
@@ -63,13 +63,13 @@ describe("Readerrolling module", function()
|
||||
end)
|
||||
it("should goto certain page", function()
|
||||
for i = 1, 10, 5 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
assert.are.same(i, rolling.current_page)
|
||||
end
|
||||
end)
|
||||
it("should goto relative page", function()
|
||||
for i = 20, 40, 5 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
rolling:onGotoViewRel(1)
|
||||
assert.are.same(i + 1, rolling.current_page)
|
||||
rolling:onGotoViewRel(-1)
|
||||
@@ -79,7 +79,7 @@ describe("Readerrolling module", function()
|
||||
it("should goto next chapter", function()
|
||||
local toc = readerui.toc
|
||||
for i = 30, 50, 5 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
rolling:onDoubleTapForward()
|
||||
assert.are.same(toc:getNextChapter(i, 0), rolling.current_page)
|
||||
end
|
||||
@@ -87,13 +87,13 @@ describe("Readerrolling module", function()
|
||||
it("should goto previous chapter", function()
|
||||
local toc = readerui.toc
|
||||
for i = 60, 80, 5 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
rolling:onDoubleTapBackward()
|
||||
assert.are.same(toc:getPreviousChapter(i, 0), rolling.current_page)
|
||||
end
|
||||
end)
|
||||
it("should emit EndOfBook event at the end", function()
|
||||
rolling:gotoPage(readerui.document:getPageCount())
|
||||
rolling:onGotoPage(readerui.document:getPageCount())
|
||||
local called = false
|
||||
readerui.onEndOfBook = function()
|
||||
called = true
|
||||
|
||||
@@ -18,10 +18,10 @@ describe("Readersearch module", function()
|
||||
rolling = readerui.rolling
|
||||
end)
|
||||
it("should search backward", function()
|
||||
rolling:gotoPage(10)
|
||||
rolling:onGotoPage(10)
|
||||
assert.truthy(search:searchFromCurrent("Verona", 1))
|
||||
for i = 1, 100, 10 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
local words = search:searchFromCurrent("Verona", 1)
|
||||
if words then
|
||||
for _, word in ipairs(words) do
|
||||
@@ -33,10 +33,10 @@ describe("Readersearch module", function()
|
||||
end
|
||||
end)
|
||||
it("should search forward", function()
|
||||
rolling:gotoPage(10)
|
||||
rolling:onGotoPage(10)
|
||||
assert.truthy(search:searchFromCurrent("Verona", 0))
|
||||
for i = 1, 100, 10 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
local words = search:searchFromCurrent("Verona", 0)
|
||||
if words then
|
||||
for _, word in ipairs(words) do
|
||||
@@ -49,35 +49,35 @@ describe("Readersearch module", function()
|
||||
end)
|
||||
it("should find the first occurrence", function()
|
||||
for i = 10, 100, 10 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
local words = search:searchFromStart("Verona")
|
||||
assert.truthy(words)
|
||||
local pageno = doc:getPageFromXPointer(words[1].start)
|
||||
assert.are.equal(7, pageno)
|
||||
end
|
||||
for i = 1, 5, 1 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
local words = search:searchFromStart("Verona")
|
||||
assert(words == nil)
|
||||
end
|
||||
end)
|
||||
it("should find the last occurrence", function()
|
||||
for i = 100, 180, 10 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
local words = search:searchFromEnd("Verona")
|
||||
assert.truthy(words)
|
||||
local pageno = doc:getPageFromXPointer(words[1].start)
|
||||
assert.are.equal(199, pageno)
|
||||
end
|
||||
for i = 230, 235, 1 do
|
||||
rolling:gotoPage(i)
|
||||
rolling:onGotoPage(i)
|
||||
local words = search:searchFromEnd("Verona")
|
||||
assert(words == nil)
|
||||
end
|
||||
end)
|
||||
it("should find all occurrences", function()
|
||||
local count = 0
|
||||
rolling:gotoPage(1)
|
||||
rolling:onGotoPage(1)
|
||||
local words = search:searchFromCurrent("Verona", 0)
|
||||
while words do
|
||||
count = count + #words
|
||||
@@ -129,10 +129,10 @@ describe("Readersearch module", function()
|
||||
assert.are.equal(0, #doc.koptinterface:findAllMatches(doc, "e", true, 1))
|
||||
end)
|
||||
it("should search backward", function()
|
||||
paging:gotoPage(20)
|
||||
paging:onGotoPage(20)
|
||||
assert.truthy(search:searchFromCurrent("test", 1))
|
||||
for i = 1, 40, 10 do
|
||||
paging:gotoPage(i)
|
||||
paging:onGotoPage(i)
|
||||
local words = search:searchFromCurrent("test", 1)
|
||||
if words then
|
||||
DEBUG("search backward: found at page", words.page)
|
||||
@@ -141,10 +141,10 @@ describe("Readersearch module", function()
|
||||
end
|
||||
end)
|
||||
it("should search forward", function()
|
||||
paging:gotoPage(20)
|
||||
paging:onGotoPage(20)
|
||||
assert.truthy(search:searchFromCurrent("test", 0))
|
||||
for i = 1, 40, 10 do
|
||||
paging:gotoPage(i)
|
||||
paging:onGotoPage(i)
|
||||
local words = search:searchFromCurrent("test", 0)
|
||||
if words then
|
||||
DEBUG("search forward: found at page", words.page)
|
||||
@@ -154,38 +154,38 @@ describe("Readersearch module", function()
|
||||
end)
|
||||
it("should find the first occurrence", function()
|
||||
for i = 20, 40, 10 do
|
||||
paging:gotoPage(i)
|
||||
paging:onGotoPage(i)
|
||||
local words = search:searchFromStart("test")
|
||||
assert.truthy(words)
|
||||
assert.are.equal(10, words.page)
|
||||
end
|
||||
for i = 1, 10, 2 do
|
||||
paging:gotoPage(i)
|
||||
paging:onGotoPage(i)
|
||||
local words = search:searchFromStart("test")
|
||||
assert(words == nil)
|
||||
end
|
||||
end)
|
||||
it("should find the last occurrence", function()
|
||||
for i = 10, 30, 10 do
|
||||
paging:gotoPage(i)
|
||||
paging:onGotoPage(i)
|
||||
local words = search:searchFromEnd("test")
|
||||
assert.truthy(words)
|
||||
assert.are.equal(32, words.page)
|
||||
end
|
||||
for i = 40, 50, 2 do
|
||||
paging:gotoPage(i)
|
||||
paging:onGotoPage(i)
|
||||
local words = search:searchFromEnd("test")
|
||||
assert(words == nil)
|
||||
end
|
||||
end)
|
||||
it("should find all occurrences", function()
|
||||
local count = 0
|
||||
paging:gotoPage(1)
|
||||
paging:onGotoPage(1)
|
||||
local words = search:searchFromCurrent("test", 0)
|
||||
while words do
|
||||
count = count + #words
|
||||
--DEBUG("found words", #words, words.page)
|
||||
paging:gotoPage(words.page)
|
||||
paging:onGotoPage(words.page)
|
||||
words = search:searchNext("test", 0)
|
||||
end
|
||||
assert.are.equal(11, count)
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
require("commonrequire")
|
||||
local DocumentRegistry = require("document/documentregistry")
|
||||
local Blitbuffer = require("ffi/blitbuffer")
|
||||
local ReaderUI = require("apps/reader/readerui")
|
||||
local UIManager = require("ui/uimanager")
|
||||
|
||||
describe("Readerview module", function()
|
||||
local DocumentRegistry, Blitbuffer, ReaderUI, UIManager
|
||||
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
DocumentRegistry = require("document/documentregistry")
|
||||
Blitbuffer = require("ffi/blitbuffer")
|
||||
ReaderUI = require("apps/reader/readerui")
|
||||
UIManager = require("ui/uimanager")
|
||||
end)
|
||||
|
||||
it("should stop hinting on document close event", function()
|
||||
local sample_epub = "spec/front/unit/data/leaves.epub"
|
||||
local readerui = ReaderUI:new{
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user