mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
readerview: add get/set methods for view context
This commit is contained in:
@@ -557,6 +557,36 @@ function ReaderView:SetZoomCenter(x, y)
|
||||
end
|
||||
end
|
||||
|
||||
function ReaderView:getViewContext()
|
||||
if self.page_scroll then
|
||||
return self.page_states
|
||||
else
|
||||
return {
|
||||
{
|
||||
page = self.state.page,
|
||||
pos = self.state.pos,
|
||||
zoom = self.state.zoom,
|
||||
rotation = self.state.rotation,
|
||||
gamma = self.state.gamma,
|
||||
offset = self.state.offset:copy(),
|
||||
bbox = self.state.bbox,
|
||||
},
|
||||
self.visible_area:copy(),
|
||||
self.page_area:copy(),
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
function ReaderView:restoreViewContext(ctx)
|
||||
if self.page_scroll then
|
||||
self.page_states = ctx
|
||||
else
|
||||
self.state = ctx[1]
|
||||
self.visible_area = ctx[2]
|
||||
self.page_area = ctx[3]
|
||||
end
|
||||
end
|
||||
|
||||
function ReaderView:onSetScreenMode(new_mode, rotation)
|
||||
if new_mode == "landscape" or new_mode == "portrait" then
|
||||
self.screen_mode = new_mode
|
||||
|
||||
@@ -39,4 +39,50 @@ describe("Readerview module", function()
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
it("should return and restore view context", function()
|
||||
local sample_pdf = "spec/front/unit/data/2col.pdf"
|
||||
local readerui = ReaderUI:new{
|
||||
document = DocumentRegistry:openDocument(sample_pdf),
|
||||
}
|
||||
local view = readerui.view
|
||||
local ctx = view:getViewContext()
|
||||
local zoom = ctx[1].zoom
|
||||
ctx[1].zoom = nil
|
||||
local saved_ctx = {
|
||||
{
|
||||
page = 1,
|
||||
pos = 0,
|
||||
gamma = 1,
|
||||
offset = {
|
||||
x = 17, y = 0,
|
||||
h = 0, w = 0,
|
||||
},
|
||||
rotation = 0,
|
||||
},
|
||||
-- visible_area
|
||||
{
|
||||
x = 0, y = 0,
|
||||
h = 800, w = 566,
|
||||
},
|
||||
-- page_area
|
||||
{
|
||||
x = 0, y = 0,
|
||||
h = 800, w = 566,
|
||||
},
|
||||
}
|
||||
assert.are.same(ctx, saved_ctx)
|
||||
assertAlmostEquals(zoom, 0.95011876484561, 0.0001)
|
||||
|
||||
assert.is.same(view.state.page, 1)
|
||||
assert.is.same(view.visible_area.x, 0)
|
||||
assert.is.same(view.visible_area.y, 0)
|
||||
saved_ctx[1].page = 2
|
||||
saved_ctx[1].zoom = zoom
|
||||
saved_ctx[2].y = 10
|
||||
view:restoreViewContext(saved_ctx)
|
||||
assert.is.same(view.state.page, 2)
|
||||
assert.is.same(view.visible_area.x, 0)
|
||||
assert.is.same(view.visible_area.y, 10)
|
||||
end)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user