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

58 lines
1.4 KiB
Go

package input
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type SwitchOptions struct {
Disabled bool
// Label names it for assistive technology; zero uses text.
Label string
}
type SwitchResult struct {
Element ui.Element
Changed bool
}
// Switch is a lever on an OFF/ON plate.
func Switch(c *ui.Context, text string, on *bool, o SwitchOptions) SwitchResult {
k := pixel.K(c)
// The base turns before it learns it is off.
v := *on
shell, base, st := pixel.Shell(c, func() ui.Element { return ui.SwitchBase(c, &v).Label(label(o.Label, text)).Disabled(o.Disabled) })
changed := v != *on && !base.IsDisabled()
if changed {
*on = v
}
shell.Size(k.D(32+pixel.Measure(text)), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Frame(0, 1, 27, 14, pixel.Raised, pixel.Shade)
g.Micro(3, 2, "OFF", pixel.Faint, 1)
g.Micro(17, 2, "ON", pixel.Faint, 1)
x, lx, lit := 6, 3, "OFF"
if *on {
x, lx, lit = 19, 17, "ON"
}
g.Micro(lx, 2, lit, pixel.Ink, 1)
g.Rect(x, 8, 3, 5, pixel.Edge)
g.Rect(x-1, 8, 5, 3, pixel.Edge)
g.Rect(x, 9, 3, 1, pixel.Lit)
ink := pixel.Ink
if st.Disabled {
ink = pixel.Faint
}
g.Text(32, 0, text, ink, 1)
g.Finish(st)
})
res := SwitchResult{Element: shell, Changed: changed}
if res.Changed {
slog.Debug("8bit: switch", "label", text, "on", *on)
}
return res
}