Files
8bitMyGoUI/input/radiogroup.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

63 lines
1.6 KiB
Go

package input
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type RadioGroupOptions struct {
// Row lays the radios out across.
Row bool
Disabled bool
Label string
}
type radioKey struct{}
type RadioGroupResult struct {
Element *ui.Element
Changed bool
}
// RadioGroup chooses *selected; one Tab stop, arrows choose.
func RadioGroup(c *ui.Context, labels []string, selected *int, o RadioGroupOptions) RadioGroupResult {
k := pixel.K(c)
var e *ui.Element
if o.Row {
e = ui.Row(c).Gap(k.D(10)).FocusGroup(ui.Horizontal)
} else {
e = ui.Column(c).Gap(k.D(2)).FocusGroup(ui.Vertical)
}
res := RadioGroupResult{Element: e}
last := ui.Local(e, radioKey{}, func() int { return -1 })
focus := -1
e.Role(ui.RoleRadioGroup).Label(o.Label).Children(func() {
for i, l := range labels {
was := *selected == i
on := was
shell, base, st := pixel.Shell(c, func() *ui.Element {
return ui.CheckboxBase(c, &on).Role(ui.RoleRadio).Label(l).Disabled(o.Disabled)
})
if base.Focused() {
focus = i
}
// Arrow moves show focus and choose; Tab entry does not.
moved := base.FocusVisible() && *last >= 0 && *last != i
if !was && (base.Changed() || moved) && !base.IsDisabled() {
*selected, res.Changed = i, true
}
shell.Size(k.D(12+pixel.Measure(l)), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
dot(k.Canvas(p, r), l, *selected == i, st)
})
}
})
*last = focus
if res.Changed {
slog.Debug("8bit: radio group", "selected", *selected)
}
return res
}