Elements are now pass-scoped value handles: tests that read bounds after a frame bind a ui.Handle. pixel tells frames apart by the build pass's root, since the Tester clock no longer moves on its own. Fix the regressions the upgrade brought: - Clipboard reads in input handlers go through c.Services(). - MyGo notes an overlay's opener after building it, so AutoFocus inside lost it; pixel.AutoFocus focuses through a Handle, and archtest bans the raw call. Dialogs hand the focus back again. - Elements around a press stay hovered: Tooltip tells a press-given focus by focus arriving under the pointer. - The web host moves the Tester clock with real time, at most 500 ms a call, and gives components the real clock. The gallery's source index also accepts CRLF checkouts. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package input
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
type RadioOptions struct {
|
|
Disabled bool
|
|
}
|
|
|
|
type RadioResult struct {
|
|
Element ui.Element
|
|
Changed bool
|
|
}
|
|
|
|
// Radio sets *selected to value when chosen.
|
|
func Radio[T comparable](c *ui.Context, text string, value T, selected *T, o RadioOptions) RadioResult {
|
|
k := pixel.K(c)
|
|
was := *selected == value
|
|
on := was
|
|
shell, base, st := pixel.Shell(c, func() ui.Element {
|
|
return ui.CheckboxBase(c, &on).Role(ui.RoleRadio).Label(text).Disabled(o.Disabled)
|
|
})
|
|
res := RadioResult{Element: shell}
|
|
if base.Changed() && !was && !base.IsDisabled() {
|
|
*selected, res.Changed = value, true
|
|
slog.Debug("8bit: radio", "label", text)
|
|
}
|
|
chosen := *selected == value
|
|
shell.Size(k.D(12+pixel.Measure(text)), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
|
|
dot(k.Canvas(p, r), text, chosen, st)
|
|
})
|
|
return res
|
|
}
|
|
|
|
// dot draws the radio, filled when on, and the label.
|
|
func dot(g pixel.Canvas, text string, on bool, st pixel.State) {
|
|
y := (pixel.LineHeight - 8) / 2
|
|
fill := pixel.Well
|
|
if st.Hover {
|
|
fill = pixel.Lit
|
|
}
|
|
g.Rect(2, y, 4, 8, pixel.Edge)
|
|
g.Rect(0, y+2, 8, 4, pixel.Edge)
|
|
g.Rect(1, y+1, 6, 6, pixel.Edge)
|
|
g.Rect(2, y+1, 4, 6, fill)
|
|
g.Rect(1, y+2, 6, 4, fill)
|
|
if on {
|
|
g.Rect(3, y+2, 2, 4, pixel.Accent)
|
|
g.Rect(2, y+3, 4, 2, pixel.Accent)
|
|
}
|
|
ink := pixel.Ink
|
|
if st.Disabled {
|
|
ink = pixel.Faint
|
|
}
|
|
g.Text(12, 0, text, ink, 1)
|
|
g.Finish(st)
|
|
}
|