Appearance
A desktop window
A small timer that runs as a desktop app: one component, opened in its own window by weft run.
toml
# weft.toml
component = "Timer"Timer.weft
weft
import { floor } from "std/math"
import { now, setInterval, clearInterval } from "std/time"
let mut running = false
let mut elapsed = 0
let mut started = 0
let mut laps: number[] = []
fn toggle() {
if running {
elapsed += now().epochMs - started
} else {
started = now().epochMs
}
running = !running
}
fn lap() {
laps.push(elapsed + (running ? now().epochMs - started : 0))
}
fn show(ms: number): string {
let s = floor(ms / 1000)
return `${floor(s / 60)}:${`${s % 60}`.padStart(2, "0")}.${floor(ms % 1000 / 100)}`
}
let mut tick = 0
effect {
let id = setInterval(() => {
if running { tick = now().epochMs - started }
}, 100)
return () => clearInterval(id)
}
---
<Box class={`flex h-full flex-col items-center justify-center gap-6 p-8 ${env.colorScheme == "dark" ? "bg-zinc-950 text-zinc-50" : "bg-white text-zinc-950"}`}>
<Text class="font-mono text-5xl font-semibold tabular-nums">{show(elapsed + (running ? tick : 0))}</Text>
<Box class="flex gap-2">
<Pressable class="rounded-md bg-zinc-900 px-4 py-2 text-white" onpress={toggle}>
<Text>{running ? "Stop" : "Start"}</Text>
</Pressable>
<Pressable class="rounded-md border px-4 py-2" onpress={lap}><Text>Lap</Text></Pressable>
</Box>
@for (t, i) of laps {
<Text class="text-sm opacity-70">Lap {i + 1}: {show(t)}</Text>
}
</Box>sh
weft run timer # opens the windowcomponentinweft.tomlmakes the project open that component in a window, with no routes and no server.env.colorSchemefollows the system's light or dark mode, andenv.widthandenv.heightthe window's size; markup that reads them updates when they change.- The window redraws only while something changes: here, ten times a second while the timer runs, and never while it's stopped.
- The same component works on a page: put it in
components/and use<Timer />.
An app with routes/ opens in a window with --window: weft dev hello --window. More: The desktop app.