fix(readerfooter): marker ticks initialization

pull/1906/head
Qingping Hou 8 years ago
parent 6446f9d2b0
commit f48d9bd6d9

@ -38,7 +38,6 @@ local ReaderFooter = InputContainer:new{
function ReaderFooter:init()
self.pageno = self.view.state.page
self.pages = self.view.document:getPageCount()
self.settings = G_reader_settings:readSetting("footer") or {
disabled = false,
@ -69,25 +68,13 @@ function ReaderFooter:init()
}
self.text_width = self.progress_text:getSize().w + self.text_left_margin
self:applyFooterMode()
local ticks_candidates = {}
if self.ui.toc and self.settings.toc_markers then
local max_level = self.ui.toc:getMaxDepth()
for i = 0, -max_level, -1 do
local ticks = self.ui.toc:getTocTicks(i)
if #ticks < self.max_ticks then
table.insert(ticks_candidates, ticks)
end
end
-- find the finest toc ticks by sorting out the largest one
table.sort(ticks_candidates, function(a, b) return #a > #b end)
end
self.progress_bar = ProgressWidget:new{
width = nil, -- width will be updated in self:resetLayout()
height = self.bar_height,
percentage = self.progress_percentage,
ticks = ticks_candidates[1] or {},
tick_width = DMINIBAR_TOC_MARKER_WIDTH,
last = self.pages,
ticks = nil, -- ticks will be populated in self:updateFooterText
last = nil, -- last will be initialized in self:updateFooterText
}
local margin_span = HorizontalSpan:new{width=self.horizontal_margin}
local screen_width = Screen:getWidth()
@ -292,6 +279,30 @@ function ReaderFooter:updateFooterPos()
end
function ReaderFooter:updateFooterText()
if self.settings.toc_markers and self.progress_bar.ticks == nil then
local ticks_candidates = {}
if self.ui.toc then
local max_level = self.ui.toc:getMaxDepth()
for i = 0, -max_level, -1 do
local ticks = self.ui.toc:getTocTicks(i)
if #ticks < self.max_ticks then
table.insert(ticks_candidates, ticks)
end
end
-- find the finest toc ticks by sorting out the largest one
table.sort(ticks_candidates, function(a, b) return #a > #b end)
end
if #ticks_candidates > 0 then
self.progress_bar.ticks = ticks_candidates[1]
self.progress_bar.last = self.pages
else
-- we still set ticks here so self.progress_bar.ticks will not be
-- initialized again if ticks_candidates is empty
self.progress_bar.ticks = {}
end
end
if self.settings.all_at_once then
local info = {}
if self.settings.battery then
@ -345,7 +356,7 @@ end
function ReaderFooter:onPageUpdate(pageno)
self.pageno = pageno
self.pages = self.view.document.info.number_of_pages
self.pages = self.view.document:getPageCount()
self:updateFooterPage()
end
@ -361,6 +372,7 @@ function ReaderFooter:onUpdatePos()
self:updateFooter()
end
function ReaderFooter:applyFooterMode(mode)
-- three modes switcher for reader footer
-- 0 for footer off

@ -48,6 +48,7 @@ end
function CreDocument:engineInit()
if not self.engine_initilized then
require "libs/libkoreader-cre"
-- initialize cache
self:cacheInit()
@ -70,7 +71,6 @@ function CreDocument:engineInit()
end
function CreDocument:init()
require "libs/libkoreader-cre"
self:engineInit()
self.configurable:loadDefaults(self.options)

@ -70,9 +70,8 @@ function ProgressWidget:paintTo(bb, x, y)
my_size.h-2*(self.margin_v+self.bordersize), self.rectcolor)
if self.ticks then
for i=1, #self.ticks do
local page = self.ticks[i]
bb:paintRect(
x + (my_size.w-2*self.margin_h)*(page/self.last),
x + (my_size.w-2*self.margin_h)*(self.ticks[i]/self.last),
y + self.margin_v + self.bordersize,
self.tick_width,
my_size.h-2*(self.margin_v+self.bordersize),

@ -32,11 +32,13 @@ describe("Readerfooter module", function()
local readerui = ReaderUI:new{
document = DocumentRegistry:openDocument(sample_epub),
}
readerui.view.footer:updateFooter()
timeinfo = readerui.view.footer:getTimeInfo()
local footer = readerui.view.footer
footer:onPageUpdate(1)
footer:updateFooter()
timeinfo = footer:getTimeInfo()
-- stats has not been initialized here, so we get na TB and TC
assert.are.same('B:0% | '..timeinfo..' | 1 / 1 | => 0 | R:100% | TB: na | TC: na',
readerui.view.footer.progress_text.text)
assert.are.same('B:0% | '..timeinfo..' | 1 / 202 | => 0 | R:0% | TB: na | TC: na',
footer.progress_text.text)
end)
it("should setup footer for pdf without error", function()
@ -130,16 +132,31 @@ describe("Readerfooter module", function()
document = DocumentRegistry:openDocument(sample_epub),
}
local footer = readerui.view.footer
readerui.view.document.info.number_of_pages = 5000
footer:onPageUpdate(1)
assert.are.same(207, footer.progress_bar.width)
assert.are.same(373, footer.text_width)
assert.are.same(215, footer.progress_bar.width)
assert.are.same(365, footer.text_width)
footer:onPageUpdate(1000)
assert.are.same(151, footer.progress_bar.width)
assert.are.same(429, footer.text_width)
footer:onPageUpdate(100)
assert.are.same(183, footer.progress_bar.width)
assert.are.same(397, footer.text_width)
end)
it("should support chapter markers", function()
local sample_epub = "spec/front/unit/data/juliet.epub"
purgeDir(DocSettings:getSidecarDir(sample_epub))
os.remove(DocSettings:getHistoryPath(sample_epub))
local readerui = ReaderUI:new{
document = DocumentRegistry:openDocument(sample_epub),
}
local footer = readerui.view.footer
footer:onPageUpdate(1)
assert.are.same({
2, 4, 7, 8, 24, 31, 38, 45, 55, 56, 59, 71, 77, 92, 97, 100, 113,
121, 131, 134, 148, 156, 160, 163, 166, 175, 180, 182
}, footer.progress_bar.ticks)
assert.are.same(202, footer.progress_bar.last)
end)
it("should schedule/unschedule auto refresh time task", function()
local sample_epub = "spec/front/unit/data/juliet.epub"

Loading…
Cancel
Save