Files
8bitMyGoUI/datetime/stopwatch.go
T
ZacharyZhang-NY b4b4d67b92 Audit keyboard, focus, veils, motion and clocks across all 500
A cross-cutting pass, each fix with a test that failed first.

Veils: MyGo still hands a key to a base that turned off this frame,
so every element key now goes through pixel.Shortcut, which an
archtest enforces. Input handlers record and the build applies them
only while enabled; the editor turns away a veil-frame letter or
copy; value bases get a copy and keep only enabled turns. Popups
give a held focus back to their opener when the veil lifts.

Motion: S5 is enforced at the clock. Every Tick, Since, Step,
Toward and Shot runs at 4-15 fps; blinks keep 1 Hz on 4 fps steps,
fast animations step at 15, and drawn time is quantized to frames.

Clocks stepping back: Since holds at zero without overflow, and
k.Elapsed moves a stored start to now, so nothing goes negative,
runs backward or indexes out of range.

Also: logs count typed text instead of holding it, gallery names
use glyphs the font has, removable rows are keyed, components take
their own Options (archtest), and sweeps over all 508 demos check
veiled keys, clicks, wheels, the clock stepping back and names.
2026-10-12 16:03:04 -04:00

83 lines
2.4 KiB
Go

package datetime
import (
"fmt"
"log/slog"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type StopwatchOptions struct {
Disabled bool
}
type StopwatchResult struct {
Element *ui.Element
Started, Stopped, Lapped, Reset bool
}
type watchKey struct{}
// watch is time banked, since when it runs, and laps.
type watch struct {
banked time.Duration
since time.Time
laps []time.Duration
}
// tenths prints d as MM:SS.T.
func tenths(d time.Duration) string {
return fmt.Sprintf("%s.%d", clock(d), int(d/(100*time.Millisecond))%10)
}
// Stopwatch times with START, STOP, LAP, RESET; laps below.
func Stopwatch(c *ui.Context, text string, o StopwatchOptions) StopwatchResult {
k := pixel.K(c)
col := ui.Column(c).Gap(k.D(4)).Role(ui.RoleGroup).Label(text)
w := ui.Local(col, watchKey{}, func() watch { return watch{} })
now := k.Now()
running := !w.since.IsZero()
elapsed := w.banked
if running {
elapsed += k.Elapsed(&w.since)
k.Tick(c, 10)
}
res := StopwatchResult{Element: col}
col.Children(func() {
s := tenths(elapsed)
ui.Box(c).Size(k.D(pixel.Measure(s)*2), k.D(pixel.LineHeight*2)).Role(ui.RoleText).Label(s).Draw(func(p *ui.Painter, r ui.Rect) {
k.Canvas(p, r).Text(0, 0, s, pixel.Ink, 2)
})
ui.Row(c).Gap(k.D(4)).Children(func() {
toggle := k.Msg("start")
if running {
toggle = k.Msg("stop")
}
if input.Button(c, toggle, input.ButtonOptions{Size: input.Small, Variant: input.Primary, Disabled: o.Disabled}).Clicked {
if running {
w.banked, w.since, res.Stopped = elapsed, time.Time{}, true
} else {
w.since, res.Started = now, true
}
}
if input.Button(c, k.Msg("lap"), input.ButtonOptions{Size: input.Small, Disabled: o.Disabled || !running}).Clicked {
w.laps, res.Lapped = append(w.laps, elapsed), true
}
if input.Button(c, k.Msg("reset"), input.ButtonOptions{Size: input.Small, Disabled: o.Disabled || running || elapsed == 0}).Clicked {
*w, res.Reset = watch{}, true
}
})
for i := len(w.laps) - 1; i >= max(0, len(w.laps)-5); i-- {
pixel.Text(c, fmt.Sprintf("%s %d %s", k.Msg("lap"), i+1, tenths(w.laps[i])), pixel.TextOptions{Slot: pixel.Faint})
}
})
if res.Started || res.Stopped || res.Lapped || res.Reset {
slog.Debug("8bit: stopwatch", "label", text, "elapsed", elapsed, "laps", len(w.laps))
}
return res
}