mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
refactoring readertoc and readerfooter
so that they don't repeat themselves.
This commit is contained in:
@@ -20,6 +20,7 @@ local ReaderFooter = InputContainer:new{
|
||||
visible = true,
|
||||
pageno = nil,
|
||||
pages = nil,
|
||||
toc_level = 0,
|
||||
progress_percentage = 0.0,
|
||||
progress_text = nil,
|
||||
text_font_face = "ffont",
|
||||
@@ -37,35 +38,39 @@ function ReaderFooter:init()
|
||||
self.pageno = self.view.state.page
|
||||
self.pages = self.view.document:getPageCount()
|
||||
|
||||
local progress_text_default = ""
|
||||
local text_default = ""
|
||||
if DMINIBAR_ALL_AT_ONCE then
|
||||
local info = {}
|
||||
if DMINIBAR_BATTERY then
|
||||
table.insert(info, "B:100%")
|
||||
end
|
||||
if DMINIBAR_TIME then
|
||||
progress_text_default = progress_text_default .. " | WW:WW"
|
||||
table.insert(info, "WW:WW")
|
||||
end
|
||||
if DMINIBAR_PAGES then
|
||||
progress_text_default = progress_text_default .. " | 0000 / 0000"
|
||||
table.insert(info, "0000 / 0000")
|
||||
end
|
||||
if DMINIBAR_NEXT_CHAPTER then
|
||||
progress_text_default = progress_text_default .. " | => 000"
|
||||
table.insert(info, "=> 000")
|
||||
end
|
||||
if DMINIBAR_BATTERY then
|
||||
progress_text_default = progress_text_default .. " | B:100%"
|
||||
end
|
||||
progress_text_default = string.sub(progress_text_default, 4)
|
||||
text_default = table.concat(info, " | ")
|
||||
else
|
||||
progress_text_default = string.format(" %d / %d ", self.pages, self.pages)
|
||||
text_default = string.format(" %d / %d ", self.pages, self.pages)
|
||||
end
|
||||
|
||||
self.progress_text = TextWidget:new{
|
||||
text = progress_text_default,
|
||||
text = text_default,
|
||||
face = Font:getFace(self.text_font_face, self.text_font_size),
|
||||
}
|
||||
local text_width = self.progress_text:getSize().w
|
||||
local ticks = (self.ui.toc and DMINIBAR_PROGRESS_MARKER)
|
||||
and self.ui.toc:getTocTicks(self.toc_level) or {}
|
||||
self.progress_bar = ProgressWidget:new{
|
||||
width = math.floor(Screen:getWidth() - text_width - self.padding),
|
||||
height = self.bar_height,
|
||||
percentage = self.progress_percentage,
|
||||
TOC = self.ui.document:getToc(),
|
||||
ticks = ticks,
|
||||
tick_width = DMINIBAR_TOC_MARKER_WIDTH,
|
||||
last = self.pages,
|
||||
}
|
||||
local horizontal_group = HorizontalGroup:new{}
|
||||
@@ -121,42 +126,55 @@ function ReaderFooter:init()
|
||||
self:applyFooterMode()
|
||||
end
|
||||
|
||||
function ReaderFooter:fillToc()
|
||||
self.toc = self.ui.document:getToc()
|
||||
function ReaderFooter:getBatteryInfo()
|
||||
local powerd = Device:getPowerDevice()
|
||||
--local state = powerd:isCharging() and -1 or powerd:getCapacity()
|
||||
return "B:" .. powerd:getCapacity() .. "%"
|
||||
end
|
||||
|
||||
function ReaderFooter:getTimeInfo()
|
||||
return os.date("%H:%M")
|
||||
end
|
||||
|
||||
function ReaderFooter:getProgressInfo()
|
||||
return string.format("%d / %d", self.pageno, self.pages)
|
||||
end
|
||||
|
||||
function ReaderFooter:getNextChapterInfo()
|
||||
local left = self.ui.toc:getChapterPagesLeft(self.pageno, self.toc_level)
|
||||
return "=> " .. (left and left or self.pages - self.pageno)
|
||||
end
|
||||
|
||||
function ReaderFooter:updateFooterPage()
|
||||
if type(self.pageno) ~= "number" then return end
|
||||
self.progress_bar.percentage = self.pageno / self.pages
|
||||
if DMINIBAR_ALL_AT_ONCE then
|
||||
self.progress_text.text = ""
|
||||
local info = {}
|
||||
if DMINIBAR_BATTERY then
|
||||
local powerd = Device:getPowerDevice()
|
||||
local state = powerd:isCharging() and -1 or powerd:getCapacity()
|
||||
self.progress_text.text = self.progress_text.text .. " | B:" .. powerd:getCapacity() .. "%"
|
||||
table.insert(info, self:getBatteryInfo())
|
||||
end
|
||||
if DMINIBAR_TIME then
|
||||
self.progress_text.text = self.progress_text.text .. " | " .. os.date("%H:%M")
|
||||
table.insert(info, self:getTimeInfo())
|
||||
end
|
||||
if DMINIBAR_PAGES then
|
||||
self.progress_text.text = self.progress_text.text .. " | " .. string.format("%d / %d", self.pageno, self.pages)
|
||||
table.insert(info, self:getProgressInfo())
|
||||
end
|
||||
if DMINIBAR_NEXT_CHAPTER then
|
||||
self.progress_text.text = self.progress_text.text .. " | => " .. self.ui.toc:_getChapterPagesLeft(self.pageno,self.pages)
|
||||
table.insert(info, self:getNextChapterInfo())
|
||||
end
|
||||
self.progress_text.text = string.sub(self.progress_text.text, 4)
|
||||
self.progress_text.text = table.concat(info, " | ")
|
||||
else
|
||||
local info = ""
|
||||
if self.mode == 1 then
|
||||
self.progress_text.text = string.format("%d / %d", self.pageno, self.pages)
|
||||
info = self:getProgressInfo()
|
||||
elseif self.mode == 2 then
|
||||
self.progress_text.text = os.date("%H:%M")
|
||||
info = self:getTimeInfo()
|
||||
elseif self.mode == 3 then
|
||||
self.progress_text.text = "=> " .. self.ui.toc:_getChapterPagesLeft(self.pageno,self.pages)
|
||||
info = self:getNextChapterInfo()
|
||||
elseif self.mode == 4 then
|
||||
local powerd = Device:getPowerDevice()
|
||||
local state = powerd:isCharging() and -1 or powerd:getCapacity()
|
||||
self.progress_text.text = "B:" .. powerd:getCapacity() .. "%"
|
||||
info = self:getBatteryInfo()
|
||||
end
|
||||
self.progress_text.text = info
|
||||
end
|
||||
|
||||
end
|
||||
@@ -166,9 +184,10 @@ function ReaderFooter:updateFooterPos()
|
||||
self.progress_bar.percentage = self.position / self.doc_height
|
||||
|
||||
if self.show_time then
|
||||
self.progress_text.text = os.date("%H:%M")
|
||||
self.progress_text.text = self:getTimeInfo()
|
||||
else
|
||||
self.progress_text.text = string.format("%1.f", self.progress_bar.percentage*100).."%"
|
||||
local percentage = self.progress_bar.percentage
|
||||
self.progress_text.text = string.format("%1.f", percentage*100) .. "%"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -247,18 +247,12 @@ function ReaderRolling:onResume()
|
||||
end
|
||||
|
||||
function ReaderRolling:onDoubleTapForward()
|
||||
local i = self.ui.toc:_getNextChapter(self.current_page+self.ui.document:getVisiblePageCount())
|
||||
if i ~= "" then
|
||||
self:onGotoPage(i)
|
||||
end
|
||||
self:onGotoPage(self.ui.toc:getNextChapter(self.current_page))
|
||||
return true
|
||||
end
|
||||
|
||||
function ReaderRolling:onDoubleTapBackward()
|
||||
local i = self.ui.toc:_getPreviousChapter(self.current_page)
|
||||
if i ~= "" then
|
||||
self:onGotoPage(i)
|
||||
end
|
||||
self:onGotoPage(self.ui.toc:getPreviousChapter(self.current_page))
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -414,7 +408,9 @@ function ReaderRolling:gotoPercent(new_percent)
|
||||
end
|
||||
|
||||
function ReaderRolling:onGotoPage(number)
|
||||
self:gotoPage(number)
|
||||
if number then
|
||||
self:gotoPage(number)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ local _ = require("gettext")
|
||||
|
||||
local ReaderToc = InputContainer:new{
|
||||
toc = nil,
|
||||
ticks = {},
|
||||
toc_menu_title = _("Table of contents"),
|
||||
}
|
||||
|
||||
@@ -60,30 +61,17 @@ function ReaderToc:onPageUpdate(pageno)
|
||||
end
|
||||
|
||||
function ReaderToc:fillToc()
|
||||
if self.toc and #self.toc > 0 then return end
|
||||
self.toc = self.ui.document:getToc()
|
||||
end
|
||||
|
||||
-- _getTocTitleByPage wrapper, so specific reader
|
||||
-- can tranform pageno according its need
|
||||
function ReaderToc:getTocTitleByPage(pn_or_xp)
|
||||
local page = pn_or_xp
|
||||
self:fillToc()
|
||||
if #self.toc == 0 then return "" end
|
||||
local pageno = pn_or_xp
|
||||
if type(pn_or_xp) == "string" then
|
||||
page = self.ui.document:getPageFromXPointer(pn_or_xp)
|
||||
pageno = self.ui.document:getPageFromXPointer(pn_or_xp)
|
||||
end
|
||||
return self:_getTocTitleByPage(page)
|
||||
end
|
||||
|
||||
function ReaderToc:_getTocTitleByPage(pageno)
|
||||
if not self.toc then
|
||||
-- build toc when needed.
|
||||
self:fillToc()
|
||||
end
|
||||
|
||||
-- no table of content
|
||||
if #self.toc == 0 then
|
||||
return ""
|
||||
end
|
||||
|
||||
local pre_entry = self.toc[1]
|
||||
for _k,_v in ipairs(self.toc) do
|
||||
if _v.page > pageno then
|
||||
@@ -98,135 +86,91 @@ function ReaderToc:getTocTitleOfCurrentPage()
|
||||
return self:getTocTitleByPage(self.pageno)
|
||||
end
|
||||
|
||||
function ReaderToc:_getChapterPagesLeft(pageno,pages)
|
||||
local i
|
||||
local j = 0
|
||||
|
||||
if not self.toc then
|
||||
-- build toc when needed.
|
||||
self:fillToc()
|
||||
end
|
||||
|
||||
-- no table of content
|
||||
if #self.toc == 0 then
|
||||
return ""
|
||||
end
|
||||
|
||||
if #self.toc > 0 then
|
||||
for i = 1, #self.toc do
|
||||
v = self.toc[i]
|
||||
if v.page > pageno then
|
||||
j = v.page
|
||||
break
|
||||
end
|
||||
function ReaderToc:getMaxDepth()
|
||||
self:fillToc()
|
||||
local max_depth = 0
|
||||
for _, v in ipairs(self.toc) do
|
||||
if v.depth > max_depth then
|
||||
max_depth = v.depth
|
||||
end
|
||||
end
|
||||
if j == 0 then
|
||||
if pages > 0 then
|
||||
return pages-pageno
|
||||
return max_depth
|
||||
end
|
||||
|
||||
--[[
|
||||
TOC ticks is a list of page number in ascending order of TOC nodes at certain level
|
||||
positive level counts nodes of the depth level (level 1 for depth 1)
|
||||
non-positive level counts nodes of reversed depth level (level -1 for max_depth-1)
|
||||
--]]
|
||||
function ReaderToc:getTocTicks(level)
|
||||
if self.ticks[level] then return self.ticks[level] end
|
||||
-- build toc ticks if not found
|
||||
self:fillToc()
|
||||
local ticks = {}
|
||||
|
||||
if #self.toc > 0 then
|
||||
local depth = nil
|
||||
if level > 0 then
|
||||
depth = level
|
||||
else
|
||||
return ""
|
||||
depth = self:getMaxDepth() + level
|
||||
end
|
||||
else
|
||||
return j-pageno-1
|
||||
end
|
||||
end
|
||||
|
||||
function ReaderToc:_getChapterPagesDone(pageno)
|
||||
local i
|
||||
local j = 0
|
||||
|
||||
if not self.toc then
|
||||
-- build toc when needed.
|
||||
self:fillToc()
|
||||
end
|
||||
|
||||
-- no table of content
|
||||
if #self.toc == 0 then
|
||||
return ""
|
||||
end
|
||||
|
||||
if #self.toc > 0 then
|
||||
for i = 1, #self.toc do
|
||||
v = self.toc[i]
|
||||
if v.page >= pageno then
|
||||
break
|
||||
end
|
||||
j = v.page
|
||||
end
|
||||
end
|
||||
if j < 2 then
|
||||
return ""
|
||||
else
|
||||
return j-pageno
|
||||
end
|
||||
end
|
||||
|
||||
function ReaderToc:_getPreviousChapter(pageno)
|
||||
local i
|
||||
local j = 0
|
||||
|
||||
if not self.toc then
|
||||
-- build toc when needed.
|
||||
self:fillToc()
|
||||
end
|
||||
|
||||
-- no table of content
|
||||
if #self.toc == 0 then
|
||||
return ""
|
||||
end
|
||||
|
||||
if #self.toc > 0 then
|
||||
for i = 1, #self.toc do
|
||||
v = self.toc[i]
|
||||
if v.page >= pageno then
|
||||
break
|
||||
end
|
||||
j = v.page
|
||||
end
|
||||
end
|
||||
if j >= pageno then
|
||||
return ""
|
||||
else
|
||||
return j
|
||||
end
|
||||
end
|
||||
|
||||
function ReaderToc:_getNextChapter(pageno)
|
||||
local i
|
||||
local j = 0
|
||||
|
||||
if not self.toc then
|
||||
-- build toc when needed.
|
||||
self:fillToc()
|
||||
end
|
||||
|
||||
-- no table of content
|
||||
if #self.toc == 0 then
|
||||
return ""
|
||||
end
|
||||
|
||||
if #self.toc > 0 then
|
||||
for i = 1, #self.toc do
|
||||
v = self.toc[i]
|
||||
if v.page >= pageno then
|
||||
j = v.page
|
||||
break
|
||||
for _, v in ipairs(self.toc) do
|
||||
if v.depth == depth then
|
||||
table.insert(ticks, v.page)
|
||||
end
|
||||
end
|
||||
-- normally the ticks are sorted already but in rare cases
|
||||
-- toc nodes may be not in ascending order
|
||||
table.sort(ticks)
|
||||
-- cache ticks only if ticks are available
|
||||
self.ticks[level] = ticks
|
||||
end
|
||||
if j < pageno then
|
||||
return ""
|
||||
else
|
||||
return j
|
||||
end
|
||||
return ticks
|
||||
end
|
||||
|
||||
function ReaderToc:getNextChapter(cur_pageno, level)
|
||||
local ticks = self:getTocTicks(level)
|
||||
local next_chapter = nil
|
||||
for i = 1, #ticks do
|
||||
if ticks[i] > cur_pageno then
|
||||
next_chapter = ticks[i]
|
||||
break
|
||||
end
|
||||
end
|
||||
return next_chapter
|
||||
end
|
||||
|
||||
function ReaderToc:getPreviousChapter(cur_pageno, level)
|
||||
local ticks = self:getTocTicks(level)
|
||||
local previous_chapter = nil
|
||||
for i = 1, #ticks do
|
||||
if ticks[i] >= cur_pageno then
|
||||
break
|
||||
end
|
||||
previous_chapter = ticks[i]
|
||||
end
|
||||
return previous_chapter
|
||||
end
|
||||
|
||||
function ReaderToc:getChapterPagesLeft(pageno, level)
|
||||
local next_chapter = self:getNextChapter(pageno, level)
|
||||
if next_chapter then
|
||||
next_chapter = next_chapter - pageno
|
||||
end
|
||||
return next_chapter
|
||||
end
|
||||
|
||||
function ReaderToc:getChapterPagesDone(pageno, level)
|
||||
local previous_chapter = self:getPreviousChapter(pageno, level)
|
||||
if previous_chapter then
|
||||
previous_chapter = pageno - previous_chapter
|
||||
end
|
||||
return previous_chapter
|
||||
end
|
||||
|
||||
function ReaderToc:onShowToc()
|
||||
if not self.toc then
|
||||
self:fillToc()
|
||||
end
|
||||
self:fillToc()
|
||||
-- build menu items
|
||||
if #self.toc > 0 and not self.toc[1].text then
|
||||
for _,v in ipairs(self.toc) do
|
||||
|
||||
Reference in New Issue
Block a user