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>
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package input
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
type ToggleGroupOptions struct {
|
|
// Single keeps at most one toggle on.
|
|
Single bool
|
|
Size Size
|
|
Disabled bool
|
|
Label string
|
|
}
|
|
|
|
type ToggleGroupResult struct {
|
|
Element ui.Element
|
|
Changed bool
|
|
}
|
|
|
|
// ToggleGroup joins toggles; on[i] holds each one's state.
|
|
func ToggleGroup(c *ui.Context, labels []string, on []bool, o ToggleGroupOptions) ToggleGroupResult {
|
|
if len(on) != len(labels) {
|
|
panic(fmt.Sprintf("input: ToggleGroup has %d labels and %d states", len(labels), len(on)))
|
|
}
|
|
k := pixel.K(c)
|
|
h, pad, top, sc := o.Size.metrics()
|
|
res := ToggleGroupResult{}
|
|
res.Element = ui.Row(c).Role(ui.RoleToolbar).Label(o.Label).FocusGroup(ui.Horizontal).Children(func() {
|
|
for i, l := range labels {
|
|
// The base turns before it learns it is off.
|
|
v := on[i]
|
|
shell, base, st := pixel.Shell(c, func() ui.Element { return ui.ToggleBase(c, &v).Label(l).Disabled(o.Disabled) })
|
|
if i > 0 {
|
|
shell.Margin(0, 0, 0, k.D(-1))
|
|
}
|
|
shell.Size(k.D(pixel.Measure(l)*sc+2*pad), k.D(h)).Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
ink, dy := face(g, Normal, st, on[i])
|
|
g.Text(pad, top+dy, l, ink, sc)
|
|
g.Finish(st)
|
|
})
|
|
if v != on[i] && !base.IsDisabled() {
|
|
on[i], res.Changed = v, true
|
|
if o.Single && on[i] {
|
|
for j := range on {
|
|
on[j] = j == i
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
if res.Changed {
|
|
slog.Debug("8bit: toggle group", "on", on)
|
|
}
|
|
return res
|
|
}
|