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:
198 files changed
+2708
-422
No files matched your search
+1
-1
@@ -39,7 +39,7 @@ func Carousel(c *ui.Context, text string, pages int, page *int, o CarouselOption
|
||||
wait := every == 0 || o.Disabled || pixel.Current(c).Inert || k.Reduce() || col.Hovered() || col.FocusWithin()
|
||||
if wait {
|
||||
*since = k.Now()
|
||||
} else if k.Since(c, *since, 1, every) == every {
|
||||
} else if k.Since(c, *since, 4, 4*every) == 4*every {
|
||||
turn(1)
|
||||
}
|
||||
col.Children(func() {
|
||||
|
||||
+6
-2
@@ -23,10 +23,14 @@ func Collapsible(c *ui.Context, title string, open *bool, o CollapsibleOptions,
|
||||
col := ui.Column(c).AlignItems(ui.Stretch)
|
||||
res := CollapsibleResult{Element: col}
|
||||
col.Children(func() {
|
||||
// The base turns before it learns it is off.
|
||||
v := *open
|
||||
shell, base, st := pixel.Shell(c, func() *ui.Element {
|
||||
return ui.CollapsibleBase(c, open).Trigger.Label(title).Disabled(o.Disabled)
|
||||
return ui.CollapsibleBase(c, &v).Trigger.Label(title).Disabled(o.Disabled)
|
||||
})
|
||||
res.Changed = base.Changed()
|
||||
if res.Changed = v != *open && !base.IsDisabled(); res.Changed {
|
||||
*open = v
|
||||
}
|
||||
shown := *open
|
||||
shell.Height(k.D(pixel.LineHeight + 6)).MinWidth(k.D(pixel.Measure(title) + 20)).Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
g := k.Canvas(p, r)
|
||||
|
||||
+3
-2
@@ -86,7 +86,8 @@ func GridView(c *ui.Context, text string, n int, selected *int, o GridViewOption
|
||||
{ui.KeyLeft, old - 1}, {ui.KeyRight, old + 1}, {ui.KeyUp, old - cols}, {ui.KeyDown, old + cols},
|
||||
{ui.KeyHome, 0}, {ui.KeyEnd, n - 1},
|
||||
} {
|
||||
if box.Shortcut(0, m.key) && m.to >= 0 && m.to < n {
|
||||
// The grid stays enabled, so off is checked here.
|
||||
if !off && pixel.Shortcut(box, 0, m.key) && m.to >= 0 && m.to < n {
|
||||
*selected = m.to
|
||||
}
|
||||
}
|
||||
@@ -94,7 +95,7 @@ func GridView(c *ui.Context, text string, n int, selected *int, o GridViewOption
|
||||
res.Changed = true
|
||||
s.rows.ScrollIntoView(*selected / cols)
|
||||
}
|
||||
if box.Shortcut(0, ui.KeyEnter) {
|
||||
if !off && pixel.Shortcut(box, 0, ui.KeyEnter) {
|
||||
res.Submitted = true
|
||||
}
|
||||
if off {
|
||||
|
||||
+1
-1
@@ -80,7 +80,7 @@ func Outline[K comparable](c *ui.Context, text string, roots []K, children func(
|
||||
pixel.Text(c, label(r.key), pixel.TextOptions{Slot: ink})
|
||||
})
|
||||
})).Height(k.D(pixel.Or("Outline Height", o.Height, 140))).Label(text)
|
||||
right, left := e.Shortcut(0, ui.KeyRight), e.Shortcut(0, ui.KeyLeft)
|
||||
right, left := pixel.Shortcut(e, 0, ui.KeyRight), pixel.Shortcut(e, 0, ui.KeyLeft)
|
||||
if (right || left) && st.at >= 0 && !off {
|
||||
at, toggled := steer(rows, st.at, st.opened, right)
|
||||
if toggled {
|
||||
|
||||
+4
-4
@@ -50,16 +50,16 @@ func Sortable(c *ui.Context, text string, items *[]string, o SortableOptions) So
|
||||
*items = slices.Insert(slices.Delete(*items, from, from+1), to, it)
|
||||
s.cur, res.Changed = to, true
|
||||
}
|
||||
if base.Shortcut(0, ui.KeyDown) {
|
||||
if pixel.Shortcut(base, 0, ui.KeyDown) {
|
||||
s.cur = min(s.cur+1, n-1)
|
||||
}
|
||||
if base.Shortcut(0, ui.KeyUp) {
|
||||
if pixel.Shortcut(base, 0, ui.KeyUp) {
|
||||
s.cur = max(s.cur-1, 0)
|
||||
}
|
||||
if base.Shortcut(ui.Alt, ui.KeyDown) {
|
||||
if pixel.Shortcut(base, ui.Alt, ui.KeyDown) {
|
||||
move(s.cur, s.cur+1)
|
||||
}
|
||||
if base.Shortcut(ui.Alt, ui.KeyUp) {
|
||||
if pixel.Shortcut(base, ui.Alt, ui.KeyUp) {
|
||||
move(s.cur, s.cur-1)
|
||||
}
|
||||
// A press takes a row; dragging carries it.
|
||||
|
||||
+11
-8
@@ -71,7 +71,8 @@ func Tree(c *ui.Context, text string, roots []TreeNode, selected *string, o Tree
|
||||
if len(rows) == 0 {
|
||||
return TreeResult{Element: shell}
|
||||
}
|
||||
at := 0
|
||||
// An unknown choice stays, unlit, until a key or click.
|
||||
at := -1
|
||||
for i, r := range rows {
|
||||
if r.key == *selected {
|
||||
at = i
|
||||
@@ -79,23 +80,23 @@ func Tree(c *ui.Context, text string, roots []TreeNode, selected *string, o Tree
|
||||
}
|
||||
old := *selected
|
||||
res := TreeResult{Element: shell}
|
||||
if base.Shortcut(0, ui.KeyDown) {
|
||||
if pixel.Shortcut(base, 0, ui.KeyDown) {
|
||||
at = min(at+1, len(rows)-1)
|
||||
}
|
||||
if base.Shortcut(0, ui.KeyUp) {
|
||||
if pixel.Shortcut(base, 0, ui.KeyUp) {
|
||||
at = max(at-1, 0)
|
||||
}
|
||||
right, left := base.Shortcut(0, ui.KeyRight), base.Shortcut(0, ui.KeyLeft)
|
||||
if right || left {
|
||||
right, left := pixel.Shortcut(base, 0, ui.KeyRight), pixel.Shortcut(base, 0, ui.KeyLeft)
|
||||
if (right || left) && at >= 0 {
|
||||
at, res.Toggled = steer(rows, at, *opened, right)
|
||||
}
|
||||
enter, space := base.Shortcut(0, ui.KeyEnter), base.Shortcut(0, ui.KeySpace)
|
||||
enter, space := pixel.Shortcut(base, 0, ui.KeyEnter), pixel.Shortcut(base, 0, ui.KeySpace)
|
||||
toggle := func(i int) {
|
||||
if key := rows[i].key; rows[i].branch {
|
||||
(*opened)[key], res.Toggled = !(*opened)[key], true
|
||||
}
|
||||
}
|
||||
if enter || space {
|
||||
if (enter || space) && at >= 0 {
|
||||
toggle(at)
|
||||
}
|
||||
// Enter and Space are taken, so clicks come from pointers.
|
||||
@@ -112,7 +113,9 @@ func Tree(c *ui.Context, text string, roots []TreeNode, selected *string, o Tree
|
||||
rows = flatten(tops, children, *opened)
|
||||
at = min(at, len(rows)-1)
|
||||
}
|
||||
*selected = rows[at].key
|
||||
if at >= 0 {
|
||||
*selected = rows[at].key
|
||||
}
|
||||
res.Changed = *selected != old
|
||||
base.Description(strings.TrimSpace(nodes[*selected].Label + " " + nodes[*selected].Value))
|
||||
shown := rows
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package data_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/data"
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
||||
)
|
||||
|
||||
func TestCollapsibleKeyInAVeilsFirstFrameOpensNothing(t *testing.T) {
|
||||
for _, veil := range []bool{false, true} {
|
||||
open, inert := false, false
|
||||
tt := underVeil(&inert, func(c *ui.Context) {
|
||||
data.Collapsible(c, "Quests", &open, data.CollapsibleOptions{}, func() { pixel.Text(c, "Slay", pixel.TextOptions{}) })
|
||||
})
|
||||
tt.Key(0, ui.KeyTab)
|
||||
inert = veil
|
||||
tt.Key(0, ui.KeySpace)
|
||||
if open == veil {
|
||||
t.Errorf("veiled %v, Space left it open %v", veil, open)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package data_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/data"
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
||||
)
|
||||
|
||||
// underVeil builds fn where the test raises a veil.
|
||||
func underVeil(inert *bool, fn func(c *ui.Context)) *ui.Tester {
|
||||
return view(400, 300, newClock(), false, func(c *ui.Context) {
|
||||
s := pixel.Current(c)
|
||||
s.Inert = *inert
|
||||
pixel.Scope(c, s, func() { fn(c) })
|
||||
})
|
||||
}
|
||||
|
||||
var tree = []data.TreeNode{{Label: "Maps", Children: []data.TreeNode{{Label: "Cave"}}}, {Label: "Saves"}}
|
||||
|
||||
func TestVeilsStopKeysInTheirFirstFrame(t *testing.T) {
|
||||
for name, tc := range map[string]struct {
|
||||
build func(c *ui.Context, v *string)
|
||||
mods ui.Modifiers
|
||||
key ui.Key
|
||||
}{
|
||||
"Sortable": {func(c *ui.Context, v *string) {
|
||||
items := []string{"A", "B", "C"}
|
||||
if r := data.Sortable(c, "List", &items, data.SortableOptions{}); r.Changed {
|
||||
*v = fmt.Sprint(items)
|
||||
}
|
||||
}, ui.Alt, ui.KeyDown},
|
||||
"Tree": {func(c *ui.Context, v *string) { data.Tree(c, "Files", tree, v, data.TreeOptions{}) }, 0, ui.KeyDown},
|
||||
"Inventory": {func(c *ui.Context, v *string) {
|
||||
at := 0
|
||||
fmt.Sscan(*v, &at)
|
||||
bag := []data.Item{{Sprite: icon.Sword, Name: "Sword"}, {Sprite: icon.Gem, Name: "Gem"}}
|
||||
data.Inventory(c, "Bag", bag, &at, data.InventoryOptions{})
|
||||
*v = fmt.Sprint(at)
|
||||
}, 0, ui.KeyRight},
|
||||
} {
|
||||
v, inert := "0", false
|
||||
tt := underVeil(&inert, func(c *ui.Context) { tc.build(c, &v) })
|
||||
tt.Key(0, ui.KeyTab)
|
||||
inert = true
|
||||
tt.Key(tc.mods, tc.key)
|
||||
if v != "0" {
|
||||
t.Errorf("%s: a key in the veil's first frame made %s", name, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDragsStopUnderAVeil(t *testing.T) {
|
||||
items, inert := []string{"A", "B", "C"}, false
|
||||
tt := underVeil(&inert, func(c *ui.Context) { data.Sortable(c, "List", &items, data.SortableOptions{}) })
|
||||
r := find(t, tt, "List")
|
||||
row := r.H / 3
|
||||
tt.Press(r.X+10, r.Y+row/2)
|
||||
tt.Move(r.X+10, r.Y+row*3/2)
|
||||
moved := fmt.Sprint(items)
|
||||
inert = true
|
||||
tt.Move(r.X+10, r.Y+row*5/2)
|
||||
tt.Release(r.X+10, r.Y+row*5/2)
|
||||
if moved != "[B A C]" || fmt.Sprint(items) != moved {
|
||||
t.Errorf("Sortable: %s before the veil, %v under it", moved, items)
|
||||
}
|
||||
bag, at := []data.Item{{Sprite: icon.Sword, Name: "Sword"}, {Sprite: icon.Gem, Name: "Gem"}}, 0
|
||||
inert = false
|
||||
tt = underVeil(&inert, func(c *ui.Context) { data.Inventory(c, "Bag", bag, &at, data.InventoryOptions{}) })
|
||||
r = find(t, tt, "Bag")
|
||||
tt.Press(r.X+20, r.Y+20)
|
||||
tt.Move(r.X+64, r.Y+20)
|
||||
inert = true
|
||||
tt.Release(r.X+64, r.Y+20)
|
||||
if bag[0].Name != "Sword" {
|
||||
t.Error("Inventory swapped under the veil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeLeavesAnUnknownChoiceAlone(t *testing.T) {
|
||||
chosen, changed := "9/9", 0
|
||||
tt := view(400, 300, newClock(), false, func(c *ui.Context) {
|
||||
if data.Tree(c, "Files", tree, &chosen, data.TreeOptions{}).Changed {
|
||||
changed++
|
||||
}
|
||||
})
|
||||
if chosen != "9/9" || changed != 0 {
|
||||
t.Fatalf("an unknown choice became %q, %d changes", chosen, changed)
|
||||
}
|
||||
tt.Key(0, ui.KeyTab)
|
||||
tt.Key(0, ui.KeyDown)
|
||||
if chosen != "0" || changed != 1 {
|
||||
t.Fatalf("Down chose %q, %d changes", chosen, changed)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user