2014-01-15 15:36:47 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local GestureRange = require("ui/gesturerange")
|
2014-05-02 08:50:43 +00:00
|
|
|
local LinkBox = require("ui/widget/linkbox")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2014-01-15 15:36:47 +00:00
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local Screen = require("ui/screen")
|
|
|
|
local Device = require("ui/device")
|
|
|
|
local Event = require("ui/event")
|
|
|
|
local DEBUG = require("dbg")
|
2014-08-17 12:29:38 +00:00
|
|
|
local _ = require("gettext")
|
2014-01-15 15:36:47 +00:00
|
|
|
|
2014-01-18 07:04:24 +00:00
|
|
|
local ReaderLink = InputContainer:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
link_states = {}
|
2014-01-18 07:04:24 +00:00
|
|
|
}
|
2014-01-15 15:36:47 +00:00
|
|
|
|
|
|
|
function ReaderLink:init()
|
2014-03-13 13:52:43 +00:00
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self:initGesListener()
|
|
|
|
end
|
2014-10-09 09:41:23 +00:00
|
|
|
self.ui:registerPostInitCallback(function()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end)
|
2014-01-15 15:36:47 +00:00
|
|
|
end
|
|
|
|
|
2014-01-22 11:59:53 +00:00
|
|
|
function ReaderLink:onReadSettings(config)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- called when loading new document
|
|
|
|
self.link_states = {}
|
2014-01-22 11:59:53 +00:00
|
|
|
end
|
|
|
|
|
2014-01-15 15:36:47 +00:00
|
|
|
function ReaderLink:initGesListener()
|
2014-03-13 13:52:43 +00:00
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
|
|
|
Tap = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Swipe = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "swipe",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2014-01-15 15:36:47 +00:00
|
|
|
end
|
|
|
|
|
2014-08-17 12:29:38 +00:00
|
|
|
function ReaderLink:addToMainMenu(tab_item_table)
|
|
|
|
-- insert table to main reader menu
|
|
|
|
table.insert(tab_item_table.navi, {
|
|
|
|
text = _("Follow links"),
|
|
|
|
checked_func = function()
|
|
|
|
return G_reader_settings:readSetting("follow_links") ~= false
|
|
|
|
end,
|
|
|
|
callback = function()
|
|
|
|
local follow_links = G_reader_settings:readSetting("follow_links")
|
|
|
|
if follow_links == nil then follow_links = true end
|
|
|
|
G_reader_settings:saveSetting("follow_links", not follow_links)
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2014-01-15 15:36:47 +00:00
|
|
|
function ReaderLink:onSetDimensions(dimen)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- update listening according to new screen dimen
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self:initGesListener()
|
|
|
|
end
|
2014-01-15 15:36:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderLink:onTap(arg, ges)
|
2014-08-17 12:29:38 +00:00
|
|
|
if G_reader_settings:readSetting("follow_links") == false then return end
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
local pos = self.view:screenToPageTransform(ges.pos)
|
|
|
|
if pos then
|
2014-05-11 12:19:00 +00:00
|
|
|
-- link box in native page
|
2014-05-02 08:50:43 +00:00
|
|
|
local link, lbox = self.ui.document:getLinkFromPosition(pos.page, pos)
|
|
|
|
if link and lbox then
|
|
|
|
-- screen box that holds the link
|
2014-05-11 12:19:00 +00:00
|
|
|
local sbox = self.view:pageToScreenTransform(pos.page,
|
|
|
|
self.ui.document:nativeToPageRectTransform(pos.page, lbox))
|
2014-05-02 08:50:43 +00:00
|
|
|
if sbox then
|
|
|
|
UIManager:show(LinkBox:new{
|
|
|
|
box = sbox,
|
|
|
|
timeout = 0.5,
|
|
|
|
callback = function() self:onGotoLink(link) end
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local link = self.ui.document:getLinkFromPosition(ges.pos)
|
|
|
|
if link ~= "" then
|
|
|
|
return self:onGotoLink(link)
|
|
|
|
end
|
|
|
|
end
|
2014-01-18 07:04:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderLink:onGotoLink(link)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
table.insert(self.link_states, self.view.state.page)
|
2014-10-20 14:55:21 +00:00
|
|
|
self.ui:handleEvent(Event:new("GotoPage", link.page + 1))
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
|
|
|
table.insert(self.link_states, self.ui.document:getXPointer())
|
2014-10-20 14:55:21 +00:00
|
|
|
self.ui:handleEvent(Event:new("GotoXPointer", link))
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
return true
|
2014-01-18 07:04:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderLink:onSwipe(arg, ges)
|
2014-03-13 13:52:43 +00:00
|
|
|
if ges.direction == "east" then
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
local last_page = table.remove(self.link_states)
|
|
|
|
if last_page then
|
2014-10-20 14:55:21 +00:00
|
|
|
self.ui:handleEvent(Event:new("GotoPage", last_page))
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local last_xp = table.remove(self.link_states)
|
|
|
|
if last_xp then
|
2014-10-20 14:55:21 +00:00
|
|
|
self.ui:handleEvent(Event:new("GotoXPointer", last_xp))
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-15 15:36:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ReaderLink
|