2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/apps/reader/modules/readerbookmark.lua

243 lines
6.9 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local InputContainer = require("ui/widget/container/inputcontainer")
local CenterContainer = require("ui/widget/container/centercontainer")
local Menu = require("ui/widget/menu")
local Device = require("ui/device")
local GestureRange = require("ui/gesturerange")
local Geom = require("ui/geometry")
local Screen = require("ui/screen")
local UIManager = require("ui/uimanager")
local Event = require("ui/event")
2014-06-10 13:32:49 +00:00
local Font = require("ui/font")
local DEBUG = require("dbg")
2013-10-18 20:38:07 +00:00
local _ = require("gettext")
local ReaderBookmark = InputContainer:new{
2014-03-13 13:52:43 +00:00
bm_menu_title = _("Bookmarks"),
bookmarks = nil,
2013-01-01 19:38:29 +00:00
}
function ReaderBookmark:init()
2014-03-13 13:52:43 +00:00
if Device:hasKeyboard() then
self.key_events = {
ShowBookmark = {
{ "B" },
doc = "show bookmarks" },
2014-03-13 13:52:43 +00:00
}
end
if Device:isTouchDevice() then
self.ges_events = {
ShowBookmark = {
GestureRange:new{
ges = "two_finger_swipe",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
},
direction = "west"
}
},
}
end
self.ui.menu:registerToMainMenu(self)
2013-01-01 19:38:29 +00:00
end
function ReaderBookmark:onReadSettings(config)
2014-03-13 13:52:43 +00:00
self.bookmarks = config:readSetting("bookmarks") or {}
2013-01-01 19:38:29 +00:00
end
2013-12-27 15:18:16 +00:00
function ReaderBookmark:onSaveSettings()
2014-03-13 13:52:43 +00:00
self.ui.doc_settings:saveSetting("bookmarks", self.bookmarks)
2013-01-01 19:38:29 +00:00
end
function ReaderBookmark:onToggleBookmark()
2014-03-13 13:52:43 +00:00
local pn_or_xp = nil
if self.ui.document.info.has_pages then
pn_or_xp = self.view.state.page
else
pn_or_xp = self.ui.document:getXPointer()
end
self:toggleBookmark(pn_or_xp)
self.view.dogear_visible = not self.view.dogear_visible
UIManager:setDirty(self.view.dialog, "partial")
return true
end
function ReaderBookmark:setDogearVisibility(pn_or_xp)
2014-03-13 13:52:43 +00:00
if self:isBookmarked(pn_or_xp) then
self.ui:handleEvent(Event:new("SetDogearVisibility", true))
else
self.ui:handleEvent(Event:new("SetDogearVisibility", false))
end
2013-01-01 19:38:29 +00:00
end
function ReaderBookmark:onPageUpdate(pageno)
2014-03-13 13:52:43 +00:00
if self.ui.document.info.has_pages then
self:setDogearVisibility(pageno)
else
-- FIXME: this is a dirty hack to prevent crash in isXPointerInCurrentPage
if pageno ~= 1 then
self:setDogearVisibility("dummy")
end
end
end
function ReaderBookmark:onPosUpdate(pos)
2014-03-13 13:52:43 +00:00
self:setDogearVisibility("dummy")
end
2013-01-01 19:38:29 +00:00
function ReaderBookmark:onShowBookmark()
2014-03-13 13:52:43 +00:00
-- build up item_table
for k, v in ipairs(self.bookmarks) do
local page = v.page
-- for CREngine, bookmark page is xpointer
if type(page) == "string" then
page = self.ui.document:getPageFromXPointer(v.page)
end
v.text = "Page "..page.." "..v.notes.." @ "..v.datetime
end
local bm_menu = Menu:new{
title = "Bookmarks",
item_table = self.bookmarks,
2014-06-10 13:32:49 +00:00
is_borderless = true,
width = Screen:getWidth(),
height = Screen:getHeight(),
2014-06-10 13:32:49 +00:00
cface = Font:getFace("cfont", 20),
on_close_ges = {
GestureRange:new{
ges = "two_finger_swipe",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
},
direction = "east"
}
}
}
local menu_container = CenterContainer:new{
dimen = Screen:getSize(),
bm_menu,
2014-03-13 13:52:43 +00:00
}
2014-03-13 13:52:43 +00:00
-- buid up menu widget method as closure
local doc = self.ui.document
local view = self.view
local sendEv = function(ev)
self.ui:handleEvent(ev)
end
function bm_menu:onMenuChoice(item)
if doc.info.has_pages then
sendEv(Event:new("PageUpdate", item.page))
elseif view.view_mode == "page" then
sendEv(Event:new("PageUpdate", doc:getPageFromXPointer(item.page)))
else
sendEv(Event:new("PosUpdate", doc:getPosFromXPointer(item.page)))
end
end
bm_menu.close_callback = function()
UIManager:close(menu_container)
end
bm_menu.show_parent = menu_container
2014-03-13 13:52:43 +00:00
UIManager:show(menu_container)
return true
2013-01-01 19:38:29 +00:00
end
2013-03-14 05:58:42 +00:00
function ReaderBookmark:addToMainMenu(tab_item_table)
2014-03-13 13:52:43 +00:00
-- insert table to main reader menu
table.insert(tab_item_table.navi, {
text = self.bm_menu_title,
callback = function()
self:onShowBookmark()
end,
})
2013-01-01 19:38:29 +00:00
end
function ReaderBookmark:isBookmarked(pn_or_xp)
2014-03-13 13:52:43 +00:00
for k,v in ipairs(self.bookmarks) do
if (type(pn_or_xp) == "number" and v.page == pn_or_xp) or
(type(pn_or_xp) == "string" and self.ui.document:isXPointerInCurrentPage(v.page)) then
return true
end
end
return false
end
function ReaderBookmark:addBookmark(pn_or_xp)
2014-03-13 13:52:43 +00:00
-- build notes from TOC
local notes = self.ui.toc:getTocTitleByPage(pn_or_xp)
if notes ~= "" then
notes = "in "..notes
end
mark_item = {
page = pn_or_xp,
datetime = os.date("%Y-%m-%d %H:%M:%S"),
notes = notes,
}
table.insert(self.bookmarks, mark_item)
table.sort(self.bookmarks, function(a,b)
return self:isBookmarkInSequence(a, b)
end)
return true
2013-03-12 17:18:53 +00:00
end
2013-01-01 19:38:29 +00:00
function ReaderBookmark:isBookmarkInSequence(a, b)
2014-03-13 13:52:43 +00:00
return a.page < b.page
2013-01-01 19:38:29 +00:00
end
function ReaderBookmark:toggleBookmark(pn_or_xp)
2014-03-13 13:52:43 +00:00
for k,v in ipairs(self.bookmarks) do
if (type(pn_or_xp) == "number" and v.page == pn_or_xp) or
(type(pn_or_xp) == "string" and self.ui.document:isXPointerInCurrentPage(v.page)) then
table.remove(self.bookmarks, k)
return
end
end
self:addBookmark(pn_or_xp)
end
2013-10-18 20:38:07 +00:00
function ReaderBookmark:getPreviousBookmarkedPage(pn_or_xp)
2014-03-13 13:52:43 +00:00
for i = #self.bookmarks, 1, -1 do
if pn_or_xp > self.bookmarks[i].page then
return self.bookmarks[i].page
end
end
end
function ReaderBookmark:getNextBookmarkedPage(pn_or_xp)
2014-03-13 13:52:43 +00:00
for i = 1, #self.bookmarks do
if pn_or_xp < self.bookmarks[i].page then
return self.bookmarks[i].page
end
end
end
function ReaderBookmark:onGotoPreviousBookmark(pn_or_xp)
2014-03-13 13:52:43 +00:00
self:GotoBookmark(self:getPreviousBookmarkedPage(pn_or_xp))
return true
end
function ReaderBookmark:onGotoNextBookmark(pn_or_xp)
2014-03-13 13:52:43 +00:00
self:GotoBookmark(self:getNextBookmarkedPage(pn_or_xp))
return true
end
function ReaderBookmark:GotoBookmark(pn_or_xp)
2014-03-13 13:52:43 +00:00
if type(pn_or_xp) == "string" then
if self.view.view_mode == "page" then
self.ui:handleEvent(Event:new("PageUpdate", self.ui.document:getPageFromXPointer(pn_or_xp)))
else
self.ui:handleEvent(Event:new("PosUpdate", self.ui.document:getPosFromXPointer(pn_or_xp)))
end
elseif type(pn_or_xp) == "number" then
self.ui:handleEvent(Event:new("PageUpdate", pn_or_xp))
end
end
2013-10-18 20:38:07 +00:00
return ReaderBookmark