Files
8bitMyGoUI/chart/spectrumbars.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

84 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package chart
import (
"fmt"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type SpectrumBarsOptions struct {
// Segments is each bar's height in blocks; zero means 10.
Segments int
}
type peaksKey struct{}
// peaks are the falling dots, when built last, whether falling.
type peaks struct {
at []float64
last time.Time
moving bool
}
// SpectrumBars lights segmented bars; peak dots fall behind them.
func SpectrumBars(c *ui.Context, text string, levels []float64, o SpectrumBarsOptions) ui.Element {
k := pixel.K(c)
n := pixel.Or("SpectrumBars Segments", o.Segments, 10)
for _, l := range levels {
if !(l >= 0 && l <= 1) {
panic(fmt.Sprintf("chart: SpectrumBars level %v outside 0–1", l))
}
}
e := ui.Box(c).Size(k.D(len(levels)*6), k.D(n*3)).Role(ui.RoleImage).Label(text).Description(nums(levels))
pk := ui.Local(e, peaksKey{}, func() peaks { return peaks{} })
if len(pk.at) != len(levels) {
pk.at = append([]float64(nil), levels...)
}
// Peaks fall a bar a second, stepped; reduced, they rest.
now := k.Now().Truncate(time.Second / 15)
fall := max(0, now.Sub(pk.last).Seconds())
switch {
case pk.last.IsZero() || k.Reduce():
fall = 1
case !pk.moving:
// Resting peaks start falling now, not at the last build.
fall = 0
}
pk.last, pk.moving = now, false
for i, l := range levels {
pk.at[i] = max(l, pk.at[i]-fall)
pk.moving = pk.moving || pk.at[i] > l
}
if pk.moving {
k.Tick(c, 15)
}
shown := append([]float64(nil), pk.at...)
return e.Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
for i, l := range levels {
lit := int(l*float64(n) + 0.5)
for s := range n {
y := (n - 1 - s) * 3
ink := pixel.Good
switch {
case s >= n*8/10:
ink = pixel.Bad
case s >= n*6/10:
ink = pixel.Warn
}
if s < lit {
g.Rect(i*6, y, 5, 2, ink)
} else {
g.Checker(i*6, y, 5, 2, pixel.Shade)
}
}
if top := int(shown[i]*float64(n) + 0.5); top > 0 {
g.Rect(i*6, (n-top)*3, 5, 1, pixel.Ink)
}
}
})
}