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>
128 lines
3.4 KiB
Go
128 lines
3.4 KiB
Go
package overlay
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
|
||
"github.com/egoist/mygo/ui"
|
||
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
|
||
)
|
||
|
||
type popoverApp struct {
|
||
open bool
|
||
top float32
|
||
panel, anchor ui.Rect
|
||
behind, applied int
|
||
}
|
||
|
||
func (a *popoverApp) view(c *ui.Context) {
|
||
core.Use(c, core.Settings{})
|
||
ui.Column(c).Padding(a.top, 0, 0, 40).Gap(8).Children(func() {
|
||
b := input.Button(c, "Filter", input.ButtonOptions{})
|
||
if b.Clicked() {
|
||
a.open = !a.open
|
||
}
|
||
a.anchor = b.Bounds()
|
||
if input.Button(c, "Behind", input.ButtonOptions{}).Clicked() {
|
||
a.behind++
|
||
}
|
||
if p := Popover(c, b, &a.open, PopoverOptions{Title: "Filter rows"}, func() {
|
||
if input.Button(c, "Apply", input.ButtonOptions{}).Clicked() {
|
||
a.applied++
|
||
a.open = false
|
||
}
|
||
}); p.Valid() {
|
||
a.panel = p.Bounds()
|
||
}
|
||
})
|
||
}
|
||
|
||
func TestPopoverOpensBelowAndFlips(t *testing.T) {
|
||
a := &popoverApp{top: 20}
|
||
tt := ui.NewTester(a.view, 400, 300)
|
||
_ = tt.Click("Filter")
|
||
tt.Frame()
|
||
if !a.open || a.panel.Y < a.anchor.Y+a.anchor.H {
|
||
t.Fatalf("below: open %v panel %+v anchor %+v", a.open, a.panel, a.anchor)
|
||
}
|
||
a = &popoverApp{top: 240}
|
||
tt = ui.NewTester(a.view, 400, 300)
|
||
_ = tt.Click("Filter")
|
||
tt.Frame()
|
||
if a.panel.Y+a.panel.H > a.anchor.Y {
|
||
t.Fatalf("no flip: panel %+v anchor %+v", a.panel, a.anchor)
|
||
}
|
||
}
|
||
|
||
func TestPopoverOutsideClickAndEscape(t *testing.T) {
|
||
a := &popoverApp{top: 20}
|
||
tt := ui.NewTester(a.view, 400, 300)
|
||
_ = tt.Click("Filter")
|
||
tt.ClickAt(390, 290)
|
||
if a.open || a.behind != 0 {
|
||
t.Fatalf("outside click: open %v behind %d", a.open, a.behind)
|
||
}
|
||
_ = tt.Click("Filter")
|
||
tt.Key(0, ui.KeyTab) // focus follows the anchor into the panel
|
||
if !tt.Focused("Apply") {
|
||
t.Fatal("Tab did not enter the popover")
|
||
}
|
||
tt.Key(0, ui.KeyEscape)
|
||
if a.open || !tt.Focused("Filter") {
|
||
t.Fatalf("Escape: open %v anchor focused %v", a.open, tt.Focused("Filter"))
|
||
}
|
||
_ = tt.Click("Filter")
|
||
_ = tt.Click("Apply")
|
||
if a.applied != 1 || a.open {
|
||
t.Fatalf("applied %d open %v", a.applied, a.open)
|
||
}
|
||
}
|
||
|
||
func TestPopoverFitsWindow(t *testing.T) {
|
||
open := true
|
||
var panel ui.Rect
|
||
tt := ui.NewTester(func(c *ui.Context) {
|
||
core.Use(c, core.Settings{})
|
||
ui.Column(c).Padding(30).Children(func() {
|
||
b := input.Button(c, "Filter", input.ButtonOptions{})
|
||
if p := Popover(c, b, &open, PopoverOptions{Width: 1000}, func() {
|
||
ui.Text(c, strings.Repeat("Long row of guests. ", 200))
|
||
input.Button(c, "Bottom", input.ButtonOptions{})
|
||
}); p.Valid() {
|
||
panel = p.Bounds()
|
||
}
|
||
})
|
||
}, 400, 300)
|
||
tt.Frame()
|
||
if panel.W > 400 || panel.X < 0 || panel.Y+panel.H > 300 {
|
||
t.Fatalf("panel %+v outside the 400×300 window", panel)
|
||
}
|
||
}
|
||
|
||
func TestPopoverNeedsAnchor(t *testing.T) {
|
||
defer func() {
|
||
if recover() == nil {
|
||
t.Fatal("no panic")
|
||
}
|
||
}()
|
||
open := true
|
||
ui.NewTester(func(c *ui.Context) {
|
||
core.Use(c, core.Settings{})
|
||
Popover(c, ui.Element{}, &open, PopoverOptions{}, nil)
|
||
}, 200, 200)
|
||
}
|
||
|
||
func TestPopoverSnapshot(t *testing.T) {
|
||
snapshot(t, "popover", 420, 260, func(c *ui.Context) {
|
||
open := true
|
||
ui.Column(c).Padding(24).AlignItems(ui.Start).Children(func() {
|
||
b := input.Button(c, "Guests", input.ButtonOptions{Variant: input.Secondary})
|
||
Popover(c, b, &open, PopoverOptions{Title: "Guest list", Width: 240}, func() {
|
||
ui.Text(c, "Twelve guests confirmed; three await a reply.").TextColor(core.Tokens(c).TextMuted)
|
||
})
|
||
})
|
||
})
|
||
}
|