From b466603358f8fa5a74551a9819fdd54ad124ef0c Mon Sep 17 00:00:00 2001 From: eunhhu Date: Sat, 26 Sep 2026 17:58:26 +0100 Subject: Give live surfaces a clock and run timers; add a stopwatch The terminal and the web page set the time events see to the wall clock and tick an app's timers on time: each round ticks what is due, draws, then waits for input, a frame or the next tick, and a tick is news to every open page at once. Scripts have no clock: time MS sets the time the next events see and tick TIMER ticks, so an app that uses time replays exactly. A module swap reschedules the timers. examples/stopwatch.aup runs on every surface; the checks drive it by script, on a pseudo-terminal by the clock, and on the web page. --- examples/stopwatch.aup | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/stopwatch.aup (limited to 'examples') diff --git a/examples/stopwatch.aup b/examples/stopwatch.aup new file mode 100644 index 0000000..cd075b0 --- /dev/null +++ b/examples/stopwatch.aup @@ -0,0 +1,43 @@ +# A stopwatch. Time is an input: a timer ticks while a surface runs the app, +# and each tick sees the time it happened (now); the views only show states. +use vega + +record Lap { + at = 0 +} + +state running = 0 +state started = 0 +state elapsed = 0 +state laps: list of Lap +state at: index of laps + +input toggle = key Space +input lap = key KeyL +input reset = key KeyR +input tick = every 100 ms + +fn two(n) = if n < 10 { "0{n}" } else { "{n}" } +fn tenths(ms) = "{floor(ms / 60000)}:{two(floor(ms / 1000) % 60)}.{floor(ms / 100) % 10}" + +on toggle.down { + if running { elapsed = now - started } + running = !running + if running { started = now - elapsed } +} +on tick { if running { elapsed = now - started } } +on lap.down { + laps.append(Lap { at: elapsed }) + at = laps.last +} +on reset.down { if !running { elapsed = 0 } } + +view title = "Stopwatch" +view time = tenths(elapsed) +view status = if running { "running" } else { "stopped" } + +view laps_list = each l in laps { + label = tenths(l.at) + tone = vega.when(l.position == laps.count - 1, vega.accent) +} +view laps_list_focus = at.position -- cgit v1.2.3