aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authoreunhhu <[email protected]>2026-09-26 17:58:26 +0100
committereunhhu <[email protected]>2026-09-26 17:58:26 +0100
commitb466603358f8fa5a74551a9819fdd54ad124ef0c (patch)
tree6f34265f30455589201222521c1a452274a0601d /examples
parent15c4616e19d997c64a983e39501289c7d9f50d0a (diff)
downloadvega-b466603358f8fa5a74551a9819fdd54ad124ef0c.tar.gz
vega-b466603358f8fa5a74551a9819fdd54ad124ef0c.zip
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.
Diffstat (limited to 'examples')
-rw-r--r--examples/stopwatch.aup43
1 files changed, 43 insertions, 0 deletions
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