readerrolling(fix): detect end of book properly

pull/1983/head
Qingping Hou 8 years ago
parent 26ecc6035a
commit 572412b1f6

@ -357,11 +357,6 @@ end
function ReaderRolling:onGotoViewRel(diff)
DEBUG("goto relative screen:", diff, ", in mode: ", self.view.view_mode)
local prev_xp
-- save xpointer to check whether we reach the end of the book
if diff > 0 then
prev_xp = self.xpointer
end
if self.view.view_mode == "scroll" then
local pan_diff = diff * self.ui.dimen.h
if self.show_overlap_enable then
@ -371,15 +366,20 @@ function ReaderRolling:onGotoViewRel(diff)
pan_diff = pan_diff + self.overlap
end
end
local old_pos = self.current_pos
self:_gotoPos(self.current_pos + pan_diff)
if diff > 0 and old_pos == self.current_pos then
self.ui:handleEvent(Event:new("EndOfBook"))
end
elseif self.view.view_mode == "page" then
local page_count = self.ui.document:getVisiblePageCount()
local old_page = self.current_page
self:_gotoPage(self.current_page + diff*page_count)
if diff > 0 and old_page == self.current_page then
self.ui:handleEvent(Event:new("EndOfBook"))
end
end
self.xpointer = self.ui.document:getXPointer()
if self.xpointer == prev_xp then
self.ui:handleEvent(Event:new("EndOfBook"))
end
return true
end

@ -51,18 +51,57 @@ describe("Readerrolling module", function()
assert.are.same(toc:getPreviousChapter(i, 0), rolling.current_page)
end
end)
it("should emit EndOfBook event at the end", function()
rolling:onGotoPage(readerui.document:getPageCount())
it("should emit EndOfBook event at the end of sample epub", function()
local called = false
readerui.onEndOfBook = function()
called = true
end
-- check beginning of the book
rolling:onGotoPage(1)
assert.is.falsy(called)
rolling:onGotoViewRel(-1)
rolling:onGotoViewRel(-1)
assert.is.falsy(called)
-- check end of the book
rolling:onGotoPage(readerui.document:getPageCount())
assert.is.falsy(called)
rolling:onGotoViewRel(1)
assert.is.truthy(called)
rolling:onGotoViewRel(1)
assert.is.truthy(called)
readerui.onEndOfBook = nil
end)
it("should emit EndOfBook event at the end sample txt", function()
local sample_txt = "spec/front/unit/data/sample.txt"
local txt_readerui = ReaderUI:new{
document = DocumentRegistry:openDocument(sample_txt),
}
local called = false
txt_readerui.onEndOfBook = function()
called = true
end
local txt_rolling = txt_readerui.rolling
-- check beginning of the book
txt_rolling:onGotoPage(1)
assert.is.falsy(called)
txt_rolling:onGotoViewRel(-1)
txt_rolling:onGotoViewRel(-1)
assert.is.falsy(called)
-- not at the end of the book
txt_rolling:onGotoPage(3)
assert.is.falsy(called)
txt_rolling:onGotoViewRel(1)
assert.is.falsy(called)
-- at the end of the book
txt_rolling:onGotoPage(txt_readerui.document:getPageCount())
assert.is.falsy(called)
txt_rolling:onGotoViewRel(1)
assert.is.truthy(called)
readerui.onEndOfBook = nil
end)
end)
describe("test in landscape screen mode", function()
it("should go to landscape screen mode", function()
readerui:handleEvent(Event:new("ChangeScreenMode", "landscape"))
@ -110,6 +149,7 @@ describe("Readerrolling module", function()
readerui.onEndOfBook = nil
end)
end)
describe("switching screen mode should not change current page number", function()
it("for portrait-landscape-portrait switching", function()
for i = 80, 100, 10 do

Loading…
Cancel
Save