|
|
|
@ -1,13 +1,17 @@
|
|
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
|
--[[
|
|
|
|
|
ReaderLink is an abstraction for document-specific link interfaces.
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
|
|
local Device = require("device")
|
|
|
|
|
local Event = require("ui/event")
|
|
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
|
local GestureRange = require("ui/gesturerange")
|
|
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
|
local LinkBox = require("ui/widget/linkbox")
|
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
|
local Screen = require("device").screen
|
|
|
|
|
local Device = require("device")
|
|
|
|
|
local logger = require("logger")
|
|
|
|
|
local Event = require("ui/event")
|
|
|
|
|
local _ = require("gettext")
|
|
|
|
|
local Screen = Device.screen
|
|
|
|
|
local T = require("ffi/util").template
|
|
|
|
|
|
|
|
|
|
local ReaderLink = InputContainer:new{
|
|
|
|
@ -55,8 +59,8 @@ function ReaderLink:initGesListener()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function isFollowLinksOn()
|
|
|
|
|
return G_reader_settings:readSetting("follow_links") ~= false
|
|
|
|
|
local function isTapToFollowLinksOn()
|
|
|
|
|
return not G_reader_settings:isFalse("tap_to_follow_links")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function isSwipeToGoBackEnabled()
|
|
|
|
@ -70,22 +74,24 @@ end
|
|
|
|
|
function ReaderLink:addToMainMenu(menu_items)
|
|
|
|
|
-- insert table to main reader menu
|
|
|
|
|
menu_items.follow_links = {
|
|
|
|
|
text = _("Follow links"),
|
|
|
|
|
text = _("Links"),
|
|
|
|
|
sub_item_table = {
|
|
|
|
|
{
|
|
|
|
|
text_func = function()
|
|
|
|
|
return isFollowLinksOn() and _("Disable") or _("Enable")
|
|
|
|
|
end,
|
|
|
|
|
callback = function()
|
|
|
|
|
G_reader_settings:saveSetting("follow_links",
|
|
|
|
|
not isFollowLinksOn())
|
|
|
|
|
end
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
text = _("Go back"),
|
|
|
|
|
text = _("Go back to previous location"),
|
|
|
|
|
enabled_func = function() return #self.location_stack > 0 end,
|
|
|
|
|
callback = function() self:onGoBackLink() end,
|
|
|
|
|
separator = true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
text = _("Tap to follow links"),
|
|
|
|
|
checked_func = isTapToFollowLinksOn,
|
|
|
|
|
callback = function()
|
|
|
|
|
G_reader_settings:saveSetting("tap_to_follow_links",
|
|
|
|
|
not isTapToFollowLinksOn())
|
|
|
|
|
end,
|
|
|
|
|
separator = true,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
text = _("Swipe to go back"),
|
|
|
|
|
checked_func = isSwipeToGoBackEnabled,
|
|
|
|
@ -106,42 +112,64 @@ function ReaderLink:addToMainMenu(menu_items)
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function ReaderLink:onSetDimensions(dimen)
|
|
|
|
|
-- update listening according to new screen dimen
|
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
|
self:initGesListener()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function ReaderLink:onTap(_, ges)
|
|
|
|
|
if not isFollowLinksOn() then return end
|
|
|
|
|
--- Gets link from gesture.
|
|
|
|
|
-- `Document:getLinkFromPosition()` behaves differently depending on
|
|
|
|
|
-- document type, so this function provides a wrapper.
|
|
|
|
|
function ReaderLink:getLinkFromGes(ges)
|
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
|
local pos = self.view:screenToPageTransform(ges.pos)
|
|
|
|
|
if pos then
|
|
|
|
|
-- link box in native page
|
|
|
|
|
local link, lbox = self.ui.document:getLinkFromPosition(pos.page, pos)
|
|
|
|
|
if link and lbox then
|
|
|
|
|
-- screen box that holds the link
|
|
|
|
|
local sbox = self.view:pageToScreenTransform(pos.page,
|
|
|
|
|
self.ui.document:nativeToPageRectTransform(pos.page, lbox))
|
|
|
|
|
if sbox then
|
|
|
|
|
UIManager:show(LinkBox:new{
|
|
|
|
|
box = sbox,
|
|
|
|
|
timeout = FOLLOW_LINK_TIMEOUT,
|
|
|
|
|
callback = function() self:onGotoLink(link) end
|
|
|
|
|
})
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
return link, lbox, pos
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
local link = self.ui.document:getLinkFromPosition(ges.pos)
|
|
|
|
|
if link ~= "" then
|
|
|
|
|
return link
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Highlights a linkbox if available and goes to it.
|
|
|
|
|
function ReaderLink:showLinkBox(link, lbox, pos)
|
|
|
|
|
if link and lbox then
|
|
|
|
|
-- screen box that holds the link
|
|
|
|
|
local sbox = self.view:pageToScreenTransform(pos.page,
|
|
|
|
|
self.ui.document:nativeToPageRectTransform(pos.page, lbox))
|
|
|
|
|
if sbox then
|
|
|
|
|
UIManager:show(LinkBox:new{
|
|
|
|
|
box = sbox,
|
|
|
|
|
timeout = FOLLOW_LINK_TIMEOUT,
|
|
|
|
|
callback = function() self:onGotoLink(link) end
|
|
|
|
|
})
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
if link ~= "" then
|
|
|
|
|
return self:onGotoLink(link)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function ReaderLink:onSetDimensions(dimen)
|
|
|
|
|
-- update listening according to new screen dimen
|
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
|
self:initGesListener()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function ReaderLink:onTap(_, ges)
|
|
|
|
|
if not isTapToFollowLinksOn() then return end
|
|
|
|
|
local link, lbox, pos = self:getLinkFromGes(ges)
|
|
|
|
|
if link then
|
|
|
|
|
self:showLinkBox(link, lbox, pos)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Goes to link.
|
|
|
|
|
function ReaderLink:onGotoLink(link)
|
|
|
|
|
logger.dbg("onGotoLink:", link)
|
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
@ -196,6 +224,7 @@ function ReaderLink:onGotoLink(link)
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Goes back to previous location.
|
|
|
|
|
function ReaderLink:onGoBackLink()
|
|
|
|
|
local saved_location = table.remove(self.location_stack)
|
|
|
|
|
if saved_location then
|
|
|
|
@ -216,8 +245,8 @@ function ReaderLink:onSwipe(_, ges)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Goes to first link on page.
|
|
|
|
|
function ReaderLink:onGoToFirstLink(ges)
|
|
|
|
|
if not isFollowLinksOn() then return end
|
|
|
|
|
local firstlink = nil
|
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
|
local pos = self.view:screenToPageTransform(ges.pos)
|
|
|
|
|