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

72 lines
2.3 KiB
Go

package input
import (
"fmt"
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type RangeSliderOptions struct {
Min, Max float64
Width int
Disabled bool
}
type RangeSliderResult = SliderResult
// RangeSlider sets *low and *high with two uncrossing knobs.
func RangeSlider(c *ui.Context, text string, low, high *float64, o RangeSliderOptions) RangeSliderResult {
k := pixel.K(c)
lo, hi := bounds(o.Min, o.Max)
w := pixel.Or("RangeSlider Width", o.Width, 96)
was := [2]float64{*low, *high}
e := ui.Box(c).Size(k.D(w), k.D(12)).Role(ui.RoleGroup).Label(text)
e.Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
a, b := knobAt(*low, lo, hi, g.W-6), knobAt(*high, lo, hi, g.W-6)
g.Frame(0, 4, g.W, 4, pixel.Sunken, pixel.Well)
g.Rect(a+3, 5, b-a, 2, pixel.Accent)
}).Children(func() {
knob(c, k, e, low, rail{lo: lo, hi: *high, label: text + " low", disabled: o.Disabled}, lo, hi, w)
knob(c, k, e, high, rail{lo: *low, hi: hi, label: text + " high", disabled: o.Disabled}, lo, hi, w)
})
if *low > *high {
panic(fmt.Sprintf("input: RangeSlider low %v above high %v", *low, *high))
}
changed := was != [2]float64{*low, *high}
if changed {
slog.Debug("8bit: range slider", "label", text, "low", *low, "high", *high)
}
return SliderResult{Element: e, Changed: changed}
}
// knob is one draggable end, fenced by r.lo and r.hi.
func knob(c *ui.Context, k pixel.Kit, track ui.Element, v *float64, r rail, lo, hi float64, w int) {
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleSlider).Label(r.label).Description(fmtValue(*v)).Disabled(r.disabled)
})
set := func(x float64) { *v = max(r.lo, min(r.hi, x)) }
if !base.IsDisabled() {
if _, _, held := base.Dragged(); held {
px, _, _ := base.PointerPosition()
t := track.Bounds()
set(lo + float64((base.Bounds().X+px-t.X)/max(1, t.W))*(hi-lo))
}
step := (hi - lo) / 20
if pixel.Shortcut(base, 0, ui.KeyLeft) || pixel.Shortcut(base, 0, ui.KeyDown) {
set(*v - step)
}
if pixel.Shortcut(base, 0, ui.KeyRight) || pixel.Shortcut(base, 0, ui.KeyUp) {
set(*v + step)
}
}
shell.Absolute().Left(k.D(knobAt(*v, lo, hi, w-6))).Top(k.D(1)).Size(k.D(6), k.D(10)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Frame(0, 0, 6, 10, pixel.Raised, pixel.Face)
g.Finish(st)
})
}