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>
75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package input
|
||
|
||
import (
|
||
"log/slog"
|
||
|
||
"github.com/egoist/mygo/ui"
|
||
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
||
)
|
||
|
||
var (
|
||
check = pixel.MustSprite(`
|
||
.....#
|
||
....##
|
||
#..##.
|
||
####..
|
||
.##...
|
||
`)
|
||
dash = pixel.MustSprite(`
|
||
####
|
||
`)
|
||
)
|
||
|
||
type CheckboxOptions struct {
|
||
Disabled bool
|
||
}
|
||
|
||
type CheckboxResult struct {
|
||
Element ui.Element
|
||
Changed bool
|
||
}
|
||
|
||
// Checkbox is a labeled 8×8 box with a hand-placed check.
|
||
func Checkbox(c *ui.Context, text string, checked *bool, o CheckboxOptions) CheckboxResult {
|
||
k := pixel.K(c)
|
||
// The base turns before it learns it is off.
|
||
on := *checked
|
||
shell, base, st := pixel.Shell(c, func() ui.Element { return ui.CheckboxBase(c, &on).Label(text).Disabled(o.Disabled) })
|
||
changed := on != *checked && !base.IsDisabled()
|
||
if changed {
|
||
*checked = on
|
||
}
|
||
mark := check
|
||
if !*checked {
|
||
mark = nil
|
||
}
|
||
shell.Size(k.D(12+pixel.Measure(text)), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
|
||
box(k.Canvas(p, r), text, mark, st)
|
||
})
|
||
res := CheckboxResult{Element: shell, Changed: changed}
|
||
if res.Changed {
|
||
slog.Debug("8bit: checkbox", "label", text, "checked", *checked)
|
||
}
|
||
return res
|
||
}
|
||
|
||
// box draws the 8×8 box, its mark and the label.
|
||
func box(g pixel.Canvas, text string, mark *pixel.Sprite, st pixel.State) {
|
||
y := (pixel.LineHeight - 8) / 2
|
||
fill := pixel.Well
|
||
if st.Hover {
|
||
fill = pixel.Lit
|
||
}
|
||
g.Frame(0, y, 8, 8, pixel.Sunken, fill)
|
||
if mark != nil {
|
||
g.Sprite((8-mark.W)/2, y+(8-mark.H)/2, mark, 0, pixel.Accent, 1)
|
||
}
|
||
ink := pixel.Ink
|
||
if st.Disabled {
|
||
ink = pixel.Faint
|
||
}
|
||
g.Text(12, 0, text, ink, 1)
|
||
g.Finish(st)
|
||
}
|