Files
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

76 lines
1.9 KiB
Go

package input
import (
"fmt"
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type RatingOptions struct {
// Max is the number of marks; zero means 5.
Max int
// Hearts draws hearts instead of stars.
Hearts bool
Disabled bool
}
type RatingResult struct {
Element ui.Element
Changed bool
}
type ratingKey struct{}
// Rating sets *value by click or arrows; reclicking clears.
func Rating(c *ui.Context, text string, value *int, o RatingOptions) RatingResult {
k := pixel.K(c)
n := pixel.Or("Rating Max", o.Max, 5)
full, empty := icon.Star, icon.StarEmpty
if o.Hearts {
full, empty = icon.Heart, icon.HeartEmpty
}
e := ui.Row(c).Gap(k.D(1)).FocusGroup(ui.Horizontal).Role(ui.RoleRadioGroup).Label(text)
last := ui.Local(e, ratingKey{}, func() int { return -1 })
focus := -1
res := RatingResult{Element: e}
e.Children(func() {
for i := 1; i <= n; i++ {
was := *value == i
on := was
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.CheckboxBase(c, &on).Role(ui.RoleRadio).Label(fmt.Sprintf("%d/%d", i, n)).Disabled(o.Disabled)
})
if base.Focused() {
focus = i
}
// Arrow moves show focus and choose; clicks toggle.
switch {
case base.IsDisabled():
case base.Changed() && was:
*value, res.Changed = 0, true
case base.Changed(), base.FocusVisible() && *last >= 0 && *last != i && !was:
*value, res.Changed = i, true
}
shell.Size(k.D(full.W+2), k.D(full.H+2)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
sp, ink := empty, pixel.Faint
if i <= *value {
sp, ink = full, pixel.Accent
}
g.Sprite(1, 1, sp, 0, ink, 1)
g.Finish(st)
})
}
})
*last = focus
e.Description(fmt.Sprintf("%d/%d", *value, n))
if res.Changed {
slog.Debug("8bit: rating", "label", text, "value", *value)
}
return res
}