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>
121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package input
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
|
|
)
|
|
|
|
func ratingTester(v *float64, opts RatingOptions, changes *int) *ui.Tester {
|
|
return ui.NewTester(func(c *ui.Context) {
|
|
core.Use(c, core.Settings{})
|
|
ui.Column(c).Padding(10).AlignItems(ui.Start).Children(func() {
|
|
if Rating(c, v, opts).Changed() {
|
|
*changes++
|
|
}
|
|
})
|
|
}, 300, 100)
|
|
}
|
|
|
|
func TestRatingKeyboardSteps(t *testing.T) {
|
|
v, n := 2.0, 0
|
|
tt := ratingTester(&v, RatingOptions{Step: 0.5, Label: "Taste"}, &n)
|
|
tt.Key(0, ui.KeyTab)
|
|
if !tt.Focused("Taste") {
|
|
t.Fatal("rating not focusable")
|
|
}
|
|
tt.Key(0, ui.KeyRight)
|
|
if v != 2.5 {
|
|
t.Fatalf("right: %v", v)
|
|
}
|
|
tt.Key(0, ui.KeyDown)
|
|
tt.Key(0, ui.KeyDown)
|
|
if v != 1.5 {
|
|
t.Fatalf("down twice: %v", v)
|
|
}
|
|
tt.Key(0, ui.KeyEnd)
|
|
tt.Key(0, ui.KeyRight)
|
|
if v != 5 {
|
|
t.Fatalf("end then right: %v", v)
|
|
}
|
|
tt.Key(0, ui.KeyHome)
|
|
tt.Key(0, ui.KeyLeft)
|
|
if v != 0 || n != 5 {
|
|
t.Fatalf("home then left: %v, %d changes", v, n)
|
|
}
|
|
}
|
|
|
|
func TestRatingPointer(t *testing.T) {
|
|
v, n := 0.0, 0
|
|
tt := ratingTester(&v, RatingOptions{Step: 0.5, Label: "Taste"}, &n)
|
|
size := theme.MinHitSize
|
|
// The left half of the third star.
|
|
tt.ClickAt(10+2*size+size*0.25, 10+size/2)
|
|
if v != 2.5 || n != 1 {
|
|
t.Fatalf("click: %v, %d changes", v, n)
|
|
}
|
|
tt.ClickAt(10+2*size+size*0.25, 10+size/2)
|
|
if n != 1 {
|
|
t.Fatalf("same value reported a change")
|
|
}
|
|
}
|
|
|
|
func TestRatingReadOnlyAndDisabled(t *testing.T) {
|
|
for _, opts := range []RatingOptions{{ReadOnly: true, Label: "Score"}, {Disabled: true, Label: "Score"}} {
|
|
v, n := 3.0, 0
|
|
tt := ratingTester(&v, opts, &n)
|
|
tt.ClickAt(12, 20)
|
|
tt.Key(0, ui.KeyTab)
|
|
tt.Key(0, ui.KeyRight)
|
|
if v != 3 || n != 0 {
|
|
t.Fatalf("%+v: value %v changes %d", opts, v, n)
|
|
}
|
|
if opts.ReadOnly != tt.Focused("Score") {
|
|
t.Fatalf("%+v: focus %v", opts, tt.Focused("Score"))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRatingShowsValueAndPanics(t *testing.T) {
|
|
v, n := 3.5, 0
|
|
tt := ratingTester(&v, RatingOptions{Step: 0.5, ShowValue: true}, &n)
|
|
if !tt.HasText("3.5 / 5") {
|
|
t.Fatalf("texts %q", tt.Texts())
|
|
}
|
|
defer func() {
|
|
if recover() == nil {
|
|
t.Fatal("Step 0.3 accepted")
|
|
}
|
|
}()
|
|
ratingTester(&v, RatingOptions{Step: 0.3}, &n)
|
|
}
|
|
|
|
func TestRatingSnapshot(t *testing.T) {
|
|
a, b, d := 3.5, 4.0, 2.0
|
|
snapshot(t, "rating", 360, 170, func(c *ui.Context) {
|
|
ui.Column(c).Padding(20).Gap(14).AlignItems(ui.Start).Children(func() {
|
|
Rating(c, &a, RatingOptions{Step: 0.5, ShowValue: true})
|
|
Rating(c, &b, RatingOptions{ReadOnly: true, Max: 7, ShowValue: true})
|
|
Rating(c, &d, RatingOptions{Disabled: true})
|
|
})
|
|
})
|
|
}
|
|
|
|
func TestRatingHitAreaCompact(t *testing.T) {
|
|
v := 2.0
|
|
var b ui.Rect
|
|
tt := ui.NewTester(func(c *ui.Context) {
|
|
core.Use(c, core.Settings{Density: theme.Compact})
|
|
ui.Column(c).AlignItems(ui.Start).Children(func() {
|
|
b = Rating(c, &v, RatingOptions{}).Element.Bounds()
|
|
})
|
|
}, 300, 100)
|
|
tt.Frame()
|
|
if b.W < 5*28 || b.H < 28 {
|
|
t.Fatalf("stars %v", b)
|
|
}
|
|
}
|