-- Clock: a stopwatch with laps, and a timer. Both carry on while you -- look at the other. The timer rings with the window closed too: it books -- a wake with the shell, which starts Clock for a moment to show a toast. local c8 = require "c8" local ID = "org.cont8nuum.Clock" local win local now = c8.now local function hms(t, tenths) t = math.max(0, t) local h, m, s = t // 3600, t % 3600 // 60, t % 60 local text = h > 0 and string.format("%d:%02d:%02d", h, m, s // 1) or string.format("%02d:%02d", m, s // 1) if tenths then text = text .. string.format(".%d", (s * 10) // 1 % 10) end return text end -- The stopwatch local sw = { running = false, base = 0, since = 0, laps = {} } local function sw_time() return sw.base + (sw.running and now() - sw.since or 0) end local function stopwatch() local face = c8.label { text = hms(0, true), style = "header" } local laps local start, lap local function refresh() face:set(hms(sw_time(), true)) start:set(sw.running and "Stop" or (sw_time() > 0 and "Go on" or "Start")) lap:set(sw.running and "Lap" or "Reset") lap.disabled = not sw.running and sw_time() == 0 end start = c8.button { label = "Start", primary = true, width = 120, on_click = function() if sw.running then sw.base, sw.running = sw_time(), false else sw.since, sw.running = now(), true end refresh() end } lap = c8.button { label = "Lap", width = 120, on_click = function() if sw.running then table.insert(sw.laps, 1, sw_time()) else sw.base, sw.laps = 0, {} end laps:count(#sw.laps) refresh() end } laps = c8.list { count = #sw.laps, item = function(i) local n = #sw.laps - i + 1 local split = sw.laps[i] - (sw.laps[i + 1] or 0) return { title = "Lap " .. n .. " " .. hms(split, true), subtitle = hms(sw.laps[i], true) } end } local ticker = c8.every(0.1, function() if sw.running then face:set(hms(sw_time(), true)) end end) local page = c8.column { spacing = 20, padding = { 0, 20, 0, 0 }, face, c8.row { spacing = 12, start, lap }, c8.scroll { laps, expand = true, fill = true } } page.on_destroy = function() ticker:cancel() end refresh() return page end -- The timer -- What the timer keeps: its length, and while it runs, when it ends -- (os.time), so a Clock started again (or for the wake) knows local saved = c8.load("clock") or {} local tm = { length = saved.length or 300, running = false, left = nil, ends = 0, alarm = nil } local function keep() c8.save("clock", { length = tm.length, ends = tm.running and os.time() + math.ceil(tm.ends - now()) or nil }) end local function tm_left() if tm.running then return tm.ends - now() end return tm.left or tm.length end local function ring() tm.running, tm.left = false, nil keep() c8.wake.cancel("timer") c8.tile.update(ID, { template = "text", heading = "Timer", text1 = "Done at " .. os.date("%H:%M") }) if win and win.open then win:present() win:dialog { title = "Time's up", message = "The " .. hms(tm.length) .. " timer is done.", buttons = { "OK" } } end end -- Rings in the app while it runs; the wake is for when it doesn't local function arm(refresh) tm.alarm = c8.after(tm_left(), function() ring() refresh() end) c8.wake.at("timer", os.time() + math.ceil(tm_left())) keep() end local function timer() local face = c8.label { text = hms(tm_left()), style = "header" } local start, reset local mins = c8.slider { header = "Minutes", min = 1, max = 120, step = 1, value = math.max(1, tm.length // 60), digits = 0, fill = true, on_change = function(_, v) tm.length = v * 60 keep() if not tm.running then tm.left = nil face:set(hms(tm_left())) end end } local function refresh() face:set(hms(tm_left())) start:set(tm.running and "Pause" or (tm.left and "Go on" or "Start")) reset.disabled = not tm.running and not tm.left mins.disabled = tm.running or tm.left ~= nil end local function stop() if tm.alarm then tm.alarm:cancel() tm.alarm = nil end c8.wake.cancel("timer") end start = c8.button { label = "Start", primary = true, width = 120, on_click = function() if tm.running then tm.left, tm.running = tm_left(), false stop() keep() else tm.ends, tm.running = now() + tm_left(), true arm(refresh) end refresh() end } reset = c8.button { label = "Reset", width = 120, on_click = function() stop() tm.running, tm.left = false, nil keep() refresh() end } local ticker = c8.every(0.25, function() if tm.running then face:set(hms(tm_left())) end end) local quick = c8.row { spacing = 8, wrap = true } for _, m in ipairs({ 1, 3, 5, 10, 25, 60 }) do quick:add(c8.button { label = m .. " min", on_click = function() if not tm.running and not tm.left then mins:set(m) tm.length = m * 60 keep() refresh() end end }) end -- A timer still running from before the window was closed if saved.ends and not tm.running and not tm.alarm then local left = saved.ends - os.time() if left > 0 then tm.ends, tm.running = now() + left, true tm.alarm = c8.after(left, function() ring() refresh() end) end saved.ends = nil end local page = c8.column { spacing = 20, padding = { 0, 20, 0, 0 }, fill = true, face, c8.row { spacing = 12, start, reset }, mins, quick } page.on_destroy = function() ticker:cancel() end refresh() return page end c8.app { id = ID, open = function() if not (win and win.open) then local tabs = c8.pivot { expand = true, fill = true, tabs = { { title = "Stopwatch", widget = stopwatch() }, { title = "Timer", widget = timer() }, } } -- Back to a timer still running from before if tm.running then tabs:set(2) end local body = c8.column { padding = { 20, 24, 20, 20 }, fill = true, tabs } win = c8.window { title = "Clock", width = 640, height = 620, body } end win:present() end, -- The timer's end, booked when it started. With Clock open the timer -- in the app rings (a moment sooner); started for this, a toast does wake = function(tag) if tag ~= "timer" or (win and win.open) then return end c8.notify { title = "Time's up", body = "The " .. hms(tm.length) .. " timer is done." } c8.tile.update(ID, { template = "text", heading = "Timer", text1 = "Done at " .. os.date("%H:%M") }) c8.save("clock", { length = tm.length }) end, }