Add gallery app, native window and architecture tests

This commit is contained in:
ZacharyZhang-NY committed 2026-10-11 16:45:34 -04:00
1 parent 7e47e3a924
commit d3b4adebb0
16 files changed
+1552 -5

No files matched your search

+6 -2
View File
@@ -20,7 +20,8 @@ type EditorOptions struct {
}
type Edit struct {
Element *ui.Element
// Element is laid out; Input takes the focus.
Element, Input *ui.Element
Changed, Submitted, Focused bool
}
@@ -78,7 +79,10 @@ func Editor(c *ui.Context, value *string, o EditorOptions) Edit {
}
return took
})
res := Edit{Element: shell, Changed: s.changed, Submitted: s.submitted, Focused: base.Focused()}
res := Edit{Element: shell, Input: base, Changed: s.changed, Submitted: s.submitted, Focused: base.Focused()}
if res.Focused {
frameOf(c).typing = true
}
if s.submitted {
slog.Debug("8bit: editor submit", "label", o.Label)
}
+49
View File
@@ -267,3 +267,52 @@ func TestEditorDrawsCaretAndSelection(t *testing.T) {
t.Fatal("selection not drawn in Accent with AccentInk text")
}
}
func TestEditorOwnsTypingKeys(t *testing.T) {
value, shortcuts := "", 0
tt := ui.NewTester(func(c *ui.Context) {
pixel.Use(c, pixel.Settings{Palette: pixel.SummerDay, Dot: 2})
pixel.Editor(c, &value, pixel.EditorOptions{Label: "field"})
if pixel.Typing(c) {
return
}
for _, key := range []ui.Key{ui.KeyP, ui.KeySlash, ui.KeyBracketRight, ui.KeySpace} {
if c.Shortcut(0, key) {
shortcuts++
}
}
}, 300, 100)
if err := tt.Click("field"); err != nil {
t.Fatal(err)
}
tt.TypeKey(0, ui.KeyP, "p")
tt.TypeKey(0, ui.KeySlash, "/")
tt.TypeKey(0, ui.KeyBracketRight, "]")
tt.TypeKey(0, ui.KeySpace, " ")
if value != "p/] " || shortcuts != 0 {
t.Fatalf("value %q, shortcuts fired %d times", value, shortcuts)
}
tt.ClickAt(150, 90)
tt.Key(0, ui.KeyP)
if shortcuts != 1 {
t.Fatalf("after leaving the field, P fired %d shortcuts", shortcuts)
}
}
func TestEditorSpaceDoesNotScroll(t *testing.T) {
value, scroll := "", ui.ScrollState{}
tt := ui.NewTester(func(c *ui.Context) {
pixel.Use(c, pixel.Settings{Palette: pixel.SummerDay, Dot: 2})
ui.Scroll(c).Height(100).TrackScroll(&scroll).Children(func() {
pixel.Editor(c, &value, pixel.EditorOptions{Label: "field"})
ui.Box(c).Height(1000)
})
}, 300, 100)
if err := tt.Click("field"); err != nil {
t.Fatal(err)
}
tt.TypeKey(0, ui.KeySpace, " ")
if value != " " || scroll.Y != 0 {
t.Fatalf("value %q, scrolled to %v", value, scroll.Y)
}
}
+7 -3
View File
@@ -43,8 +43,9 @@ func (k Kit) Now() time.Time { return k.now }
func (k Kit) Reduce() bool { return k.reduce }
type frame struct {
cur Settings
at time.Time
cur Settings
at time.Time
typing bool
}
type frameKey struct{}
@@ -56,10 +57,13 @@ func Use(c *ui.Context, s Settings) {
if f.cur.Palette != s.Palette {
slog.Debug("8bit: palette", "name", s.Palette.Name)
}
f.cur, f.at = s, c.Now()
f.cur, f.at, f.typing = s, c.Now(), false
c.SetTheme(theme(s.Palette))
}
// Typing reports a focused Editor built so far this frame.
func Typing(c *ui.Context) bool { return current(c).typing }
// Scope builds fn under other settings.
func Scope(c *ui.Context, s Settings, fn func()) {
f := current(c)