Add the ability to launch read timer with a gesture (#14061)
Some checks failed
macos / macOS 13 x86-64 🔨15.2 🎯10.15 (push) Has been cancelled
macos / macOS 14 ARM64 🔨15.4 🎯11.0 (push) Has been cancelled

Closes #14014.
This commit is contained in:
Volterxien
2025-07-18 06:12:41 -04:00
committed by GitHub
parent 70039c9f04
commit de789615ac
2 changed files with 119 additions and 77 deletions

View File

@@ -67,7 +67,7 @@ function Calibre:onDispatcherRegisterActions()
Dispatcher:registerAction("calibre_browse_authors", { category="none", event="CalibreBrowseBy", arg="authors", title=_("Browse all calibre authors"), general=true,})
Dispatcher:registerAction("calibre_browse_titles", { category="none", event="CalibreBrowseBy", arg="title", title=_("Browse all calibre titles"), general=true, separator=true,})
Dispatcher:registerAction("calibre_start_connection", { category="none", event="StartWirelessConnection", title=_("Calibre wireless connect"), general=true,})
Dispatcher:registerAction("calibre_close_connection", { category="none", event="CloseWirelessConnection", title=_("Calibre wireless disconnect"), general=true,})
Dispatcher:registerAction("calibre_close_connection", { category="none", event="CloseWirelessConnection", title=_("Calibre wireless disconnect"), general=true, separator=true,})
end
function Calibre:init()

View File

@@ -1,6 +1,7 @@
local CheckButton = require("ui/widget/checkbutton")
local ConfirmBox = require("ui/widget/confirmbox")
local DateTimeWidget = require("ui/widget/datetimewidget")
local Dispatcher = require("dispatcher")
local Event = require("ui/event")
local InfoMessage = require("ui/widget/infomessage")
local UIManager = require("ui/uimanager")
@@ -17,6 +18,15 @@ local ReadTimer = WidgetContainer:extend{
last_interval_time = 0,
}
function ReadTimer:onDispatcherRegisterActions()
Dispatcher:registerAction("show_alarm",
{category="none", event="ShowAlarm", title=_("Set reader alarm"), general=true})
Dispatcher:registerAction("show_timer",
{category="none", event="ShowTimer", title=_("Set reader timer"), general=true})
Dispatcher:registerAction("stop_timer",
{category="none", event="StopTimer", title=_("Stop reader timer"), general=true, separator=true})
end
function ReadTimer:init()
self.timer_symbol = "\u{23F2}" -- ⏲ timer symbol
self.timer_letter = "T"
@@ -91,6 +101,7 @@ function ReadTimer:init()
end
self.ui.menu:registerToMainMenu(self)
self:onDispatcherRegisterActions()
end
function ReadTimer:update_status_bars(seconds)
@@ -248,85 +259,14 @@ function ReadTimer:addToMainMenu(menu_items)
text = _("Set time"),
keep_menu_open = true,
callback = function(touchmenu_instance)
local now_t = os.date("*t")
local curr_hour = now_t.hour
local curr_min = now_t.min
local time_widget = DateTimeWidget:new{
hour = curr_hour,
min = curr_min,
ok_text = _("Set alarm"),
title_text = _("New alarm"),
info_text = _("Enter a time in hours and minutes."),
callback = function(alarm_time)
self.last_interval_time = 0
self:unschedule()
local then_t = now_t
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
then_t.day = then_t.day + 1
seconds = os.difftime(os.time(then_t), os.time())
end
self:rescheduleIn(seconds)
local user_duration_format = G_reader_settings:readSetting("duration_format")
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", 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
end
}
self:addCheckboxes(time_widget)
UIManager:show(time_widget)
self:onShowAlarm(touchmenu_instance)
end,
},
{
text = _("Set interval"),
keep_menu_open = true,
callback = function(touchmenu_instance)
local remain_time = {}
local remain_hours, remain_minutes = self:remainingTime()
if not remain_hours and not remain_minutes then
remain_time = G_reader_settings:readSetting("reader_timer_remain_time")
if remain_time then
remain_hours = remain_time[1]
remain_minutes = remain_time[2]
end
end
local time_widget = DateTimeWidget:new{
hour = remain_hours or 0,
min = remain_minutes or 0,
hour_max = 17,
ok_text = _("Set timer"),
title_text = _("Set reader timer"),
info_text = _("Enter a time in hours and minutes."),
callback = function(timer_time)
self:unschedule()
local seconds = timer_time.hour * 3600 + timer_time.min * 60
if seconds > 0 then
self.last_interval_time = seconds
self:rescheduleIn(seconds)
local user_duration_format = G_reader_settings:readSetting("duration_format")
UIManager:show(InfoMessage:new{
-- @translators This is a duration
text = T(_("Timer will expire in %1."),
datetime.secondsToClockDuration(user_duration_format, seconds, true)),
timeout = 5,
})
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)
self:onShowTimer(touchmenu_instance)
end,
},
{
@@ -336,9 +276,7 @@ function ReadTimer:addToMainMenu(menu_items)
return self:scheduled()
end,
callback = function(touchmenu_instance)
self.last_interval_time = 0
self:unschedule()
touchmenu_instance:updateItems()
self:onStopTimer(touchmenu_instance)
end,
},
},
@@ -365,4 +303,108 @@ function ReadTimer:onResume()
end
end
function ReadTimer:onShowAlarm(touchmenu_instance)
local now_t = os.date("*t")
local curr_hour = now_t.hour
local curr_min = now_t.min
local time_widget = DateTimeWidget:new{
hour = curr_hour,
min = curr_min,
ok_text = _("Set alarm"),
title_text = _("New alarm"),
info_text = _("Enter a time in hours and minutes."),
callback = function(alarm_time)
self:setAlarm(alarm_time, now_t, touchmenu_instance)
end
}
self:addCheckboxes(time_widget)
UIManager:show(time_widget)
return true
end
function ReadTimer:setAlarm(alarm_time, then_t, touchmenu_instance)
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
then_t.day = then_t.day + 1
seconds = os.difftime(os.time(then_t), os.time())
end
self.last_interval_time = 0
self:unschedule()
self:rescheduleIn(seconds)
local user_duration_format = G_reader_settings:readSetting("duration_format")
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", 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
end
function ReadTimer:onShowTimer(touchmenu_instance)
local remain_hours, remain_minutes = self:remainingTime()
if not remain_hours and not remain_minutes then
local remain_time = G_reader_settings:readSetting("reader_timer_remain_time")
if remain_time then
remain_hours = remain_time[1]
remain_minutes = remain_time[2]
end
end
local time_widget = DateTimeWidget:new{
hour = remain_hours or 0,
min = remain_minutes or 0,
hour_max = 17,
ok_text = _("Set timer"),
title_text = _("Set reader timer"),
info_text = _("Enter a time in hours and minutes."),
callback = function(timer_time)
self:setInterval(timer_time, touchmenu_instance)
end
}
self:addCheckboxes(time_widget)
UIManager:show(time_widget)
return true
end
function ReadTimer:setInterval(timer_time, touchmenu_instance)
local seconds = timer_time.hour * 3600 + timer_time.min * 60
if seconds > 0 then
self:unschedule()
self.last_interval_time = seconds
self:rescheduleIn(seconds)
local user_duration_format = G_reader_settings:readSetting("duration_format")
UIManager:show(InfoMessage:new{
-- @translators This is a duration
text = T(_("Timer will expire in %1."),
datetime.secondsToClockDuration(user_duration_format, seconds, true)),
timeout = 5,
})
if touchmenu_instance then touchmenu_instance:updateItems() end
-- Save settings
local remain_time = {timer_time.hour, timer_time.min}
G_reader_settings:saveSetting("reader_timer_remain_time", remain_time)
end
end
function ReadTimer:onStopTimer(touchmenu_instance)
if self:scheduled() then
self.last_interval_time = 0
self:unschedule()
if touchmenu_instance then
touchmenu_instance:updateItems()
else
UIManager:show(InfoMessage:new{text=_("Timer stopped")})
end
end
return true
end
return ReadTimer