2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/frontend/apps/reader/readerui.lua

513 lines
16 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local InputContainer = require("ui/widget/container/inputcontainer")
local DocumentRegistry = require("document/documentregistry")
local Screenshoter = require("ui/widget/screenshoter")
local InfoMessage = require("ui/widget/infomessage")
local InputDialog = require("ui/widget/inputdialog")
local ConfirmBox = require("ui/widget/confirmbox")
local lfs = require("libs/libkoreader-lfs")
local DocSettings = require("docsettings")
local UIManager = require("ui/uimanager")
2013-10-18 20:38:07 +00:00
local Geom = require("ui/geometry")
local Device = require("device")
local Screen = require("device").screen
2013-10-18 20:38:07 +00:00
local Event = require("ui/event")
local Cache = require("cache")
local dbg = require("dbg")
local T = require("ffi/util").template
2013-10-18 20:38:07 +00:00
local _ = require("gettext")
local ReaderView = require("apps/reader/modules/readerview")
local ReaderZooming = require("apps/reader/modules/readerzooming")
local ReaderPanning = require("apps/reader/modules/readerpanning")
local ReaderRotation = require("apps/reader/modules/readerrotation")
local ReaderPaging = require("apps/reader/modules/readerpaging")
local ReaderRolling = require("apps/reader/modules/readerrolling")
local ReaderToc = require("apps/reader/modules/readertoc")
local ReaderBookmark = require("apps/reader/modules/readerbookmark")
local ReaderFont = require("apps/reader/modules/readerfont")
local ReaderTypeset = require("apps/reader/modules/readertypeset")
local ReaderMenu = require("apps/reader/modules/readermenu")
local ReaderGoto = require("apps/reader/modules/readergoto")
local ReaderConfig = require("apps/reader/modules/readerconfig")
local ReaderCropping = require("apps/reader/modules/readercropping")
local ReaderKoptListener = require("apps/reader/modules/readerkoptlistener")
local ReaderCoptListener = require("apps/reader/modules/readercoptlistener")
local ReaderHinting = require("apps/reader/modules/readerhinting")
local ReaderHighlight = require("apps/reader/modules/readerhighlight")
local ReaderFrontLight = require("apps/reader/modules/readerfrontlight")
local ReaderDictionary = require("apps/reader/modules/readerdictionary")
local ReaderWikipedia = require("apps/reader/modules/readerwikipedia")
local ReaderHyphenation = require("apps/reader/modules/readerhyphenation")
local ReaderActivityIndicator = require("apps/reader/modules/readeractivityindicator")
2014-11-05 04:28:11 +00:00
local FileManagerHistory = require("apps/filemanager/filemanagerhistory")
local ReaderSearch = require("apps/reader/modules/readersearch")
local ReaderLink = require("apps/reader/modules/readerlink")
2016-02-02 17:38:14 +00:00
local ReaderStatus = require("apps/reader/modules/readerstatus")
local PluginLoader = require("apps/reader/pluginloader")
--[[
This is an abstraction for a reader interface
it works using data gathered from a document interface
]]--
2013-10-18 20:38:07 +00:00
local ReaderUI = InputContainer:new{
name = "ReaderUI",
2014-03-13 13:52:43 +00:00
key_events = {
Close = { { "Home" },
doc = "close document", event = "Close" },
2014-03-13 13:52:43 +00:00
},
active_widgets = {},
2014-03-13 13:52:43 +00:00
-- our own size
dimen = Geom:new{ w = 400, h = 600 },
-- if we have a parent container, it must be referenced for now
dialog = nil,
2014-03-13 13:52:43 +00:00
-- the document interface
document = nil,
2014-03-13 13:52:43 +00:00
-- password for document unlock
password = nil,
2014-03-13 13:52:43 +00:00
postInitCallback = nil,
}
function ReaderUI:registerModule(name, ui_module, always_active)
if name then self[name] = ui_module end
ui_module.name = "reader" .. name
table.insert(always_active and self.active_widgets or self, ui_module)
end
function ReaderUI:registerPostInitCallback(callback)
table.insert(self.postInitCallback, callback)
end
function ReaderUI:init()
2014-03-13 13:52:43 +00:00
self.postInitCallback = {}
-- if we are not the top level dialog ourselves, it must be given in the table
if not self.dialog then
self.dialog = self
end
2012-06-26 09:05:09 +00:00
if Device:hasKeys() then
2014-03-13 13:52:43 +00:00
self.key_events.Back = {
{ "Back" }, doc = "close document",
2014-03-13 13:52:43 +00:00
event = "Close" }
end
2014-03-13 13:52:43 +00:00
self.doc_settings = DocSettings:open(self.document.file)
2012-06-26 09:05:09 +00:00
2014-03-13 13:52:43 +00:00
-- a view container (so it must be child #1!)
self:registerModule("view", ReaderView:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
dimen = self.dimen,
ui = self,
document = self.document,
})
-- goto link controller
self:registerModule("link", ReaderLink:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self,
document = self.document,
})
-- text highlight
self:registerModule("highlight", ReaderHighlight:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self,
document = self.document,
})
-- menu widget should be registered after link widget and highlight widget
-- so that taps on link and highlight areas won't popup reader menu
-- reader menu controller
self:registerModule("menu", ReaderMenu:new{
view = self.view,
ui = self
})
2014-03-13 13:52:43 +00:00
-- rotation controller
self:registerModule("rotation", ReaderRotation:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
-- Table of content controller
self:registerModule("toc", ReaderToc:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
2014-03-13 13:52:43 +00:00
-- bookmark controller
self:registerModule("bookmark", ReaderBookmark:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
-- reader goto controller
-- "goto" being a dirty keyword in Lua?
self:registerModule("gotopage", ReaderGoto:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self,
document = self.document,
})
-- dictionary
self:registerModule("dictionary", ReaderDictionary:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self,
document = self.document,
})
-- wikipedia
self:registerModule("wikipedia", ReaderWikipedia:new{
dialog = self.dialog,
view = self.view,
ui = self,
document = self.document,
})
2014-03-13 13:52:43 +00:00
-- screenshot controller
self:registerModule("screenshot", Screenshoter:new{
prefix = 'Reader',
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
}, true)
2014-03-13 13:52:43 +00:00
-- frontlight controller
if Device:hasFrontlight() then
self:registerModule("frontlight", ReaderFrontLight:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
end
-- configuable controller
if self.document.info.configurable then
-- config panel controller
self:registerModule("config", ReaderConfig:new{
2014-03-13 13:52:43 +00:00
configurable = self.document.configurable,
options = self.document.options,
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
if self.document.info.has_pages then
-- kopt option controller
self:registerModule("koptlistener", ReaderKoptListener:new{
dialog = self.dialog,
view = self.view,
ui = self,
document = self.document,
})
else
2014-03-13 13:52:43 +00:00
-- cre option controller
self:registerModule("crelistener", ReaderCoptListener:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self,
document = self.document,
})
end
-- activity indicator when some configurations take long take to affect
self:registerModule("activityindicator", ReaderActivityIndicator:new{
dialog = self.dialog,
view = self.view,
ui = self,
document = self.document,
})
2014-03-13 13:52:43 +00:00
end
-- for page specific controller
if self.document.info.has_pages then
-- cropping controller
self:registerModule("cropping", ReaderCropping:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self,
document = self.document,
})
-- paging controller
self:registerModule("paging", ReaderPaging:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
-- zooming controller
self:registerModule("zooming", ReaderZooming:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
2014-03-13 13:52:43 +00:00
-- panning controller
self:registerModule("panning", ReaderPanning:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
-- hinting controller
self:registerModule("hinting", ReaderHinting:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
zoom = self.zooming,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self,
document = self.document,
})
else
-- make sure we render document first before calling any callback
self:registerPostInitCallback(function()
self.document:loadDocument()
-- used to read additional settings after the document has been
-- loaded (but not rendered yet)
self:handleEvent(Event:new("PreRenderDocument", self.doc_settings))
self.document:render()
2014-03-13 13:52:43 +00:00
end)
-- typeset controller
self:registerModule("typeset", ReaderTypeset:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
-- font menu
self:registerModule("font", ReaderFont:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
2014-03-13 13:52:43 +00:00
-- hyphenation menu
self:registerModule("hyphenation", ReaderHyphenation:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
2014-03-13 13:52:43 +00:00
-- rolling controller
self:registerModule("rolling", ReaderRolling:new{
2014-03-13 13:52:43 +00:00
dialog = self.dialog,
view = self.view,
2014-03-13 13:52:43 +00:00
ui = self
})
self.disable_double_tap = G_reader_settings:readSetting("disable_double_tap") ~= false
2014-03-13 13:52:43 +00:00
end
2014-11-05 04:28:11 +00:00
-- fulltext search
self:registerModule("search", ReaderSearch:new{
dialog = self.dialog,
view = self.view,
ui = self
})
-- book status
2016-02-02 17:38:14 +00:00
self:registerModule("status", ReaderStatus:new{
ui = self,
document = self.document,
view = self.view,
2016-02-02 17:38:14 +00:00
})
-- history view
self:registerModule("history", FileManagerHistory:new{
dialog = self.dialog,
ui = self,
})
-- koreader plugins
for _,plugin_module in ipairs(PluginLoader:loadPlugins()) do
dbg("Loaded plugin", plugin_module.name, "at", plugin_module.path)
self:registerModule(plugin_module.name, plugin_module:new{
dialog = self.dialog,
view = self.view,
ui = self,
document = self.document,
})
end
--dbg(self.doc_settings)
2014-03-13 13:52:43 +00:00
-- we only read settings after all the widgets are initialized
self:handleEvent(Event:new("ReadSettings", self.doc_settings))
2014-03-13 13:52:43 +00:00
for _,v in ipairs(self.postInitCallback) do
v()
end
-- After initialisation notify that document is loaded and rendered
-- CREngine only reports correct page count after rendering is done
-- Need the same event for PDF document
self:handleEvent(Event:new("ReaderReady", self.doc_settings))
end
function ReaderUI:showReader(file)
dbg("show reader ui")
require("readhistory"):addItem(file)
if lfs.attributes(file, "mode") ~= "file" then
UIManager:show(InfoMessage:new{
text = T(_("File '%1' does not exist."), file)
})
return
end
UIManager:show(InfoMessage:new{
text = T(_("Opening file '%1'."), file),
timeout = 0.0,
})
-- doShowReader might block for a long time, so force repaint here
UIManager:forceRePaint()
2016-03-05 20:37:45 +00:00
UIManager:nextTick(function()
dbg("creating coroutine for showing reader")
local co = coroutine.create(function()
self:doShowReader(file)
end)
local ok, err = coroutine.resume(co)
2016-01-03 08:56:55 +00:00
if err ~= nil or ok == false then
2016-03-18 04:52:57 +00:00
io.stderr:write('[!] doShowReader coroutine crashed:\n')
io.stderr:write(debug.traceback(co, err, 1))
2016-03-05 20:37:45 +00:00
UIManager:quit()
end
end)
end
local _running_instance = nil
function ReaderUI:doShowReader(file)
dbg("opening file", file)
-- keep only one instance running
if _running_instance then
_running_instance:onClose()
end
local document = DocumentRegistry:openDocument(file)
if not document then
UIManager:show(InfoMessage:new{
text = _("No reader engine for this file.")
})
return
end
if document.is_locked then
dbg("document is locked")
self._coroutine = coroutine.running() or self._coroutine
self:unlockDocumentWithPassword(document)
if coroutine.running() then
local unlock_success = coroutine.yield()
if not unlock_success then
return
end
end
end
G_reader_settings:saveSetting("lastfile", file)
local reader = ReaderUI:new{
dimen = Screen:getSize(),
document = document,
}
UIManager:show(reader)
_running_instance = reader
end
2016-02-17 08:06:55 +00:00
function ReaderUI:_getRunningInstance()
return _running_instance
2016-02-17 08:06:55 +00:00
end
function ReaderUI:unlockDocumentWithPassword(document, try_again)
dbg("show input password dialog")
self.password_dialog = InputDialog:new{
title = try_again and _("Password is incorrect, try again?")
or _("Input document password"),
buttons = {
{
{
text = _("Cancel"),
enabled = true,
callback = function()
self:closeDialog()
coroutine.resume(self._coroutine)
end,
},
{
text = _("OK"),
enabled = true,
callback = function()
local success = self:onVerifyPassword(document)
self:closeDialog()
if success then
coroutine.resume(self._coroutine, success)
else
self:unlockDocumentWithPassword(document, true)
end
end,
},
},
},
text_type = "password",
}
self.password_dialog:onShowKeyboard()
UIManager:show(self.password_dialog)
end
function ReaderUI:onVerifyPassword(document)
local password = self.password_dialog:getInputText()
return document:unlock(password)
end
function ReaderUI:closeDialog()
self.password_dialog:onClose()
UIManager:close(self.password_dialog)
end
2012-06-12 16:14:23 +00:00
function ReaderUI:onSetDimensions(dimen)
2014-03-13 13:52:43 +00:00
self.dimen = dimen
2012-06-12 16:14:23 +00:00
end
2013-12-27 15:18:16 +00:00
function ReaderUI:saveSettings()
2014-03-13 13:52:43 +00:00
self:handleEvent(Event:new("SaveSettings"))
self.doc_settings:flush()
G_reader_settings:flush()
2013-12-27 15:18:16 +00:00
end
function ReaderUI:onFlushSettings()
self:saveSettings()
return true
end
function ReaderUI:closeDocument()
self.document:close()
self.document = nil
end
function ReaderUI:notifyCloseDocument()
self:handleEvent(Event:new("CloseDocument"))
if self.document:isEdited() then
local setting = G_reader_settings:readSetting("save_document")
if setting == "always" then
self:closeDocument()
elseif setting == "disable" then
self.document:discardChange()
self:closeDocument()
else
UIManager:show(ConfirmBox:new{
text = _("Do you want to save this document?"),
ok_text = _("Yes"),
cancel_text = _("No"),
ok_callback = function()
self:closeDocument()
end,
cancel_callback = function()
self.document:discardChange()
self:closeDocument()
end,
})
end
else
self:closeDocument()
end
end
function ReaderUI:onClose()
dbg("closing reader")
2014-03-13 13:52:43 +00:00
self:saveSettings()
if self.document ~= nil then
dbg("closing document")
self:notifyCloseDocument()
2014-03-13 13:52:43 +00:00
end
UIManager:close(self.dialog, "full")
-- serialize last used items for later launch
Cache:serialize()
if _running_instance == self then
_running_instance = nil
end
2014-03-13 13:52:43 +00:00
return true
end
2013-10-18 20:38:07 +00:00
return ReaderUI