diff --git a/frontend/apps/reader/modules/readercoptlistener.lua b/frontend/apps/reader/modules/readercoptlistener.lua index 3b5c9e9e3..e0cb80490 100644 --- a/frontend/apps/reader/modules/readercoptlistener.lua +++ b/frontend/apps/reader/modules/readercoptlistener.lua @@ -12,6 +12,10 @@ 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 @@ -44,6 +48,7 @@ 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() @@ -84,7 +89,10 @@ end function ReaderCoptListener:updatePageInfoOverride(pageno) pageno = pageno or self.ui.view.footer.pageno - if not (self.document.configurable.status_line == 0 and self.view.view_mode == "page" and self.page_info_override) then + 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("") return end -- There are a few cases where we may not be updated on change, at least: @@ -126,7 +134,18 @@ function ReaderCoptListener:updatePageInfoOverride(pageno) end end - local page_info = "" + 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 if self.page_number == 1 or self.page_count == 1 then page_info = page_info .. page_pre if self.page_number == 1 then @@ -158,13 +177,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}" + batt_pre = "[\u{21AF}" -- ↯-symbol end -- Sum both batteries for the actual text batt_lvl = batt_lvl + aux_batt_lvl else if powerd:isCharging() then - batt_pre = "[\u{21AF}" + batt_pre = "[\u{21AF}" -- ↯-symbol end end batt_val = string.format("%2d%%", batt_lvl) @@ -300,15 +319,30 @@ 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 - if self.page_info_override then - self:updatePageInfoOverride() - else - self.document:setPageInfoOverride("") -- Don't forget to restore CRE default behaviour. - end + or (self.battery == 1 and self.battery_percent == 1) -- don't forget a sole battery + + self:updatePageInfoOverride() -- Have crengine redraw it (even if hidden by the menu at this time) self.ui.rolling:updateBatteryState() self:updateHeader() diff --git a/frontend/apps/reader/modules/readerfooter.lua b/frontend/apps/reader/modules/readerfooter.lua index cef53d893..68d9d344b 100644 --- a/frontend/apps/reader/modules/readerfooter.lua +++ b/frontend/apps/reader/modules/readerfooter.lua @@ -341,10 +341,10 @@ local footerTextGeneratorMap = { prefix .. " ", left) end, mem_usage = function(footer) - local symbol_type = footer.settings.item_prefix - local prefix = symbol_prefix[symbol_type].mem_usage local statm = io.open("/proc/self/statm", "r") if statm then + local symbol_type = footer.settings.item_prefix + local prefix = symbol_prefix[symbol_type].mem_usage local dummy, rss = statm:read("*number", "*number") statm:close() -- we got the nb of 4Kb-pages used, that we convert to MiB @@ -516,6 +516,8 @@ ReaderFooter.default_settings = { function ReaderFooter:init() self.settings = G_reader_settings:readSetting("footer", self.default_settings) + self.additional_footer_content = {} -- place, where additional header content can be inserted. + -- Remove items not supported by the current device if not Device:hasFastWifiStatusQuery() then MODE.wifi_status = nil @@ -1981,6 +1983,19 @@ function ReaderFooter:genAlignmentMenuItems(value) } end +function ReaderFooter:addAdditionalFooterContent(content_func) + table.insert(self.additional_footer_content, content_func) +end + +function ReaderFooter:removeAdditionalFooterContent(content_func) + for i, v in ipairs(self.additional_footer_content) do + if v == content_func then + table.remove(self.additional_footer_content, i) + return true + end + end +end + -- this method will be updated at runtime based on user setting function ReaderFooter:genFooterText() end @@ -2153,6 +2168,13 @@ function ReaderFooter:_updateFooterText(force_repaint, full_repaint) return end local text = self:genFooterText() + for dummy, v in ipairs(self.additional_footer_content) do + local value = v() + if value and value ~= "" then + text = value .. " " .. self:get_separator_symbol() .. " " .. text + end + end + if not text then text = "" end self.footer_text:setText(text) if self.settings.disable_progress_bar then diff --git a/frontend/ui/widget/datetimewidget.lua b/frontend/ui/widget/datetimewidget.lua index b4622714d..ab16a9fe2 100644 --- a/frontend/ui/widget/datetimewidget.lua +++ b/frontend/ui/widget/datetimewidget.lua @@ -60,6 +60,7 @@ local Geom = require("ui/geometry") local GestureRange = require("ui/gesturerange") local Font = require("ui/font") local HorizontalGroup = require("ui/widget/horizontalgroup") +local HorizontalSpan = require("ui/widget/horizontalspan") local NumberPickerWidget = require("ui/widget/numberpickerwidget") local Size = require("ui/size") local TextWidget = require("ui/widget/textwidget") @@ -342,7 +343,9 @@ function DateTimeWidget:createLayout() self.sec = self.sec_widget:getValue() self:callback(self) end - self:onClose() + if not self.keep_shown_on_apply then + self:onClose() + end end, }, }) @@ -369,17 +372,18 @@ function DateTimeWidget:createLayout() w = self.width, h = math.floor(date_group:getSize().h * 1.2), }, - date_group + date_group, }, CenterContainer:new{ dimen = Geom:new{ w = self.width, h = ok_cancel_buttons:getSize().h, }, - ok_cancel_buttons - } + ok_cancel_buttons, + }, } } + self[1] = WidgetContainer:new{ align = "center", dimen = Geom:new{ @@ -396,6 +400,21 @@ function DateTimeWidget:createLayout() self:refocusWidget() end +function DateTimeWidget:addWidget(widget) + table.insert(self.layout, #self.layout, {widget}) + widget = HorizontalGroup:new{ + align = "center", + HorizontalSpan:new{ width = Size.span.horizontal_default }, + widget, + } + table.insert(self.date_frame[1], #self.date_frame[1], widget) +end + +function DateTimeWidget:getAddedWidgetAvailableWidth() + return self.date_frame[1][1].width - 2*Size.padding.default +end + + function DateTimeWidget:update(year, month, day, hour, min, sec) self.year_widget.value = year self.year_widget:update() diff --git a/plugins/readtimer.koplugin/main.lua b/plugins/readtimer.koplugin/main.lua index 0f85cd58d..7bde0ec2f 100644 --- a/plugins/readtimer.koplugin/main.lua +++ b/plugins/readtimer.koplugin/main.lua @@ -1,10 +1,13 @@ -local DateTimeWidget = require("ui/widget/datetimewidget") -local InfoMessage = require("ui/widget/infomessage") +local CheckButton = require("ui/widget/checkbutton") local ConfirmBox = require("ui/widget/confirmbox") +local DateTimeWidget = require("ui/widget/datetimewidget") +local Event = require("ui/event") +local InfoMessage = require("ui/widget/infomessage") local UIManager = require("ui/uimanager") local WidgetContainer = require("ui/widget/container/widgetcontainer") local logger = require("logger") local datetime = require("datetime") +local time = require("ui/time") local _ = require("gettext") local T = require("ffi/util").template @@ -15,6 +18,9 @@ local ReadTimer = WidgetContainer:extend{ } function ReadTimer:init() + self.timer_symbol = "\u{23F2}" -- ⏲ timer symbol + self.timer_letter = "T" + self.alarm_callback = function() -- Don't do anything if we were unscheduled if self.time == 0 then return end @@ -46,18 +52,78 @@ function ReadTimer:init() }) end end + + self.additional_header_content_func = function() + if self:scheduled() then + local hours, minutes, dummy = self:remainingTime(1) + local timer_info = string.format("%02d:%02d", hours, minutes) + return self.timer_symbol .. timer_info + end + return + end + + self.additional_footer_content_func = function() + if self:scheduled() then + local item_prefix = self.ui.view.footer.settings.item_prefix + local hours, minutes, dummy = self:remainingTime(1) + local timer_info = string.format("%02d:%02d", hours, minutes) + + if item_prefix == "icons" then + return self.timer_symbol .. " " .. timer_info + elseif item_prefix == "compact_items" then + return self.timer_symbol .. timer_info + else + return self.timer_letter .. ": " .. timer_info + end + end + return + end + + self.show_value_in_header = G_reader_settings:readSetting("readtimer_show_value_in_header") + self.show_value_in_footer = G_reader_settings:readSetting("readtimer_show_value_in_footer") + + if self.show_value_in_header then + self:addAdditionalHeaderContent() + else + self:removeAdditionalHeaderContent() + end + + if self.show_value_in_footer then + self:addAdditionalFooterContent() + else + self:removeAdditionalFooterContent() + end + self.ui.menu:registerToMainMenu(self) end +function ReadTimer:update_status_bars(seconds) + if self.show_value_in_header then + UIManager:broadcastEvent(Event:new("UpdateHeader")) + end + if self.show_value_in_footer then + UIManager:broadcastEvent(Event:new("UpdateFooter", true)) + end + -- if seconds schedule 1ms later + if seconds and seconds >= 0 then + UIManager:scheduleIn(math.max(math.floor(seconds)%60, 0.001), self.update_status_bars, self) + elseif seconds and seconds < 0 and self:scheduled() then + UIManager:scheduleIn(math.max(math.floor(self:remaining())%60, 0.001), self.update_status_bars, self) + else + UIManager:scheduleIn(60, self.update_status_bars, self) + end +end + function ReadTimer:scheduled() return self.time ~= 0 end function ReadTimer:remaining() if self:scheduled() then - local td = os.difftime(self.time, os.time()) - if td > 0 then - return td + -- Resolution: time.now() subsecond, os.time() two seconds + local remaining_s = time.to_s(self.time - time.now()) + if remaining_s > 0 then + return remaining_s else return 0 end @@ -66,9 +132,21 @@ function ReadTimer:remaining() end end -function ReadTimer:remainingTime() +-- can round +function ReadTimer:remainingTime(round) if self:scheduled() then local remainder = self:remaining() + if round then + if round < 0 then -- round down + remainder = remainder - 59 + elseif round == 0 then + remainder = remainder + 30 + else -- round up + remainder = remainder + 59 + end + remainder = math.floor(remainder * (1/60)) * 60 + end + local hours = math.floor(remainder * (1/3600)) local minutes = math.floor(remainder % 3600 * (1/60)) local seconds = math.floor(remainder % 60) @@ -76,16 +154,74 @@ function ReadTimer:remainingTime() end end +function ReadTimer:addAdditionalHeaderContent() + self.ui.crelistener:addAdditionalHeaderContent(self.additional_header_content_func) + self:update_status_bars(-1) +end +function ReadTimer:addAdditionalFooterContent() + self.ui.view.footer:addAdditionalFooterContent(self.additional_footer_content_func) + self:update_status_bars(-1) +end + +function ReadTimer:removeAdditionalHeaderContent() + self.ui.crelistener:removeAdditionalHeaderContent(self.additional_header_content_func) + self:update_status_bars(-1) + UIManager:broadcastEvent(Event:new("UpdateHeader")) +end +function ReadTimer:removeAdditionalFooterContent() + self.ui.view.footer:removeAdditionalFooterContent(self.additional_footer_content_func) + self:update_status_bars(-1) + UIManager:broadcastEvent(Event:new("UpdateFooter", true)) +end + function ReadTimer:unschedule() if self:scheduled() then UIManager:unschedule(self.alarm_callback) self.time = 0 end + UIManager:unschedule(self.update_status_bars, self) end function ReadTimer:rescheduleIn(seconds) - self.time = os.time() + seconds + -- Resolution: time.now() subsecond, os.time() two seconds + self.time = time.now() + time.s(seconds) UIManager:scheduleIn(seconds, self.alarm_callback) + if self.show_value_in_header or self.show_value_in_footer then + self:update_status_bars(seconds) + end +end + +function ReadTimer:addCheckboxes(widget) + local checkbox_header = CheckButton:new{ + text = _("Show timer in alt status bar"), + checked = self.show_value_in_header, + parent = widget, + callback = function() + self.show_value_in_header = not self.show_value_in_header + G_reader_settings:saveSetting("readtimer_show_value_in_header", self.show_value_in_header) + if self.show_value_in_header then + self:addAdditionalHeaderContent() + else + self:removeAdditionalHeaderContent() + end + end, + } + local checkbox_footer = CheckButton:new{ + text = _("Show timer in status bar"), + checked = self.show_value_in_footer, + parent = widget, + callback = function() + self.show_value_in_footer = not self.show_value_in_footer + G_reader_settings:saveSetting("readtimer_show_value_in_footer", self.show_value_in_footer) + if self.show_value_in_footer then + self:addAdditionalFooterContent() + else + self:removeAdditionalFooterContent() + end + end, + } + widget:addWidget(checkbox_header) + widget:addWidget(checkbox_footer) end function ReadTimer:addToMainMenu(menu_items) @@ -116,13 +252,12 @@ function ReadTimer:addToMainMenu(menu_items) ok_text = _("Set alarm"), title_text = _("New alarm"), info_text = _("Enter a time in hours and minutes."), - callback = function(time) + callback = function(alarm_time) self.last_interval_time = 0 - touchmenu_instance:closeMenu() self:unschedule() local then_t = now_t - then_t.hour = time.hour - then_t.min = time.min + then_t.hour = alarm_time.hour + then_t.min = alarm_time.min then_t.sec = 0 local seconds = os.difftime(os.time(then_t), os.time()) if seconds > 0 then @@ -131,10 +266,11 @@ function ReadTimer:addToMainMenu(menu_items) UIManager:show(InfoMessage:new{ -- @translators %1:%2 is a clock time (HH:MM), %3 is a duration text = T(_("Timer set for %1:%2.\n\nThat's %3 from now."), - string.format("%02d", time.hour), string.format("%02d", time.min), + string.format("%02d", alarm_time.hour), string.format("%02d", alarm_time.min), datetime.secondsToClockDuration(user_duration_format, seconds, false)), timeout = 5, }) + if touchmenu_instance then touchmenu_instance:updateItems() end else UIManager:show(InfoMessage:new{ text = _("Timer could not be set. The selected time is in the past."), @@ -143,6 +279,7 @@ function ReadTimer:addToMainMenu(menu_items) end end } + self:addCheckboxes(time_widget) UIManager:show(time_widget) end, }, @@ -166,10 +303,9 @@ function ReadTimer:addToMainMenu(menu_items) ok_text = _("Set timer"), title_text = _("Set reader timer"), info_text = _("Enter a time in hours and minutes."), - callback = function(time) - touchmenu_instance:closeMenu() + callback = function(timer_time) self:unschedule() - local seconds = time.hour * 3600 + time.min * 60 + local seconds = timer_time.hour * 3600 + timer_time.min * 60 if seconds > 0 then self.last_interval_time = seconds self:rescheduleIn(seconds) @@ -180,11 +316,14 @@ function ReadTimer:addToMainMenu(menu_items) datetime.secondsToClockDuration(user_duration_format, seconds, true)), timeout = 5, }) - remain_time = {time.hour, time.min} + remain_time = {timer_time.hour, timer_time.min} G_reader_settings:saveSetting("reader_timer_remain_time", remain_time) + if touchmenu_instance then touchmenu_instance:updateItems() end end end } + + self:addCheckboxes(time_widget) UIManager:show(time_widget) end, },