From b26d3e981534377f9ff8f9bddddaac99a366e871 Mon Sep 17 00:00:00 2001 From: zwim <36999612+zwim@users.noreply.github.com> Date: Wed, 1 Jun 2022 00:04:46 +0200 Subject: [PATCH] Autoturn: change to minutes and seconds (#9055) fix #9051 and add some internal improvements (default values) to the datetimewidget. --- frontend/util.lua | 70 +++++++++++++++--------------- plugins/autoturn.koplugin/main.lua | 38 +++++++++------- 2 files changed, 59 insertions(+), 49 deletions(-) diff --git a/frontend/util.lua b/frontend/util.lua index ccafc829e..7eb3364c4 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -153,11 +153,11 @@ end --- Converts seconds to a period of time string. ---- @int seconds number of seconds ----- @bool withoutSeconds if true 1h30', if false 1h30'10'' +---- @bool withoutSeconds if true 1h30', if false 1h30'10" ---- @bool hmsFormat, if true format 1h30m10s ---- @bool withDays, if true format 1d12h30m10s ----- @treturn string clock string in the form of 1h30'10'' or 1h30m10s -function util.secondsToHClock(seconds, withoutSeconds, hmsFormat, withDays) +---- @treturn string clock string in the form of 1h30'10" or 1h30m10s +function util.secondsToHClock(seconds, withoutSeconds, hmsFormat, withDays, compact) local SECONDS_SYMBOL = "\"" seconds = tonumber(seconds) if seconds == 0 then @@ -169,7 +169,7 @@ function util.secondsToHClock(seconds, withoutSeconds, hmsFormat, withDays) end else if hmsFormat then - return T(_("%1s"), "0") + return T(C_("Time", "%1s"), "0") else return "0" .. SECONDS_SYMBOL end @@ -177,24 +177,29 @@ function util.secondsToHClock(seconds, withoutSeconds, hmsFormat, withDays) elseif seconds < 60 then if withoutSeconds and seconds < 30 then if hmsFormat then - -- @translators This is the 'm' for minute, like in 30m30s. This is a duration. - return T(_("%1m"), "0") + return T(C_("Time", "%1m"), "0") else return "0'" end elseif withoutSeconds and seconds >= 30 then if hmsFormat then - -- @translators This is the 'm' for minute, like in 30m30s. This is a duration. - return T(_("%1m"), "1") + return T(C_("Time", "%1m"), "1") else return "1'" end else if hmsFormat then - -- @translators This is the 'm' for minute and 's' for seconds, like in 30m30s. This is a duration. - return T(_("%1m%2s"), "0", string.format("%02d", seconds)) + if compact then + return T(C_("Time", "%1s"), string.format("%d", seconds)) + else + return T(C_("Time", "%1m%2s"), "0", string.format("%02d", seconds)) + end else - return "0'" .. string.format("%02d", seconds) .. SECONDS_SYMBOL + if compact then + return string.format("%d", seconds) .. SECONDS_SYMBOL + else + return "0'" .. string.format("%02d", seconds) .. SECONDS_SYMBOL + end end end else @@ -202,21 +207,21 @@ function util.secondsToHClock(seconds, withoutSeconds, hmsFormat, withDays) if withoutSeconds then time_string = time_string .. ":" end - if hmsFormat then - -- @translators This is the 'h' for hour, like in 1h30m30s. This is a duration. - time_string = time_string:gsub(":", C_("Time", "h"), 1) - -- @translators This is the 'm' for minute, like in 1h30m30s. This is a duration. - time_string = time_string:gsub(":", C_("Time", "m"), 1) - time_string = time_string:gsub("^00" .. C_("Time", "h"), "") -- delete leading "00h" + time_string = time_string:gsub(":", C_("Time", "h"), 1) + time_string = time_string:gsub(":", C_("Time", "m"), 1) + time_string = time_string:gsub("^00" .. C_("Time", "h"), "") -- delete leading "00h" + time_string = time_string:gsub("^00" .. C_("Time", "m"), "") -- delete leading "00m" + if time_string:find("^0%d") then time_string = time_string:gsub("^0", "") -- delete leading "0" - -- @translators This is the 's' for second, like in 1h30m30s. This is a duration. + end + if withoutSeconds and time_string == "" then + time_string = "0" .. C_("Time", "m") + end + + if hmsFormat then return withoutSeconds and time_string or (time_string .. C_("Time", "s")) else - -- @translators This is the 'h' for hour, like in 1h30m30s. This is a duration. - time_string = time_string:gsub(":", C_("Time", "h"), 1) - time_string = time_string:gsub(":", "'", 1) - time_string = time_string:gsub("^00" .. C_("Time", "h"), "") -- delete leading "00h" - time_string = time_string:gsub("^0", "") -- delete leading "0" + time_string = time_string:gsub(C_("Time", "m"), "'") -- replace m with ' return withoutSeconds and time_string or (time_string .. SECONDS_SYMBOL) end end @@ -228,12 +233,13 @@ end ---- @bool withoutSeconds if true 1h30' or 1h30m, if false 1h30'10" or 1h30m10s ---- @bool hmsFormat, modern format only, if true format 1h30m or 1h30m10s ---- @bool withDays, if hours>=24 include days in clock string 1d12h10m10s +---- @bool compact, if set removes all leading zeros (incl. units if necessary) ---- @treturn string clock string in the specific format of 1h30', 1h30'10" resp. 1h30m, 1h30m10s -function util.secondsToClockDuration(format, seconds, withoutSeconds, hmsFormat, withDays) +function util.secondsToClockDuration(format, seconds, withoutSeconds, hmsFormat, withDays, compact) if format == "modern" then - return util.secondsToHClock(seconds, withoutSeconds, hmsFormat, withDays) + return util.secondsToHClock(seconds, withoutSeconds, hmsFormat, withDays, compact) else - -- Assume "classic" to give safe default + -- Assume "classic" to give safe default return util.secondsToClock(seconds, withoutSeconds, withDays) end end @@ -993,19 +999,15 @@ function util.getFriendlySize(size, right_align) size = tonumber(size) if not size or type(size) ~= "number" then return end if size > 1000*1000*1000 then - -- @translators This is an abbreviation for the gigabyte, a unit of computer memory or data storage capacity. - return T(_("%1 GB"), string.format(frac_format, size/1000/1000/1000)) + return T(C_("Data storage size", "%1 GB"), string.format(frac_format, size/1000/1000/1000)) end if size > 1000*1000 then - -- @translators This is an abbreviation for the megabyte, a unit of computer memory or data storage capacity. - return T(_("%1 MB"), string.format(frac_format, size/1000/1000)) + return T(C_("Data storage size", "%1 MB"), string.format(frac_format, size/1000/1000)) end if size > 1000 then - -- @translators This is an abbreviation for the kilobyte, a unit of computer memory or data storage capacity. - return T(_("%1 kB"), string.format(frac_format, size/1000)) + return T(C_("Data storage size", "%1 kB"), string.format(frac_format, size/1000)) else - -- @translators This is an abbreviation for the byte, a unit of computer memory or data storage capacity. - return T(_("%1 B"), string.format(deci_format, size)) + return T(C_("Data storage size", "%1 B"), string.format(deci_format, size)) end end diff --git a/plugins/autoturn.koplugin/main.lua b/plugins/autoturn.koplugin/main.lua index bbd39bd1f..cc8037e13 100644 --- a/plugins/autoturn.koplugin/main.lua +++ b/plugins/autoturn.koplugin/main.lua @@ -5,6 +5,7 @@ local UIManager = require("ui/uimanager") local WidgetContainer = require("ui/widget/container/widgetcontainer") local logger = require("logger") local time = require("ui/time") +local util = require("util") local _ = require("gettext") local T = require("ffi/util").template @@ -66,8 +67,8 @@ function AutoTurn:_start() local text if self.autoturn_distance == 1 then - text = T(_("Autoturn is now active and will automatically turn the page every %1 seconds."), - self.autoturn_sec) + local time_string = util.secondsToClockDuration("modern", self.autoturn_sec, false, true, true, true) + text = T(_("Autoturn is now active and will automatically turn the page every %1."), time_string) else text = T(_("Autoturn is now active and will automatically scroll %1 % of the page every %2 seconds."), self.autoturn_distance * 100, @@ -139,20 +140,27 @@ end function AutoTurn:addToMainMenu(menu_items) menu_items.autoturn = { sorting_hint = "navi", - text_func = function() return self:_enabled() and T(_("Autoturn: %1 s"), self.autoturn_sec) - or _("Autoturn") end, + text_func = function() + local time_string = util.secondsToClockDuration("modern", self.autoturn_sec, false, true, true, true) + return self:_enabled() and T(_("Autoturn: %1"), time_string) or _("Autoturn") + end, checked_func = function() return self:_enabled() end, callback = function(menu) - local SpinWidget = require("ui/widget/spinwidget") - local curr_items = G_reader_settings:readSetting("autoturn_timeout_seconds") or 30 - local autoturn_spin = SpinWidget:new { - value = curr_items, - value_min = 0, - value_max = 240, - value_hold_step = 5, + local DateTimeWidget = require("ui/widget/datetimewidget") + local autoturn_seconds = G_reader_settings:readSetting("autoturn_timeout_seconds", 30) + local autoturn_minutes = math.floor(autoturn_seconds / 60) + autoturn_seconds = autoturn_seconds % 60 + local autoturn_spin = DateTimeWidget:new { + title_text = _("Autoturn time"), + info_text = _("Enter time in minutes and seconds."), + min = autoturn_minutes, + min_max = 60 * 24, -- maximum one day + min_default = 0, + sec = autoturn_seconds, + sec_default = 30, + keep_shown_on_apply = true, ok_text = _("Set timeout"), cancel_text = _("Disable"), - title_text = _("Timeout in seconds"), cancel_callback = function() self.enabled = false G_reader_settings:makeFalse("autoturn_enabled") @@ -162,9 +170,9 @@ function AutoTurn:addToMainMenu(menu_items) self.onLeaveStandby = nil end, ok_always_enabled = true, - callback = function(autoturn_spin) - self.autoturn_sec = autoturn_spin.value - G_reader_settings:saveSetting("autoturn_timeout_seconds", autoturn_spin.value) + callback = function(t) + self.autoturn_sec = t.min * 60 + t.sec + G_reader_settings:saveSetting("autoturn_timeout_seconds", self.autoturn_sec) self.enabled = true G_reader_settings:makeTrue("autoturn_enabled") self:_unschedule()