Audit keyboard, focus, veils, motion and clocks across all 500

A cross-cutting pass, each fix with a test that failed first.

Veils: MyGo still hands a key to a base that turned off this frame,
so every element key now goes through pixel.Shortcut, which an
archtest enforces. Input handlers record and the build applies them
only while enabled; the editor turns away a veil-frame letter or
copy; value bases get a copy and keep only enabled turns. Popups
give a held focus back to their opener when the veil lifts.

Motion: S5 is enforced at the clock. Every Tick, Since, Step,
Toward and Shot runs at 4-15 fps; blinks keep 1 Hz on 4 fps steps,
fast animations step at 15, and drawn time is quantized to frames.

Clocks stepping back: Since holds at zero without overflow, and
k.Elapsed moves a stored start to now, so nothing goes negative,
runs backward or indexes out of range.

Also: logs count typed text instead of holding it, gallery names
use glyphs the font has, removable rows are keyed, components take
their own Options (archtest), and sweeps over all 508 demos check
veiled keys, clicks, wheels, the clock stepping back and names.
This commit is contained in:
ZacharyZhang-NY committed 2026-10-12 16:03:04 -04:00
1 parent 81003b21ee
commit b4b4d67b92
198 files changed
+2708 -422

No files matched your search

+1 -1
View File
@@ -49,7 +49,7 @@ func AlertDialog(c *ui.Context, title, message string, open *bool, o AlertDialog
res.Element = ui.DialogBase(c, open, func(back, panel *ui.Element) {
dim(k, back, pixel.Back, 8)
panel.Width(k.D(220)).Role(ui.RoleAlertDialog).Label(title).Description(message).Focusable().FocusRing(false)
if panel.Shortcut(0, ui.KeyEnter) && !o.Disabled && !pixel.Current(c).Inert {
if pixel.Shortcut(panel, 0, ui.KeyEnter) && !o.Disabled && !pixel.Current(c).Inert {
press(o.Default)
}
panel.Children(func() {
+7 -2
View File
@@ -50,6 +50,11 @@ func DialogueBox(c *ui.Context, pages []string, page *int, o DialogueBoxOptions)
panic(fmt.Sprintf("overlay: DialogueBox page %d of %d", *page, len(pages)))
}
cps := pixel.Or("DialogueBox CPS", o.CPS, 30)
if cps > 1000 {
panic(fmt.Sprintf("overlay: DialogueBox CPS %d over 1000", cps))
}
// S5 steps at 4–15 fps; fast typing shows more.
fps := max(4, min(cps, 15))
off := o.Disabled || pixel.Current(c).Inert
box := ui.Box(c).Role(ui.RoleText).Label(strings.TrimSpace(o.Name + " " + pages[*page])).FocusRing(false)
if !off {
@@ -98,7 +103,7 @@ func DialogueBox(c *ui.Context, pages []string, page *int, o DialogueBoxOptions)
}
shown := total
if !st.out && !k.Reduce() {
shown = k.Since(c, st.since, cps, total)
shown = min(total, k.Since(c, st.since, fps, (total*fps+cps-1)/cps)*cps/fps)
}
st.out = shown == total
words.Height(k.D(max(2, len(lines)) * pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
@@ -112,7 +117,7 @@ func DialogueBox(c *ui.Context, pages []string, page *int, o DialogueBoxOptions)
left -= len(lr)
end = pixel.Measure(string(lr[:n]))
}
if shown == total && g.Step(2)%2 == 0 {
if shown == total && g.Step(4)%4 < 2 {
g.Sprite(end+3, len(lines)*pixel.LineHeight-6, more, 0, pixel.Accent, 1)
}
})
+1 -1
View File
@@ -34,7 +34,7 @@ func Lightbox(c *ui.Context, text string, n int, at *int, open *bool, o Lightbox
res.Element = ui.DialogBase(c, open, func(back, panel *ui.Element) {
deep, light := dark(k)
dim(k, back, deep, 13)
left, right := panel.Shortcut(0, ui.KeyLeft), panel.Shortcut(0, ui.KeyRight)
left, right := pixel.Shortcut(panel, 0, ui.KeyLeft), pixel.Shortcut(panel, 0, ui.KeyRight)
switch {
case off:
case left:
+1 -1
View File
@@ -52,7 +52,7 @@ func Notifications(c *ui.Context, queue *[]Notification, o NotificationsOptions)
if life == 0 {
life = 5 * time.Second
}
if n.shown.IsZero() || now.Sub(n.shown) < life {
if n.shown.IsZero() || k.Elapsed(&n.shown) < life {
kept = append(kept, n)
}
}
+6 -5
View File
@@ -40,18 +40,19 @@ func choose(c *ui.Context, text string, items []string, cur *int, digits, off bo
if off {
box.Description(k.Msg("disabled"))
}
if box.Shortcut(0, ui.KeyDown) {
// The list stays enabled, so off is checked here.
if !off && pixel.Shortcut(box, 0, ui.KeyDown) {
*cur = (*cur + 1) % n
}
if box.Shortcut(0, ui.KeyUp) {
if !off && pixel.Shortcut(box, 0, ui.KeyUp) {
*cur = (*cur + n - 1) % n
}
enter, space := box.Shortcut(0, ui.KeyEnter), box.Shortcut(0, ui.KeySpace)
if enter || space {
enter, space := pixel.Shortcut(box, 0, ui.KeyEnter), pixel.Shortcut(box, 0, ui.KeySpace)
if (enter || space) && !off {
picked = *cur
}
for i := range min(9, n) {
if digits && box.Shortcut(0, ui.Key1+ui.Key(i)) {
if digits && !off && pixel.Shortcut(box, 0, ui.Key1+ui.Key(i)) {
*cur, picked = i, i
}
}
+47
View File
@@ -1,6 +1,8 @@
package overlay_test
import (
"fmt"
"strings"
"testing"
"time"
@@ -348,3 +350,48 @@ func TestDisabledPanelsTakeNoClicks(t *testing.T) {
t.Fatalf("disabled: at %d open %v saving %v next %d queue %d", at, open, saving, next, len(queue))
}
}
func TestDialogueBoxKeepsItsSpeedOnS5Steps(t *testing.T) {
for _, wait := range []time.Duration{900 * time.Millisecond, 1050 * time.Millisecond} {
clk, page := newClock(), 0
// Thirty letters at the default thirty a second.
pages := []string{"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234", "NEXT"}
tt := view(640, 400, clk, false, func(c *ui.Context) {
overlay.DialogueBox(c, pages, &page, overlay.DialogueBoxOptions{})
})
clk.t = clk.t.Add(wait)
tt.Frame()
r := find(t, tt, pages[0])
tt.ClickAt(r.X+4, r.Y+4)
// A press on typed-out text turns the page.
if turned := page == 1; turned != (wait > time.Second) {
t.Errorf("after %v a press left page %d", wait, page)
}
}
}
func TestPausedBlinksOnceASecond(t *testing.T) {
clk, paused := newClock(), true
tt := view(640, 400, clk, false, func(c *ui.Context) { overlay.PauseMenu(c, &paused, overlay.PauseMenuOptions{}) })
var ink []int
for _, at := range []time.Duration{100, 350, 600} {
clk.t = time.Unix(3_000_000, 0).Add(at * time.Millisecond)
tt.Frame()
ink = append(ink, count(t, tt.Image(), find(t, tt, "PAUSED"), pixel.Back))
}
if ink[0] != ink[1] || ink[1] == ink[2] {
t.Fatalf("PAUSED ink at .1, .35 and .6 s: %v", ink)
}
}
func TestDialogueBoxSpeedHasACeiling(t *testing.T) {
defer func() {
if r := fmt.Sprint(recover()); !strings.HasPrefix(r, "overlay: DialogueBox CPS") {
t.Fatalf("CPS 1001 gave %q", r)
}
}()
page := 0
view(640, 400, newClock(), false, func(c *ui.Context) {
overlay.DialogueBox(c, []string{"HI"}, &page, overlay.DialogueBoxOptions{CPS: 1001})
})
}
+1 -1
View File
@@ -36,7 +36,7 @@ func PauseMenu(c *ui.Context, paused *bool, o PauseMenuOptions) PauseMenuResult
pixel.Harbor(c, panel, func() {
ui.Box(c).Size(k.D(pixel.Measure(title)*3), k.D(pixel.LineHeight*3)).Role(ui.RoleText).Label(title).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
if g.Step(2)%2 == 0 || k.Reduce() {
if g.Step(4)%4 < 2 || k.Reduce() {
g.Text(0, 0, title, light, 3)
}
})
+16 -6
View File
@@ -21,35 +21,45 @@ type PopconfirmResult struct {
type popKey struct{}
// popping is open, and held the focus last frame.
type popping struct{ open, held bool }
// Popconfirm asks question in a bubble before its button acts.
func Popconfirm(c *ui.Context, text, question string, o PopconfirmOptions) PopconfirmResult {
k := pixel.K(c)
wrap := ui.Box(c)
open := ui.Local(wrap, popKey{}, func() bool { return false })
pop := ui.Local(wrap, popKey{}, func() popping { return popping{} })
open := &pop.open
var b input.ButtonResult
wrap.Children(func() { b = input.Button(c, text, input.ButtonOptions{Disabled: o.Disabled}) })
if b.Clicked {
*open = true
}
// A popup above a veil closes with it, focus parked.
if o.Disabled || pixel.Current(c).Inert {
if *open {
c.Root().Focus()
// A focus in the closing popup parks on the button.
if *open && pop.held {
b.Base.Focus()
pixel.Park(c, b.Base, true)
}
*open = false
}
was := *open
res := PopconfirmResult{Element: wrap}
pop.held = false
Popover(c, question, b.Element, open, PopoverOptions{}, func() {
ui.Column(c).Gap(k.D(4)).Children(func() {
pixel.Text(c, question, pixel.TextOptions{})
ui.Row(c).Gap(k.D(4)).Justify(ui.End).Children(func() {
if input.Button(c, k.Msg("no"), input.ButtonOptions{Size: input.Small}).Clicked {
no := input.Button(c, k.Msg("no"), input.ButtonOptions{Size: input.Small})
yes := input.Button(c, k.Msg("yes"), input.ButtonOptions{Size: input.Small, Variant: input.Primary})
if no.Clicked {
*open = false
}
if input.Button(c, k.Msg("yes"), input.ButtonOptions{Size: input.Small, Variant: input.Primary}).Clicked {
if yes.Clicked {
res.Confirmed, *open = true, false
}
// Focused, not FocusWithin: MyGo links parents after frames.
pop.held = no.Base.Focused() || yes.Base.Focused()
})
})
})
+1 -1
View File
@@ -36,7 +36,7 @@ func PromptDialog(c *ui.Context, title string, value *string, open *bool, o Prom
f := input.TextInput(c, title, value, input.TextInputOptions{Placeholder: o.Placeholder, MaxLength: o.MaxLength, Width: 196, Disabled: o.Disabled})
f.Input.AutoFocus()
// A Shortcut, not Submitted: the frame builds again closed.
if f.Element.Shortcut(0, ui.KeyEnter) && !o.Disabled {
if pixel.Shortcut(f.Element, 0, ui.KeyEnter) && !o.Disabled {
res.Submitted, *open = true, false
}
actions(c, []string{k.Msg("cancel"), k.Msg("ok")}, o.Disabled, func(i int) {
+73
View File
@@ -0,0 +1,73 @@
package overlay_test
import (
"testing"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/overlay"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
func TestPopconfirmGivesPopupFocusBackAfterAVeil(t *testing.T) {
inert := false
tt := view(400, 300, newClock(), false, func(c *ui.Context) {
s := pixel.Current(c)
s.Inert = inert
pixel.Scope(c, s, func() { overlay.Popconfirm(c, "DELETE", "Sure?", overlay.PopconfirmOptions{}) })
pixel.Text(c, "Elsewhere", pixel.TextOptions{})
})
tt.Key(0, ui.KeyTab)
tt.Key(0, ui.KeyEnter)
tt.Key(0, ui.KeyTab)
// Focus in the popup, not the root that holds everything.
if !tt.Focused("NO") || tt.Focused("Elsewhere") {
t.Fatalf("focus is not in the popup: %q", tt.Texts())
}
inert = true
tt.Frame()
inert = false
tt.Frame()
tt.Key(0, ui.KeyEnter)
if !tt.HasText("Sure?") {
t.Fatal("the button did not get its focus back after the veil")
}
}
func TestSheetPullStopsUnderAVeil(t *testing.T) {
open, inert := true, false
tt := view(400, 400, newClock(), false, func(c *ui.Context) {
s := pixel.Current(c)
s.Inert = inert
pixel.Scope(c, s, func() {
overlay.Sheet(c, "Bag", &open, overlay.SheetOptions{}, func() { pixel.Text(c, "Potion", pixel.TextOptions{}) })
})
})
r := find(t, tt, "Bag")
tt.Press(r.X+r.W/2, r.Y+10)
tt.Move(r.X+r.W/2, r.Y+60)
inert = true
tt.Move(r.X+r.W/2, r.Y+200)
tt.Release(r.X+r.W/2, r.Y+200)
if !open {
t.Fatal("a pull under the veil closed the sheet")
}
}
func TestPauseMenuPicksNothingAsAVeilFalls(t *testing.T) {
paused, inert, chose := true, false, -1
tt := view(640, 400, newClock(), false, func(c *ui.Context) {
s := pixel.Current(c)
s.Inert = inert
pixel.Scope(c, s, func() {
if r := overlay.PauseMenu(c, &paused, overlay.PauseMenuOptions{}); r.Chosen >= 0 {
chose = r.Chosen
}
})
})
inert = true
tt.Key(0, ui.KeyEnter)
if chose >= 0 {
t.Fatalf("Enter as the veil fell chose %d", chose)
}
}