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
+18
-3
@@ -62,12 +62,25 @@ func (k Kit) At(c *ui.Context, t time.Time) {
|
||||
ask(c.Now().Add(d).UnixNano())
|
||||
}
|
||||
|
||||
// Elapsed: time since *t; clock steps back, *t becomes now.
|
||||
func (k Kit) Elapsed(t *time.Time) time.Duration {
|
||||
if k.now.Before(*t) {
|
||||
*t = k.now
|
||||
}
|
||||
return k.now.Sub(*t)
|
||||
}
|
||||
|
||||
// Since counts steps from t at fps, up to done.
|
||||
func (k Kit) Since(c *ui.Context, t time.Time, fps, done int) int {
|
||||
checkFPS(fps)
|
||||
if t.IsZero() {
|
||||
return done
|
||||
}
|
||||
n := int(k.now.Sub(t) * time.Duration(fps) / time.Second)
|
||||
// A clock stepping back holds at zero; division cannot overflow.
|
||||
n := 0
|
||||
if d := k.now.Sub(t); d > 0 {
|
||||
n = int(min(d/(time.Second/time.Duration(fps)), time.Duration(done)))
|
||||
}
|
||||
if n < done {
|
||||
k.Tick(c, fps)
|
||||
}
|
||||
@@ -125,6 +138,7 @@ type shot struct {
|
||||
|
||||
// Shot steps a cued one-shot; Reduce Motion spends it whole.
|
||||
func (k Kit) Shot(c *ui.Context, e *ui.Element, cue, fps, done int) int {
|
||||
checkFPS(fps)
|
||||
// Before any change the zero start reads as done.
|
||||
s := ui.Local(e, shotKey{}, func() shot { return shot{cue: cue} })
|
||||
if cue != s.cue {
|
||||
@@ -165,8 +179,9 @@ func frameAt(t time.Time, fps int) (int, time.Duration) {
|
||||
return int(n), time.Duration((n+1)*period - ns)
|
||||
}
|
||||
|
||||
// checkFPS holds every clock to S5's 4–15 fps.
|
||||
func checkFPS(fps int) {
|
||||
if fps < 1 || fps > 60 {
|
||||
panic(fmt.Sprintf("pixel: fps %d outside 1–60", fps))
|
||||
if fps < 4 || fps > 15 {
|
||||
panic(fmt.Sprintf("pixel: fps %d outside 4–15", fps))
|
||||
}
|
||||
}
|
||||
+14
-8
@@ -14,13 +14,18 @@ type snapshot struct {
|
||||
|
||||
// editState is one Editor's text, caret and history.
|
||||
type editState struct {
|
||||
text []rune
|
||||
caret, anchor int
|
||||
compose []rune
|
||||
composeCaret int
|
||||
undo, redo []snapshot
|
||||
changed bool
|
||||
submitted bool
|
||||
text []rune
|
||||
caret, anchor int
|
||||
compose []rune
|
||||
composeCaret int
|
||||
undo, redo []snapshot
|
||||
changed bool
|
||||
submitted bool
|
||||
// kept is the value the last frame ended with.
|
||||
kept string
|
||||
// clip is a copy waiting for an enabled build.
|
||||
clip string
|
||||
clipped bool
|
||||
scroll int
|
||||
dragging bool
|
||||
pressAt time.Time
|
||||
@@ -28,8 +33,9 @@ type editState struct {
|
||||
clicks int
|
||||
}
|
||||
|
||||
// reset takes v as the text and the value kept.
|
||||
func (s *editState) reset(v string) {
|
||||
*s = editState{text: []rune(v)}
|
||||
*s = editState{text: []rune(v), kept: v}
|
||||
s.caret = len(s.text)
|
||||
s.anchor = s.caret
|
||||
}
|
||||
|
||||
+16
-6
@@ -57,6 +57,15 @@ func Editor(c *ui.Context, value *string, o EditorOptions) Edit {
|
||||
return ui.Box(c).Focusable().Role(ui.RoleTextField).Label(o.Label).Disabled(o.Disabled)
|
||||
})
|
||||
s := ui.Local(base, editKey{}, func() editState { return editState{} })
|
||||
// Copies wait here, so a veil's first frame copies nothing.
|
||||
if s.clipped && !base.IsDisabled() {
|
||||
c.WriteClipboard(s.clip)
|
||||
}
|
||||
s.clip, s.clipped = "", false
|
||||
// A veil's first frame types nothing; callers' writes win.
|
||||
if s.changed && base.IsDisabled() && *value == string(s.text) {
|
||||
*value = s.kept
|
||||
}
|
||||
if string(s.text) != *value {
|
||||
s.reset(*value)
|
||||
}
|
||||
@@ -64,7 +73,7 @@ func Editor(c *ui.Context, value *string, o EditorOptions) Edit {
|
||||
base.Description(*value)
|
||||
}
|
||||
width, check := WrapWidth(c, shell)
|
||||
env := editEnv{multi: o.Multiline, sends: o.EnterSends, maxLen: o.MaxLength, mask: o.Mask, width: width, dot: k.dot, read: c.ReadClipboard, write: c.WriteClipboard}
|
||||
env := editEnv{multi: o.Multiline, sends: o.EnterSends, maxLen: o.MaxLength, mask: o.Mask, width: width, dot: k.dot, read: c.ReadClipboard, write: func(v string) { s.clip, s.clipped = v, true }}
|
||||
shown := s.view(o.Mask, true)
|
||||
caret := s.caret + s.composeCaret
|
||||
lines := editLayout(shown, width, o.Multiline)
|
||||
@@ -91,14 +100,15 @@ func Editor(c *ui.Context, value *string, o EditorOptions) Edit {
|
||||
}
|
||||
return took
|
||||
})
|
||||
res := Edit{Element: shell, Input: base, Changed: s.changed, Submitted: s.submitted, Focused: base.Focused(), Line: cl}
|
||||
// A send from the frame a veil falls is dropped.
|
||||
res := Edit{Element: shell, Input: base, Changed: s.changed, Submitted: s.submitted && !base.IsDisabled(), Focused: base.Focused(), Line: cl}
|
||||
if res.Focused {
|
||||
frameOf(c).typing = true
|
||||
}
|
||||
if s.submitted {
|
||||
if res.Submitted {
|
||||
slog.Debug("8bit: editor submit", "label", o.Label)
|
||||
}
|
||||
s.changed, s.submitted = false, false
|
||||
s.changed, s.submitted, s.kept = false, false, *value
|
||||
shell.Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
check(r)
|
||||
g := k.Canvas(p, r)
|
||||
@@ -127,7 +137,7 @@ func Editor(c *ui.Context, value *string, o EditorOptions) Edit {
|
||||
}
|
||||
}
|
||||
}
|
||||
if res.Focused && !o.Disabled && g.Step(2)%2 == 0 {
|
||||
if res.Focused && !o.Disabled && g.Step(4)%4 < 2 {
|
||||
g.VLine(cx-scroll, cl*LineHeight+1, LineHeight-2, Ink)
|
||||
}
|
||||
})
|
||||
@@ -270,7 +280,7 @@ func (s *editState) handle(ev ui.InputEvent, e editEnv) bool {
|
||||
|
||||
func (s *editState) press(ev ui.InputEvent, e editEnv) {
|
||||
now := time.Now()
|
||||
if now.Sub(s.pressAt) < 500*time.Millisecond && abs32(ev.X-s.pressX) < 5 && abs32(ev.Y-s.pressY) < 5 {
|
||||
if d := now.Sub(s.pressAt); d >= 0 && d < 500*time.Millisecond && abs32(ev.X-s.pressX) < 5 && abs32(ev.Y-s.pressY) < 5 {
|
||||
s.clicks++
|
||||
} else {
|
||||
s.clicks = 1
|
||||
|
||||
@@ -345,3 +345,36 @@ func TestEditorTakesAValueChangedBetweenFrames(t *testing.T) {
|
||||
h.tt.Type("x")
|
||||
h.want("x")
|
||||
}
|
||||
|
||||
func TestEditorVeilKeepsCallersAndClipboard(t *testing.T) {
|
||||
// The caller writes during the build the veil falls in.
|
||||
v, inert, update := "old", false, false
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
pixel.Use(c, pixel.Settings{Palette: pixel.SpringDay, Inert: inert})
|
||||
if update {
|
||||
v, update = "server", false
|
||||
}
|
||||
pixel.Editor(c, &v, pixel.EditorOptions{Label: "Edit"}).Element.Width(200)
|
||||
}, 400, 100)
|
||||
tt.Key(0, ui.KeyTab)
|
||||
inert, update = true, true
|
||||
tt.Type("x")
|
||||
if v != "server" {
|
||||
t.Fatalf("the caller's write became %q", v)
|
||||
}
|
||||
for _, command := range []string{"copy", "cut"} {
|
||||
v, inert := "SecretValue", false
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
pixel.Use(c, pixel.Settings{Palette: pixel.SpringDay, Inert: inert})
|
||||
pixel.Editor(c, &v, pixel.EditorOptions{Label: "Edit"}).Element.Width(200)
|
||||
}, 400, 100)
|
||||
tt.Key(0, ui.KeyTab)
|
||||
tt.Key(ui.Cmd, ui.KeyA)
|
||||
tt.SetClipboard("original")
|
||||
inert = true
|
||||
tt.Command(command)
|
||||
if got := tt.Clipboard(); got != "original" || v != "SecretValue" {
|
||||
t.Errorf("%s as the veil fell: clipboard %q, value %q", command, got, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,6 +102,12 @@ func Shell(c *ui.Context, base func() *ui.Element) (shell, b *ui.Element, st Sta
|
||||
return shell, b, st
|
||||
}
|
||||
|
||||
// Shortcut is e's key this frame, while e is enabled.
|
||||
func Shortcut(e *ui.Element, mods ui.Modifiers, key ui.Key) bool {
|
||||
// MyGo still hands keys to a base just turned off.
|
||||
return e.Shortcut(mods, key) && !e.IsDisabled()
|
||||
}
|
||||
|
||||
type parkKey struct{}
|
||||
|
||||
// Park moves focus off e while off, back once on.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package pixel_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -153,3 +154,79 @@ func TestTowardStepsAndTurnsWhereItIs(t *testing.T) {
|
||||
}, 10, 10)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMotionStepsAtFourToFifteen(t *testing.T) {
|
||||
at := time.Unix(1_700_000_000, 0)
|
||||
run := func(fn func(c *ui.Context, k pixel.Kit)) {
|
||||
ui.NewTester(func(c *ui.Context) {
|
||||
pixel.Use(c, pixel.Settings{Palette: pixel.SpringDay, Now: func() time.Time { return at }})
|
||||
fn(c, pixel.K(c))
|
||||
}, 10, 10)
|
||||
}
|
||||
// S5: whole frames at 4–15 fps, for every clock.
|
||||
for _, fps := range []int{1, 2, 3, 16, 30} {
|
||||
mustPanic(t, fmt.Sprint("Tick at ", fps), func() { run(func(c *ui.Context, k pixel.Kit) { k.Tick(c, fps) }) })
|
||||
mustPanic(t, fmt.Sprint("Since at ", fps), func() { run(func(c *ui.Context, k pixel.Kit) { k.Since(c, at, fps, 9) }) })
|
||||
mustPanic(t, fmt.Sprint("Toward at ", fps), func() { run(func(c *ui.Context, k pixel.Kit) { k.Toward(c, ui.Box(c), true, fps, 2) }) })
|
||||
mustPanic(t, fmt.Sprint("Since from zero at ", fps), func() { run(func(c *ui.Context, k pixel.Kit) { k.Since(c, time.Time{}, fps, 9) }) })
|
||||
mustPanic(t, fmt.Sprint("Shot at ", fps), func() { run(func(c *ui.Context, k pixel.Kit) { k.Shot(c, ui.Box(c), 0, fps, 9) }) })
|
||||
// Reduce Motion spends a shot without counting.
|
||||
mustPanic(t, fmt.Sprint("a reduced Shot at ", fps), func() {
|
||||
ui.NewTester(func(c *ui.Context) {
|
||||
pixel.Use(c, pixel.Settings{Palette: pixel.SpringDay, Now: func() time.Time { return at }, Reduce: true})
|
||||
pixel.K(c).Shot(c, ui.Box(c), 0, fps, 9)
|
||||
}, 10, 10)
|
||||
})
|
||||
mustPanic(t, fmt.Sprint("Step at ", fps), func() {
|
||||
run(func(c *ui.Context, k pixel.Kit) {
|
||||
ui.Box(c).Size(4, 4).Draw(func(p *ui.Painter, r ui.Rect) { k.Canvas(p, r).Step(fps) })
|
||||
})
|
||||
})
|
||||
}
|
||||
for _, fps := range []int{4, 15} {
|
||||
run(func(c *ui.Context, k pixel.Kit) {
|
||||
k.Tick(c, fps)
|
||||
k.Since(c, at, fps, 9)
|
||||
k.Toward(c, ui.Box(c), true, fps, 2)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSinceHoldsAtZeroWhenTheClockStepsBack(t *testing.T) {
|
||||
at := time.Unix(1_700_000_000, 0)
|
||||
got := -1
|
||||
ui.NewTester(func(c *ui.Context) {
|
||||
pixel.Use(c, pixel.Settings{Palette: pixel.SpringDay, Now: func() time.Time { return at }})
|
||||
got = pixel.K(c).Since(c, at.Add(time.Hour), 8, 5)
|
||||
}, 10, 10)
|
||||
if got != 0 {
|
||||
t.Fatalf("a start an hour ahead counted %d steps", got)
|
||||
}
|
||||
// Thirty years ahead must not overflow into a full count.
|
||||
ui.NewTester(func(c *ui.Context) {
|
||||
pixel.Use(c, pixel.Settings{Palette: pixel.SpringDay, Now: func() time.Time { return at }})
|
||||
got = pixel.K(c).Since(c, at.AddDate(30, 0, 0), 15, 5)
|
||||
}, 10, 10)
|
||||
if got != 0 {
|
||||
t.Fatalf("a start thirty years ahead counted %d steps", got)
|
||||
}
|
||||
ui.NewTester(func(c *ui.Context) {
|
||||
pixel.Use(c, pixel.Settings{Palette: pixel.SpringDay, Now: func() time.Time { return at }})
|
||||
got = pixel.K(c).Since(c, at.AddDate(-30, 0, 0), 15, 5)
|
||||
}, 10, 10)
|
||||
if got != 5 {
|
||||
t.Fatalf("a start thirty years back counted %d of 5 steps", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestElapsedRebasesWhenTheClockStepsBack(t *testing.T) {
|
||||
at := time.Unix(1_700_000_000, 0)
|
||||
start, got := at.Add(time.Hour), time.Duration(-1)
|
||||
ui.NewTester(func(c *ui.Context) {
|
||||
pixel.Use(c, pixel.Settings{Palette: pixel.SpringDay, Now: func() time.Time { return at }})
|
||||
got = pixel.K(c).Elapsed(&start)
|
||||
}, 10, 10)
|
||||
if got != 0 || !start.Equal(at) {
|
||||
t.Fatalf("elapsed %v, start moved to %v", got, start)
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -22,11 +22,11 @@ func Pad(c *ui.Context, text string, cols, n, cw, ch int, cur *int, disabled boo
|
||||
{ui.KeyUp, func() int { return *cur - cols }}, {ui.KeyDown, func() int { return *cur + cols }},
|
||||
{ui.KeyHome, func() int { return 0 }}, {ui.KeyEnd, func() int { return n - 1 }},
|
||||
} {
|
||||
if to := m.to(); base.Shortcut(0, m.key) && to >= 0 && to < n {
|
||||
if to := m.to(); Shortcut(base, 0, m.key) && to >= 0 && to < n {
|
||||
*cur = to
|
||||
}
|
||||
}
|
||||
enter, space := base.Shortcut(0, ui.KeyEnter), base.Shortcut(0, ui.KeySpace)
|
||||
enter, space := Shortcut(base, 0, ui.KeyEnter), Shortcut(base, 0, ui.KeySpace)
|
||||
if enter || space {
|
||||
picked = *cur
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user