Files
8bitMyGoUI/account/optioncycler.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

71 lines
2.0 KiB
Go

package account
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type OptionCyclerOptions struct {
// Width is the value's room; zero fits the longest.
Width int
Disabled bool
}
type OptionCyclerResult struct {
Element ui.Element
Changed bool
}
// OptionCycler steps ◀ VALUE ▶ through options, wrapping around.
func OptionCycler(c *ui.Context, text string, options []string, selected *int, o OptionCyclerOptions) OptionCyclerResult {
k := pixel.K(c)
choice("OptionCycler", selected, len(options))
longest := 0
for _, s := range options {
longest = max(longest, pixel.Measure(s))
}
w := pixel.Or("OptionCycler Width", o.Width, longest)
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleStepper).Label(text).Disabled(o.Disabled)
})
n, old := len(options), *selected
if !base.IsDisabled() {
step := 0
if pixel.Shortcut(base, 0, ui.KeyLeft) {
step--
}
// Enter and Space step on; taken, clicks come from pointers.
right, enter, space := pixel.Shortcut(base, 0, ui.KeyRight), pixel.Shortcut(base, 0, ui.KeyEnter), pixel.Shortcut(base, 0, ui.KeySpace)
if right || enter || space {
step++
}
// A click on the left half steps back, else on.
if base.Clicked() {
if x, _, _ := base.PointerPosition(); x < shell.Bounds().W/2 {
step--
} else {
step++
}
}
*selected = ((*selected+step)%n + n) % n
}
base.Description(options[*selected])
value := options[*selected]
total := w + 32
shell.Size(k.D(total), k.D(pixel.LineHeight+4)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Text(2, 2, "◀", pixel.Accent, 1)
g.Text(16+(w-pixel.Measure(value))/2, 2, value, pixel.Ink, 1)
g.Text(total-2-pixel.Measure("▶"), 2, "▶", pixel.Accent, 1)
g.Finish(st)
})
res := OptionCyclerResult{Element: shell, Changed: *selected != old}
if res.Changed {
slog.Debug("8bit: option cycler", "label", text, "selected", *selected)
}
return res
}