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>
106 lines
2.7 KiB
Go
106 lines
2.7 KiB
Go
package feedback
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
|
|
)
|
|
|
|
// motionFrames builds view and returns its tester and a counter of built frames.
|
|
func motionFrames(view func(c *ui.Context), w, h int) (*ui.Tester, *int) {
|
|
n := new(int)
|
|
tt := ui.NewTester(func(c *ui.Context) {
|
|
*n++
|
|
core.Use(c, core.Settings{})
|
|
view(c)
|
|
}, w, h)
|
|
return tt, n
|
|
}
|
|
|
|
// motionBusy reports whether one more frame keeps asking for frames.
|
|
func motionBusy(tt *ui.Tester, n *int) bool {
|
|
before := *n
|
|
tt.Frame()
|
|
return *n-before > 2
|
|
}
|
|
|
|
// motionCount counts texts and names equal to s.
|
|
func motionCount(tt *ui.Tester, s string) int {
|
|
n := 0
|
|
for _, x := range tt.Texts() {
|
|
if x == s {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
var loaderCases = map[string]func(c *ui.Context, o LoaderOptions) ui.Element{
|
|
"dots": DotsLoader, "bars": BarsLoader, "pulse": PulseLoader, "orbit": OrbitLoader, "wave": WaveLoader,
|
|
}
|
|
|
|
func testLoaderBehavior(t *testing.T, loader func(*ui.Context, LoaderOptions) ui.Element) {
|
|
show := true
|
|
tt, n := motionFrames(func(c *ui.Context) {
|
|
if show {
|
|
loader(c, LoaderOptions{Size: 32, Label: "Thinking"})
|
|
}
|
|
}, 200, 80)
|
|
if _, ok := tt.Find("Thinking"); !ok {
|
|
t.Fatal("loader not named")
|
|
}
|
|
if motionCount(tt, "Thinking") != 1 {
|
|
t.Fatal("label shown without ShowLabel")
|
|
}
|
|
a := encode(t, tt.Image())
|
|
tt.Advance(120 * time.Millisecond)
|
|
if bytes.Equal(a, encode(t, tt.Image())) || !motionBusy(tt, n) {
|
|
t.Fatal("loader does not move")
|
|
}
|
|
tt.SetPreferences(ui.Preferences{ReduceMotion: true, TextScale: 1})
|
|
if motionBusy(tt, n) || motionCount(tt, "Thinking") != 2 {
|
|
t.Fatalf("reduced motion: still repainting or no static label: %q", tt.Texts())
|
|
}
|
|
a = encode(t, tt.Image())
|
|
tt.Advance(120 * time.Millisecond)
|
|
if !bytes.Equal(a, encode(t, tt.Image())) {
|
|
t.Fatal("loader moved under reduced motion")
|
|
}
|
|
tt.SetPreferences(ui.Preferences{TextScale: 1})
|
|
show = false
|
|
tt.Frame()
|
|
if motionBusy(tt, n) {
|
|
t.Fatal("hidden loader keeps repainting")
|
|
}
|
|
}
|
|
|
|
func TestLoadersInvalidSize(t *testing.T) {
|
|
for name, loader := range loaderCases {
|
|
func() {
|
|
defer func() {
|
|
if recover() == nil {
|
|
t.Errorf("%s: negative size did not panic", name)
|
|
}
|
|
}()
|
|
ui.Render(func(c *ui.Context) {
|
|
core.Use(c, core.Settings{})
|
|
loader(c, LoaderOptions{Size: -1})
|
|
}, 100, 50, 1)
|
|
}()
|
|
}
|
|
}
|
|
|
|
func loaderSnapshot(t *testing.T, name string, loader func(c *ui.Context, o LoaderOptions) ui.Element) {
|
|
snapshot(t, name, 480, 90, func(c *ui.Context) {
|
|
ui.Row(c).Padding(20).Gap(24).AlignItems(ui.Center).Children(func() {
|
|
loader(c, LoaderOptions{})
|
|
loader(c, LoaderOptions{Size: 28, Label: "Thinking", ShowLabel: true})
|
|
loader(c, LoaderOptions{Size: 20, Color: core.Tokens(c).Info})
|
|
})
|
|
})
|
|
}
|