Files
8bitMyGoUI/media/recordbutton.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

81 lines
1.7 KiB
Go

package media
import (
"log/slog"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type RecordButtonOptions struct {
Disabled bool
}
type RecordButtonResult struct {
Element *ui.Element
Changed bool
}
type takeKey struct{}
// take is when recording began and the last take's length.
type take struct {
since time.Time
took time.Duration
}
var (
recDot = pixel.MustSprite(`
..rrr..
.rrrrr.
rrrrrrr
rrrrrrr
rrrrrrr
.rrrrr.
..rrr..
`)
recOff = pixel.MustSprite(`
..sss..
.s...s.
s.....s
s.....s
s.....s
.s...s.
..sss..
`)
)
// RecordButton toggles *rec; the dot blinks, time runs.
func RecordButton(c *ui.Context, text string, rec *bool, o RecordButtonOptions) RecordButtonResult {
k := pixel.K(c)
now := k.Now()
row := ui.Row(c)
tk := ui.Local(row, takeKey{}, func() take { return take{} })
// Follow *rec, whoever flips it; a click builds again.
switch {
case *rec && tk.since.IsZero():
tk.since, tk.took = now, 0
case !*rec && !tk.since.IsZero():
tk.since, tk.took = time.Time{}, k.Elapsed(&tk.since)
}
took, dot := tk.took, recDot
if *rec {
took = k.Elapsed(&tk.since)
k.At(c, tk.since.Add(took.Truncate(time.Second)+time.Second))
if !k.Reduce() && k.Tick(c, 4)%4 >= 2 {
dot = recOff
}
}
res := RecordButtonResult{Element: row}
row.Children(func() {
res.Changed = input.Toggle(c, k.Msg("rec")+" "+mmss(took), rec, input.ToggleOptions{Icon: dot, Label: text + ", " + mmss(took), Disabled: o.Disabled}).Changed
})
if res.Changed {
slog.Debug("8bit: record button", "label", text, "rec", *rec, "time", mmss(took))
}
return res
}