Files
8bitMyGoUI/input/knob.go
T
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

56 lines
1.5 KiB
Go

package input
import (
"math"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type KnobOptions struct {
// Min and Max are zero for 0 to 1.
Min, Max float64
// Step snaps the value; zero means none.
Step float64
Disabled bool
}
type KnobResult = SliderResult
type knobKey struct{}
// Knob turns through 16 angles by vertical drag or arrows.
func Knob(c *ui.Context, text string, value *float64, o KnobOptions) KnobResult {
k := pixel.K(c)
lo, hi := bounds(o.Min, o.Max)
r := rail{lo: lo, hi: hi, step: o.Step}
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleSlider).Label(text).Description(fmtValue(*value)).Disabled(o.Disabled)
})
old, set := *value, r.snap(value)
// raw is the unrounded value a drag carries.
raw := ui.Local(base, knobKey{}, func() float64 { return *value })
if !base.IsDisabled() {
if _, dy, held := base.Dragged(); held {
*raw = max(lo, min(hi, *raw-float64(dy/k.Dot())*(hi-lo)/64))
set(*raw)
} else {
*raw = *value
}
r.keys(base, value, set)
}
shell.Size(k.D(24), k.D(24)).Draw(func(p *ui.Painter, rc ui.Rect) {
g := k.Canvas(p, rc)
g.Disc(12, 12, 11, pixel.Edge)
g.Disc(12, 12, 10, pixel.Shade)
g.Disc(11, 11, 9, pixel.Face)
a := (-135 + 18*math.Round((*value-lo)/(hi-lo)*15)) * math.Pi / 180
x, y := 12+int(math.Round(7*math.Sin(a))), 12-int(math.Round(7*math.Cos(a)))
g.Line(12, 12, x, y, pixel.Accent)
g.Dot(x, y, pixel.Ink)
g.Finish(st)
})
return report(shell, *value != old, "knob", text, *value)
}