2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/frontend/ui/reader/readerrolling.lua

349 lines
9.7 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local InputContainer = require("ui/widget/container/inputcontainer")
local Screen = require("ui/screen")
local Device = require("ui/device")
2013-10-18 20:38:07 +00:00
local Geom = require("ui/geometry")
local Input = require("ui/input")
local Event = require("ui/event")
2013-10-18 20:38:07 +00:00
local GestureRange = require("ui/gesturerange")
local ReaderPanning = require("ui/reader/readerpanning")
local UIManager = require("ui/uimanager")
2013-10-22 18:51:29 +00:00
local DEBUG = require("dbg")
2013-10-18 20:38:07 +00:00
local _ = require("gettext")
2013-10-18 20:38:07 +00:00
local ReaderRolling = InputContainer:new{
2012-06-12 09:42:02 +00:00
old_doc_height = nil,
old_page = nil,
2012-06-05 07:23:36 +00:00
current_pos = 0,
-- only used for page view mode
current_page= nil,
2012-06-12 09:42:02 +00:00
doc_height = nil,
panning_steps = ReaderPanning.panning_steps,
2012-12-08 06:05:10 +00:00
show_overlap_enable = true,
overlap = 20,
2012-06-05 07:23:36 +00:00
}
function ReaderRolling:init()
2013-02-02 06:36:29 +00:00
if Device:hasKeyboard() then
2012-12-04 09:05:40 +00:00
self.key_events = {
GotoNextView = {
{ Input.group.PgFwd },
2013-04-08 07:21:59 +00:00
doc = _("go to next view"),
2012-12-04 09:05:40 +00:00
event = "GotoViewRel", args = 1
},
GotoPrevView = {
{ Input.group.PgBack },
2013-04-08 07:21:59 +00:00
doc = _("go to previous view"),
2012-12-04 09:05:40 +00:00
event = "GotoViewRel", args = -1
},
MoveUp = {
{ "Up" },
2013-04-08 07:21:59 +00:00
doc = _("move view up"),
2012-12-04 09:05:40 +00:00
event = "Panning", args = {0, -1}
},
MoveDown = {
{ "Down" },
2013-04-08 07:21:59 +00:00
doc = _("move view down"),
2012-12-04 09:05:40 +00:00
event = "Panning", args = {0, 1}
},
2013-04-08 07:21:59 +00:00
GotoFirst = {
{"1"}, doc = _("go to start"), event = "GotoPercent", args = 0},
Goto11 = {
{"2"}, doc = _("go to 11%"), event = "GotoPercent", args = 11},
Goto22 = {
{"3"}, doc = _("go to 22%"), event = "GotoPercent", args = 22},
Goto33 = {
{"4"}, doc = _("go to 33%"), event = "GotoPercent", args = 33},
Goto44 = {
{"5"}, doc = _("go to 44%"), event = "GotoPercent", args = 44},
Goto55 = {
{"6"}, doc = _("go to 55%"), event = "GotoPercent", args = 55},
Goto66 = {
{"7"}, doc = _("go to 66%"), event = "GotoPercent", args = 66},
Goto77 = {
{"8"}, doc = _("go to 77%"), event = "GotoPercent", args = 77},
Goto88 = {
{"9"}, doc = _("go to 88%"), event = "GotoPercent", args = 88},
GotoLast = {
{"0"}, doc = _("go to end"), event = "GotoPercent", args = 100},
2012-12-04 09:05:40 +00:00
}
end
table.insert(self.ui.postInitCallback, function()
self.doc_height = self.ui.document.info.doc_height
self.old_doc_height = self.doc_height
self.old_page = self.ui.document.info.number_of_pages
end)
2012-06-05 07:23:36 +00:00
end
2013-02-02 06:36:29 +00:00
-- This method will be called in onSetDimensions handler
function ReaderRolling:initGesListener()
self.ges_events = {
TapForward = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = Screen:getWidth()*DTAP_ZONE_FORWARD.x,
y = Screen:getHeight()*DTAP_ZONE_FORWARD.y,
w = Screen:getWidth()*DTAP_ZONE_FORWARD.w,
h = Screen:getHeight()*DTAP_ZONE_FORWARD.h,
2013-02-02 06:36:29 +00:00
}
}
},
TapBackward = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = Screen:getWidth()*DTAP_ZONE_BACKWARD.x,
y = Screen:getHeight()*DTAP_ZONE_BACKWARD.y,
w = Screen:getWidth()*DTAP_ZONE_BACKWARD.w,
h = Screen:getHeight()*DTAP_ZONE_BACKWARD.h,
2013-02-02 06:36:29 +00:00
}
}
2013-04-14 10:19:42 +00:00
},
Swipe = {
GestureRange:new{
ges = "swipe",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
}
},
Pan = {
GestureRange:new{
ges = "pan",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
},
rate = 4.0,
}
},
2013-02-02 06:36:29 +00:00
}
end
2012-12-04 09:05:40 +00:00
function ReaderRolling:onReadSettings(config)
2012-12-08 06:05:10 +00:00
local soe = config:readSetting("show_overlap_enable")
if not soe then
self.show_overlap_enable = soe
end
local last_xp = config:readSetting("last_xpointer")
if last_xp then
table.insert(self.ui.postInitCallback, function()
self:gotoXPointer(last_xp)
-- we have to do a real jump in self.ui.document._document to
-- update status information in CREngine.
self.ui.document:gotoXPointer(last_xp)
end)
end
-- we read last_percent just for backward compatibility
if not last_xp then
local last_per = config:readSetting("last_percent")
if last_per then
table.insert(self.ui.postInitCallback, function()
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)
end)
end
end
if self.view.view_mode == "page" then
self.ui:handleEvent(Event:new("PageUpdate", self.ui.document:getCurrentPage()))
end
2012-06-05 07:23:36 +00:00
end
2013-12-27 15:18:16 +00:00
function ReaderRolling:onSaveSettings()
-- remove last_percent config since its deprecated
self.ui.doc_settings:saveSetting("last_percent", nil)
self.ui.doc_settings:saveSetting("last_xpointer", self.ui.document:getXPointer())
self.ui.doc_settings:saveSetting("percent_finished", self:getLastPercent())
end
function ReaderRolling:getLastPercent()
if self.view.view_mode == "page" then
return self.current_page / self.old_page
else
-- FIXME: the calculated percent is not accurate in "scroll" mode.
return self.ui.document:getPosFromXPointer(
self.ui.document:getXPointer()) / self.doc_height
end
2012-06-12 09:42:02 +00:00
end
2012-12-04 09:05:40 +00:00
function ReaderRolling:onTapForward()
self:onGotoViewRel(1)
return true
end
2012-12-04 09:05:40 +00:00
function ReaderRolling:onTapBackward()
self:onGotoViewRel(-1)
return true
end
2013-04-14 10:19:42 +00:00
function ReaderRolling:onSwipe(arg, ges)
if ges.direction == "west" or ges.direction == "north" then
self:onGotoViewRel(1)
elseif ges.direction == "east" or ges.direction == "south" then
self:onGotoViewRel(-1)
end
return true
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)
elseif ges.direction == "south" then
self:gotoPos(self.current_pos - ges.distance)
end
end
return true
end
2012-12-04 09:05:40 +00:00
function ReaderRolling:onPosUpdate(new_pos)
self.current_pos = new_pos
2012-06-11 16:34:30 +00:00
end
function ReaderRolling:onPageUpdate(new_page)
self.current_page = new_page
end
2012-06-05 07:23:36 +00:00
function ReaderRolling:onGotoPercent(percent)
DEBUG("goto document offset in percent:", percent)
2012-06-11 16:34:30 +00:00
self:gotoPercent(percent)
2012-06-05 07:23:36 +00:00
return true
end
function ReaderRolling:onGotoViewRel(diff)
DEBUG("goto relative screen:", diff, ", in mode: ", self.view.view_mode)
if self.view.view_mode == "scroll" then
local pan_diff = diff * self.ui.dimen.h
if self.show_overlap_enable then
if pan_diff > self.overlap then
pan_diff = pan_diff - self.overlap
elseif pan_diff < -self.overlap then
pan_diff = pan_diff + self.overlap
end
2012-12-08 06:05:10 +00:00
end
self:gotoPos(self.current_pos + pan_diff)
elseif self.view.view_mode == "page" then
self:gotoPage(self.current_page + diff)
2012-12-08 06:05:10 +00:00
end
2012-06-05 07:23:36 +00:00
return true
end
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)
return true
end
2012-06-05 07:23:36 +00:00
function ReaderRolling:onZoom()
2012-06-12 09:42:02 +00:00
--@TODO re-read doc_height info after font or lineheight changes 05.06 2012 (houqp)
self:updatePos()
2012-06-05 07:23:36 +00:00
end
--[[
remember to signal this event when the document has been zoomed,
font has been changed, or line height has been changed.
--]]
2012-12-04 09:05:40 +00:00
function ReaderRolling:onUpdatePos()
UIManager:scheduleIn(0.1, function () self:updatePos() end)
return true
end
function ReaderRolling:updatePos()
2012-12-04 09:05:40 +00:00
-- reread document height
self.ui.document:_readMetadata()
-- update self.current_pos if the height of document has been changed.
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.ui.document:getXPointer())
self.old_doc_height = new_height
self.old_page = new_page
self.ui:handleEvent(Event:new("UpdateToc"))
2012-12-04 09:05:40 +00:00
end
2014-01-03 15:18:10 +00:00
UIManager.repaint_all = true
2012-12-04 09:05:40 +00:00
end
function ReaderRolling:onChangeViewMode()
self.ui.document:_readMetadata()
self.old_doc_height = self.ui.document.info.doc_height
self.old_page = self.ui.document.info.number_of_pages
self.ui:handleEvent(Event:new("UpdateToc"))
self:gotoXPointer(self.ui.document:getXPointer())
if self.view.view_mode == "scroll" then
self.current_pos = self.ui.document:getCurrentPos()
else
self.current_page = self.ui.document:getCurrentPage()
end
return true
end
function ReaderRolling:onRedrawCurrentView()
if self.view.view_mode == "page" then
self.ui:handleEvent(Event:new("PageUpdate", self.current_page))
else
self.ui:handleEvent(Event:new("PosUpdate", self.current_pos))
end
return true
end
2013-02-02 06:36:29 +00:00
function ReaderRolling:onSetDimensions()
-- update listening according to new screen dimen
if Device:isTouchDevice() then
self:initGesListener()
end
end
--[[
PosUpdate event is used to signal other widgets that pos has been changed.
--]]
2012-12-04 09:05:40 +00:00
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
2012-12-08 06:05:10 +00:00
-- adjust dim_area according to new_pos
if self.view.view_mode ~= "page" and self.show_overlap_enable then
2012-12-08 06:05:10 +00:00
local panned_step = new_pos - self.current_pos
self.view.dim_area.x = 0
self.view.dim_area.h = self.ui.dimen.h - math.abs(panned_step)
self.view.dim_area.w = self.ui.dimen.w
if panned_step < 0 then
self.view.dim_area.y = self.ui.dimen.h - self.view.dim_area.h
elseif panned_step > 0 then
self.view.dim_area.y = 0
end
end
2012-12-04 09:05:40 +00:00
self.ui:handleEvent(Event:new("PosUpdate", new_pos))
end
function ReaderRolling:gotoPage(new_page)
self.ui.document:gotoPage(new_page)
self.ui:handleEvent(Event:new("PageUpdate", new_page))
end
function ReaderRolling:gotoXPointer(xpointer)
if self.view.view_mode == "page" then
self:gotoPage(self.ui.document:getPageFromXPointer(xpointer))
else
self:gotoPos(self.ui.document:getPosFromXPointer(xpointer))
end
end
2012-12-04 09:05:40 +00:00
function ReaderRolling:gotoPercent(new_percent)
self:gotoPos(new_percent * self.doc_height / 10000)
end
2012-12-04 09:05:40 +00:00
2013-10-17 15:53:29 +00:00
function ReaderRolling:onGotoPage(number)
self:gotoPage(number)
return true
end
2013-10-18 20:38:07 +00:00
return ReaderRolling