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>
91 lines
2.7 KiB
Go
91 lines
2.7 KiB
Go
package chart
|
|
|
|
import (
|
|
"image/color"
|
|
"math"
|
|
"testing"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
|
|
)
|
|
|
|
func TestChartCrosshairSnapsAndFollowsKeys(t *testing.T) {
|
|
h := &chartHarness{build: func(c *ui.Context) ChartResult { return partsChart(c, nil, true) }}
|
|
tt := ui.NewTester(h.view, 520, 420)
|
|
b := h.box(tt)
|
|
plot := ChartInset(ui.Rect{X: b.X, Y: b.Y, W: b.W, H: 240}, 52, 26)
|
|
// Point 1 is ten DIPs from the pointer.
|
|
px, py := plot.X+plot.W/2, plot.Y+plot.H*0.2
|
|
tt.Move(px+10, py-6)
|
|
if h.res.Point != 1 || chartReadout(tt) != "x 5 · y 80" {
|
|
t.Fatalf("snap: point %d %q", h.res.Point, chartReadout(tt))
|
|
}
|
|
if !crosshairAt(tt, int(px), int(plot.Y+plot.H-2)) {
|
|
t.Fatal("vertical line does not pass through the snapped point")
|
|
}
|
|
tt.Move(px+60, py+60)
|
|
if h.res.Point != -1 {
|
|
t.Fatalf("snapped from too far: %d", h.res.Point)
|
|
}
|
|
tabTo(t, tt, "Parts")
|
|
tt.Key(0, ui.KeyEnd)
|
|
if !crosshairAt(tt, int(plot.X+plot.W*0.8), int(plot.Y+plot.H-2)) {
|
|
t.Fatal("keyboard point has no crosshair")
|
|
}
|
|
}
|
|
|
|
// crosshairAt checks a crosshair pixel.
|
|
func crosshairAt(tt *ui.Tester, x, y int) bool {
|
|
want := toRGBA(theme.Light().TextMuted)
|
|
for dx := -1; dx <= 1; dx++ {
|
|
if color.RGBAModel.Convert(tt.Image().At(x+dx, y)).(color.RGBA) == want {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func TestChartCrosshairBad(t *testing.T) {
|
|
defer func() {
|
|
if recover() == nil {
|
|
t.Fatal("no panic without axes")
|
|
}
|
|
}()
|
|
ui.NewTester(func(c *ui.Context) { core.Use(c, core.Settings{}); ChartCrosshair(c, ChartCrosshairOptions{}) }, 100, 100)
|
|
}
|
|
|
|
func TestChartCrosshairSnapshot(t *testing.T) {
|
|
snapshot(t, "chart_crosshair", 420, 240, func(c *ui.Context) {
|
|
x := ChartAxis(c, ChartAxisOptions{Min: 0, Max: 10})
|
|
y := ChartAxis(c, ChartAxisOptions{Side: SideLeft, Min: 0, Max: 100})
|
|
hair := ChartCrosshair(c, ChartCrosshairOptions{X: &x, Y: &y})
|
|
ui.Box(c).Fill().Draw(func(p *ui.Painter, r ui.Rect) {
|
|
plot := ui.Rect{X: r.X + 48, Y: r.Y + 16, W: r.W - 64, H: r.H - 48}
|
|
x.Draw(p, plot)
|
|
y.Draw(p, plot)
|
|
hair.Draw(p, plot, x.Map(plot, 6.5), y.Map(plot, 42))
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestChartCrosshairDisablesFocusedReadout(t *testing.T) {
|
|
h := &chartHarness{build: func(c *ui.Context) ChartResult { return partsChart(c, nil, true) }}
|
|
tt := ui.NewTester(h.view, 520, 420)
|
|
tabTo(t, tt, "Parts")
|
|
tt.Key(0, ui.KeyRight)
|
|
h.disabled = true
|
|
tt.Frame()
|
|
if h.res.Point != -1 {
|
|
t.Fatalf("disabled readout %d", h.res.Point)
|
|
}
|
|
}
|
|
|
|
func TestChartCrosshairFiniteReach(t *testing.T) {
|
|
charts2MustPanic(t, func(c *ui.Context) {
|
|
a := ChartAxis(c, ChartAxisOptions{Max: 1})
|
|
ChartCrosshair(c, ChartCrosshairOptions{X: &a, Reach: float32(math.NaN())})
|
|
})
|
|
}
|