Files
8bitMyGoUI/input/toggle.go
T
ZacharyZhang-NYandClaude Opus 5.5 03e1f91706 Upgrade MyGo to v0.5.5
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>
2026-10-17 01:31:08 -04:00

59 lines
1.3 KiB
Go

package input
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type ToggleOptions struct {
Size Size
Icon *pixel.Sprite
Disabled bool
// Label names it for assistive technology; zero uses text.
Label string
}
type ToggleResult struct {
Element ui.Element
Changed bool
}
// Toggle is a button that stays sunken while *on.
func Toggle(c *ui.Context, text string, on *bool, o ToggleOptions) ToggleResult {
k := pixel.K(c)
h, pad, top, sc := o.Size.metrics()
// The base turns before it learns it is off.
v := *on
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.ToggleBase(c, &v).Label(label(o.Label, text)).Disabled(o.Disabled)
})
changed := v != *on && !base.IsDisabled()
if changed {
*on = v
}
lead := 0
if o.Icon != nil {
lead = o.Icon.W
if text != "" {
lead += 3
}
}
shell.Size(k.D(pixel.Measure(text)*sc+lead+2*pad), k.D(h)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
ink, dy := face(g, Normal, st, *on)
if o.Icon != nil {
g.Sprite(pad, (h-o.Icon.H)/2+dy, o.Icon, 0, ink, 1)
}
g.Text(pad+lead, top+dy, text, ink, sc)
g.Finish(st)
})
res := ToggleResult{Element: shell, Changed: changed}
if res.Changed {
slog.Debug("8bit: toggle", "label", label(o.Label, text), "on", *on)
}
return res
}