Files
MujicaUI/input/range_slider.go
T
ZacharyZhang-NYandClaude Opus 5.5 bcce34cda1 Upgrade MyGo to v0.5.5
MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:

- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
  elements expire and Context methods are inert: read IsDisabled into a
  local during the build, keep a ui.Handle for Focus, use c.Services()
  for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
  content builds, so same-frame autofocus lost focus restore on close.
  It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
  c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
  split button tooltip no longer reorders its open menu; disabled
  menubar rows pass presses to the panel; Form defers blur-started
  async checks so the growing message no longer moves Submit away
  from the release; Combobox drops its disabled copy now that MyGo
  blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
  time.Sleep/time.Now and read element state inside the view. The web
  mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.

Snapshot goldens are darwin-only and were not re-verified here.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:37:54 -04:00

124 lines
3.8 KiB
Go

package input
import (
"log/slog"
"math"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// RangeSliderOptions configure a RangeSlider. Min and Max both zero mean 0 to 100.
type RangeSliderOptions struct {
Min, Max float64
// Step snaps both ends to Min plus multiples of it; zero is continuous.
Step float64
Marks []SliderMark
ShowValue bool
Format func(float64) string
Disabled bool
// Label names the range; the thumbs read "<Label> minimum/maximum, value".
Label string
}
// RangeSlider sets *low and *high, which never cross; a press moves the nearer thumb, each takes the keys.
func RangeSlider(c *ui.Context, low, high *float64, opts RangeSliderOptions) core.ChoiceResult {
g, h := SliderSetup(opts.Min, opts.Max, opts.Step, opts.Marks, opts.ShowValue, c)
format := sliderFormat(opts.Format, g)
size := core.FontSize(c, theme.CaptionSize)
k := core.Tokens(c)
*low, *high = g.snap(math.Min(*low, *high)), g.snap(math.Max(*low, *high))
oldLow, oldHigh := *low, *high
e := ui.Box(c).Height(h).MinWidth(120).Shrink(0).Role(ui.RoleGroup).Disabled(opts.Disabled).Cursor(ui.CursorPointer)
if opts.Label != "" {
e.Label(opts.Label)
}
drag := ui.Local(e, rangeSliderDrag{}, func() int { return -1 })
px, _, _ := e.PointerPosition()
r := e.Bounds()
at := g.snap(g.lo + float64((px-SliderThumb/2)/max(r.W-SliderThumb, 1))*(g.hi-g.lo))
var knobs [2]ui.Element
pressed := false
e.Children(func() {
for i, v := range []*float64{low, high} {
from, to := g.lo, *high
if i == 1 {
from, to = *low, g.hi
}
x := g.X(ui.Rect{W: r.W}, *v) - SliderThumb/2
var kb ui.Element
var next float64
var held bool
// Each thumb is a SliderBase, which reports the range and value to assistive technology.
ui.Box(c.Key(i)).Absolute().Left(x).Top(g.TrackY-SliderThumb/2).Size(SliderThumb, SliderThumb).Children(func() {
kb, next, held = SliderControl(c, *v, g, from, to)
})
kb.Size(SliderThumb, SliderThumb).FocusRing(false)
name := core.Msg(c, []string{"range_slider.min", "range_slider.max"}[i])
if opts.Label != "" {
name = opts.Label + " " + name
}
kb.Label(name)
if held {
pressed = true
*drag = i
} else if !opts.Disabled {
*v = next
}
knobs[i] = kb
}
})
if _, _, ok := e.Dragged(); ok {
pressed = true
}
if !pressed || opts.Disabled {
*drag = -1
} else {
if *drag < 0 {
*drag = 0
if math.Abs(at-*high) < math.Abs(at-*low) || at > *high {
*drag = 1
}
}
if *drag == 0 {
*low = min(at, *high)
} else {
*high = max(at, *low)
}
knobs[*drag].Focus()
}
changed := *low != oldLow || *high != oldHigh
if changed {
slog.Debug("mujica: range slider changed", "label", opts.Label, "low", *low, "high", *high)
}
lo, hi, which := *low, *high, *drag
hover := e.Hovered()
e.Draw(func(p *ui.Painter, r ui.Rect) {
x0, x1 := g.X(r, lo), g.X(r, hi)
SliderTrackPaint(p, r, g, k, opts.Marks, size, x0, x1)
for i, x := range []float32{x0, x1} {
hot := which == i || knobs[i].Hovered()
SliderThumbPaint(p, k, x, r.Y+g.TrackY, hot, knobs[i].FocusVisible())
if opts.ShowValue && (hot || hover || knobs[i].Focused()) {
rangeSliderBubble(p, k, x, r.Y+g.TrackY, format([]float64{lo, hi}[i]), i, size)
}
}
})
return core.NewChoiceResult(e, changed)
}
// rangeSliderDrag keys which thumb the pointer drags, -1 for none.
type rangeSliderDrag struct{}
// rangeSliderBubble keeps the two tips apart: the low one leans left, the high one right.
func rangeSliderBubble(p *ui.Painter, k theme.Tokens, x, y float32, s string, i int, size float32) {
w, _ := p.MeasureText(1000, ui.Span{Text: s, Size: size})
shift := (w+12)/2 - SliderThumb/2
if i == 0 {
shift = -shift
}
sliderBubblePaint(p, k, x+shift, y, s, size)
}