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.6 KiB
Go

package display
import (
"fmt"
"github.com/egoist/mygo/ui"
"rsc.io/qr"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type QRCodeOptions struct {
// Scale is dots per module; zero means 2.
Scale int
}
type qrKey struct{}
type qrCache struct {
text string
code *qr.Code
}
// QRCode encodes s at level M, with a quiet zone.
func QRCode(c *ui.Context, s string, o QRCodeOptions) ui.Element {
k := pixel.K(c)
sc := pixel.Or("QRCode Scale", o.Scale, 2)
e := ui.Box(c)
qc := ui.Local(e, qrKey{}, func() qrCache { return qrCache{} })
if qc.code == nil || qc.text != s {
code, err := qr.Encode(s, qr.M)
if err != nil {
panic(fmt.Sprintf("display: QRCode %q: %v", s, err))
}
*qc = qrCache{s, code}
}
code := qc.code
n := (code.Size + 8) * sc
dark, light := extremes(k.Palette())
return e.Size(k.D(n), k.D(n)).Role(ui.RoleImage).Label(s).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Rect(0, 0, n, n, light)
for y := range code.Size {
for x := 0; x < code.Size; {
if !code.Black(x, y) {
x++
continue
}
end := x + 1
for end < code.Size && code.Black(end, y) {
end++
}
g.Rect((4+x)*sc, (4+y)*sc, (end-x)*sc, sc, dark)
x = end
}
}
})
}
// extremes are the darkest and lightest slots, for scanners.
func extremes(pal *pixel.Palette) (dark, light pixel.Slot) {
lum := func(s pixel.Slot) int {
c := pal.Colors[s]
return 299*int(c.R) + 587*int(c.G) + 114*int(c.B)
}
for s := range pixel.Slot(pixel.Slots) {
if lum(s) < lum(dark) {
dark = s
}
if lum(s) > lum(light) {
light = s
}
}
return dark, light
}