Skip to content

Commit

Permalink
Add author option for alt status bar (koreader/crengine#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
trash-pandy committed Jul 21, 2024
1 parent ad1286f commit 1a48af1
Showing 1 changed file with 22 additions and 45 deletions.
67 changes: 22 additions & 45 deletions frontend/apps/reader/modules/readercoptlistener.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ local ReaderCoptListener = EventListener:extend{}

local CRE_HEADER_DEFAULT_SIZE = 20

function ReaderCoptListener:init()
self.additional_header_content = {} -- place, where additional header content can be inserted.
end

function ReaderCoptListener:onReadSettings(config)
local view_mode_name = self.document.configurable.view_mode == 0 and "page" or "scroll"
-- Let crengine know of the view mode before rendering, as it can
Expand All @@ -25,8 +21,8 @@ function ReaderCoptListener:onReadSettings(config)
-- ReaderView is the holder of the view_mode state
self.view.view_mode = view_mode_name

-- crengine top status bar can only show author and title together
self.title = G_reader_settings:readSetting("cre_header_title", 1)
self.author = G_reader_settings:readSetting("cre_header_author", 1)
self.clock = G_reader_settings:readSetting("cre_header_clock", 1)
self.header_auto_refresh = G_reader_settings:readSetting("cre_header_auto_refresh", 1)
self.page_number = G_reader_settings:readSetting("cre_header_page_number", 1)
Expand All @@ -37,6 +33,7 @@ function ReaderCoptListener:onReadSettings(config)
self.chapter_marks = G_reader_settings:readSetting("cre_header_chapter_marks", 1)

self.document._document:setIntProperty("window.status.title", self.title)
self.document._document:setIntProperty("window.status.author", self.author)
self.document._document:setIntProperty("window.status.clock", self.clock)
self.document._document:setIntProperty("window.status.pos.page.number", self.page_number)
self.document._document:setIntProperty("window.status.pos.page.count", self.page_count)
Expand All @@ -48,7 +45,6 @@ function ReaderCoptListener:onReadSettings(config)
-- We will build the top status bar page info string ourselves,
-- if we have to display any chunk of it
self.page_info_override = self.page_number == 1 or self.page_count == 1 or self.reading_percent == 1
or (self.battery == 1 and self.battery_percent == 1) -- don't forget a sole battery
self.document:setPageInfoOverride("") -- an empty string lets crengine display its own page info

self:onTimeFormatChanged()
Expand Down Expand Up @@ -89,10 +85,7 @@ end
function ReaderCoptListener:updatePageInfoOverride(pageno)
pageno = pageno or self.ui.view.footer.pageno

if self.document.configurable.status_line ~= 0 or self.view.view_mode ~= "page"
or not self.page_info_override or not next(self.additional_header_content) then

self.document:setPageInfoOverride("")
if not (self.document.configurable.status_line == 0 and self.view.view_mode == "page" and self.page_info_override) then
return
end
-- There are a few cases where we may not be updated on change, at least:
Expand Down Expand Up @@ -134,18 +127,7 @@ function ReaderCoptListener:updatePageInfoOverride(pageno)
end
end

local additional_content = ""
for dummy, v in ipairs(self.additional_header_content) do
local value = v()
if value and value ~= "" then
additional_content = additional_content .. value
if self.page_number == 1 or self.page_count == 1 then
additional_content = additional_content .. " " -- double spaces as crengine's own drawing
end
end
end

local page_info = additional_content
local page_info = ""
if self.page_number == 1 or self.page_count == 1 then
page_info = page_info .. page_pre
if self.page_number == 1 then
Expand Down Expand Up @@ -177,13 +159,13 @@ function ReaderCoptListener:updatePageInfoOverride(pageno)
if Device:hasAuxBattery() and powerd:isAuxBatteryConnected() then
local aux_batt_lvl = powerd:getAuxCapacity()
if powerd:isAuxCharging() then
batt_pre = "[\u{21AF}" -- ↯-symbol
batt_pre = "[\u{21AF}"
end
-- Sum both batteries for the actual text
batt_lvl = batt_lvl + aux_batt_lvl
else
if powerd:isCharging() then
batt_pre = "[\u{21AF}" -- ↯-symbol
batt_pre = "[\u{21AF}"
end
end
batt_val = string.format("%2d%%", batt_lvl)
Expand Down Expand Up @@ -319,30 +301,15 @@ end
ReaderCoptListener.onCloseDocument = ReaderCoptListener.unscheduleHeaderRefresh
ReaderCoptListener.onSuspend = ReaderCoptListener.unscheduleHeaderRefresh

function ReaderCoptListener:addAdditionalHeaderContent(content_func)
table.insert(self.additional_header_content, content_func)
end

function ReaderCoptListener:removeAdditionalHeaderContent(content_func)
for i, v in ipairs(self.additional_header_content) do
if v == content_func then
table.remove(self.additional_header_content, i)
return true
end
end
end

function ReaderCoptListener:setAndSave(setting, property, value)
self.document._document:setIntProperty(property, value)
G_reader_settings:saveSetting(setting, value)
self:onUpdateHeader()
end

function ReaderCoptListener:onUpdateHeader()
self.page_info_override = self.page_number == 1 or self.page_count == 1 or self.reading_percent == 1
or (self.battery == 1 and self.battery_percent == 1) -- don't forget a sole battery

self:updatePageInfoOverride()
if self.page_info_override then
self:updatePageInfoOverride()
else
self.document:setPageInfoOverride("") -- Don't forget to restore CRE default behaviour.
end
-- Have crengine redraw it (even if hidden by the menu at this time)
self.ui.rolling:updateBatteryState()
self:updateHeader()
Expand Down Expand Up @@ -385,7 +352,7 @@ function ReaderCoptListener:getAltStatusBarMenu()
separator = true
},
{
text = _("Book author and title"),
text = _("Book title"),
checked_func = function()
return self.title == 1
end,
Expand All @@ -394,6 +361,16 @@ function ReaderCoptListener:getAltStatusBarMenu()
self:setAndSave("cre_header_title", "window.status.title", self.title)
end,
},
{
text = _("Book author"),
checked_func = function()
return self.author == 1
end,
callback = function()
self.author = self.author == 0 and 1 or 0
self:setAndSave("cre_header_author", "window.status.author", self.author)
end,
},
{
text = _("Current time"),
checked_func = function()
Expand Down

0 comments on commit 1a48af1

Please sign in to comment.