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
@@ -41,17 +41,17 @@ func CommandMenu(c *ui.Context, text string, items []MenuItem, o CommandMenuOpti
|
||||
res.Chosen = i
|
||||
}
|
||||
}
|
||||
if base.Shortcut(0, ui.KeyDown) {
|
||||
if pixel.Shortcut(base, 0, ui.KeyDown) {
|
||||
*cur = (*cur + 1) % n
|
||||
}
|
||||
if base.Shortcut(0, ui.KeyUp) {
|
||||
if pixel.Shortcut(base, 0, ui.KeyUp) {
|
||||
*cur = (*cur + n - 1) % n
|
||||
}
|
||||
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)
|
||||
if enter || space {
|
||||
pick(*cur)
|
||||
}
|
||||
res.Canceled = base.Shortcut(0, ui.KeyEscape)
|
||||
res.Canceled = pixel.Shortcut(base, 0, ui.KeyEscape)
|
||||
// Enter and Space are taken, so clicks come from pointers.
|
||||
if base.Clicked() {
|
||||
_, y, _ := base.PointerPosition()
|
||||
|
||||
@@ -68,16 +68,16 @@ func CommandPalette(c *ui.Context, text string, commands []MenuItem, open *bool,
|
||||
if f.Changed {
|
||||
st.lit = 0
|
||||
}
|
||||
if f.Element.Shortcut(0, ui.KeyDown) {
|
||||
if pixel.Shortcut(f.Element, 0, ui.KeyDown) {
|
||||
st.lit++
|
||||
}
|
||||
if f.Element.Shortcut(0, ui.KeyUp) {
|
||||
if pixel.Shortcut(f.Element, 0, ui.KeyUp) {
|
||||
st.lit--
|
||||
}
|
||||
st.lit = max(0, min(st.lit, len(shown)-1))
|
||||
st.top = max(0, min(st.top, st.lit, len(shown)-paletteRows), st.lit-paletteRows+1)
|
||||
// A Shortcut, not Submitted: the frame builds again closed.
|
||||
if f.Element.Shortcut(0, ui.KeyEnter) && len(shown) > 0 {
|
||||
if pixel.Shortcut(f.Element, 0, ui.KeyEnter) && len(shown) > 0 {
|
||||
run(shown[st.lit])
|
||||
}
|
||||
ui.Column(c).AlignItems(ui.Stretch).Children(func() {
|
||||
|
||||
@@ -47,7 +47,7 @@ func ContextMenu(c *ui.Context, text string, items []MenuItem, o ContextMenuOpti
|
||||
s.x, s.y, _ = area.PointerPosition()
|
||||
s.m.show(items)
|
||||
}
|
||||
key, shiftF10 := area.Shortcut(0, ui.KeyContextMenu), area.Shortcut(ui.Shift, ui.KeyF10)
|
||||
key, shiftF10 := pixel.Shortcut(area, 0, ui.KeyContextMenu), pixel.Shortcut(area, ui.Shift, ui.KeyF10)
|
||||
if (key || shiftF10) && !off {
|
||||
// At the pointer when over the area, else its corner.
|
||||
x, y, over := area.PointerPosition()
|
||||
|
||||
+35
-21
@@ -1,6 +1,7 @@
|
||||
package navigation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
@@ -30,35 +31,48 @@ type DocumentTabsResult struct {
|
||||
// DocumentTabs are closable tabs; × or Delete reports Closed.
|
||||
func DocumentTabs(c *ui.Context, text string, titles []string, selected *int, o DocumentTabsOptions) DocumentTabsResult {
|
||||
k := pixel.K(c)
|
||||
seen := map[string]bool{}
|
||||
for _, t := range titles {
|
||||
if seen[t] {
|
||||
panic(fmt.Sprintf("navigation: DocumentTabs has %q twice", t))
|
||||
}
|
||||
seen[t] = true
|
||||
}
|
||||
res := DocumentTabsResult{Closed: -1}
|
||||
p := ui.TabsBase(c, selected, len(titles))
|
||||
// The base turns before it learns it is off.
|
||||
v := *selected
|
||||
p := ui.TabsBase(c, &v, len(titles))
|
||||
p.List.Label(text).Children(func() {
|
||||
for i, t := range titles {
|
||||
var x *ui.Element
|
||||
shell, base, st := pixel.Shell(c, func() *ui.Element {
|
||||
tab := p.Tab(i).Label(t).Disabled(o.Disabled)
|
||||
// The × is no Tab stop; Delete closes by keyboard.
|
||||
tab.Children(func() {
|
||||
x = ui.Box(c).Absolute().Right(k.D(4)).Top(k.D(6)).Size(k.D(7), k.D(7)).Role(ui.RoleButton).Label(k.Msg("close") + " " + t)
|
||||
// Keyed by title, so focus follows its tab.
|
||||
ui.Box(c).Key(t).Children(func() {
|
||||
var x *ui.Element
|
||||
shell, base, st := pixel.Shell(c, func() *ui.Element {
|
||||
tab := p.Tab(i).Label(t).Disabled(o.Disabled)
|
||||
// The × is no Tab stop; Delete closes by keyboard.
|
||||
tab.Children(func() {
|
||||
x = ui.Box(c).Absolute().Right(k.D(4)).Top(k.D(6)).Size(k.D(7), k.D(7)).Role(ui.RoleButton).Label(k.Msg("close") + " " + t)
|
||||
})
|
||||
return tab
|
||||
})
|
||||
return tab
|
||||
})
|
||||
if x.Clicked() || base.Shortcut(0, ui.KeyDelete) {
|
||||
res.Closed = i
|
||||
}
|
||||
hot := x.Hovered()
|
||||
shell.Size(k.D(pixel.Measure(t)+24), k.D(pixel.LineHeight+6)).Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
g := k.Canvas(p, r)
|
||||
folder(g, t, i == *selected, false, st)
|
||||
ink := pixel.Faint
|
||||
if hot {
|
||||
ink = pixel.Bad
|
||||
del := pixel.Shortcut(base, 0, ui.KeyDelete)
|
||||
if x.Clicked() || del {
|
||||
res.Closed = i
|
||||
}
|
||||
g.Sprite(g.W-10, 7, cross, 0, ink, 1)
|
||||
hot := x.Hovered()
|
||||
shell.Size(k.D(pixel.Measure(t)+24), k.D(pixel.LineHeight+6)).Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
g := k.Canvas(p, r)
|
||||
folder(g, t, i == *selected, false, st)
|
||||
ink := pixel.Faint
|
||||
if hot {
|
||||
ink = pixel.Bad
|
||||
}
|
||||
g.Sprite(g.W-10, 7, cross, 0, ink, 1)
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
res.Element, res.Changed = p.List, p.List.Changed()
|
||||
res.Element, res.Changed = p.List, keep(c, v, selected, o.Disabled)
|
||||
if res.Closed >= 0 {
|
||||
slog.Debug("8bit: tab closed", "label", text, "tab", titles[res.Closed])
|
||||
}
|
||||
|
||||
@@ -24,18 +24,15 @@ func DropdownMenu(c *ui.Context, text string, items []MenuItem, o DropdownMenuOp
|
||||
off := o.Disabled || pixel.Current(c).Inert
|
||||
b := input.Button(c, text+" ▼", input.ButtonOptions{Label: text, Disabled: o.Disabled})
|
||||
m := ui.Local(b.Element, menuKey{}, func() menu { return menu{lit: -1} })
|
||||
// The button keeps the focus and parks it while off.
|
||||
if off {
|
||||
// Closing on disable must not return focus to it.
|
||||
if m.open {
|
||||
c.Root().Focus()
|
||||
}
|
||||
m.open = false
|
||||
}
|
||||
res := DropdownMenuResult{Element: b.Element, Chosen: -1}
|
||||
switch {
|
||||
case b.Clicked && m.open:
|
||||
m.open = false
|
||||
case b.Clicked, !m.open && !off && b.Element.Shortcut(0, ui.KeyDown):
|
||||
case b.Clicked, !m.open && !off && pixel.Shortcut(b.Element, 0, ui.KeyDown):
|
||||
m.show(items)
|
||||
}
|
||||
res.Chosen = m.keys(b.Element.Shortcut, items)
|
||||
|
||||
@@ -44,9 +44,8 @@ func MenuBar(c *ui.Context, text string, menus []Menu, o MenuBarOptions) MenuBar
|
||||
b.open = i
|
||||
b.m.show(menus[i].Items)
|
||||
}
|
||||
// The title keeps the focus and parks it while off.
|
||||
if off && b.open >= 0 {
|
||||
// Closing on disable must not return focus to it.
|
||||
c.Root().Focus()
|
||||
b.open = -1
|
||||
}
|
||||
if b.open >= 0 && !b.m.open {
|
||||
@@ -55,7 +54,7 @@ func MenuBar(c *ui.Context, text string, menus []Menu, o MenuBarOptions) MenuBar
|
||||
n := len(menus)
|
||||
// Left and Right hand an open menu to its neighbor.
|
||||
if b.open >= 0 {
|
||||
left, right := row.Shortcut(0, ui.KeyLeft), row.Shortcut(0, ui.KeyRight)
|
||||
left, right := pixel.Shortcut(row, 0, ui.KeyLeft), pixel.Shortcut(row, 0, ui.KeyRight)
|
||||
switch {
|
||||
case left:
|
||||
open((b.open + n - 1) % n)
|
||||
@@ -87,7 +86,7 @@ func MenuBar(c *ui.Context, text string, menus []Menu, o MenuBarOptions) MenuBar
|
||||
case t.Clicked:
|
||||
open(i)
|
||||
anchor = t.Element
|
||||
case b.open < 0 && !off && t.Element.FocusWithin() && row.Shortcut(0, ui.KeyDown):
|
||||
case b.open < 0 && !off && t.Element.FocusWithin() && pixel.Shortcut(row, 0, ui.KeyDown):
|
||||
open(i)
|
||||
anchor = t.Element
|
||||
}
|
||||
|
||||
@@ -88,14 +88,15 @@ func TestTabsChooseAndShowTheirPanel(t *testing.T) {
|
||||
for _, side := range []bool{false, true} {
|
||||
tab, changes := 0, 0
|
||||
tt := view(500, 300, newClock(), false, func(c *ui.Context) {
|
||||
build := navigation.Tabs
|
||||
if side {
|
||||
build = navigation.VerticalTabs
|
||||
}
|
||||
names := []string{"Items", "Magic", "Status"}
|
||||
if build(c, "Menu", names, &tab, navigation.TabsOptions{}, func() {
|
||||
pixel.Text(c, "panel "+names[tab], pixel.TextOptions{})
|
||||
}).Changed {
|
||||
panel := func() { pixel.Text(c, "panel "+names[tab], pixel.TextOptions{}) }
|
||||
changed := false
|
||||
if side {
|
||||
changed = navigation.VerticalTabs(c, "Menu", names, &tab, navigation.VerticalTabsOptions{}, panel).Changed
|
||||
} else {
|
||||
changed = navigation.Tabs(c, "Menu", names, &tab, navigation.TabsOptions{}, panel).Changed
|
||||
}
|
||||
if changed {
|
||||
changes++
|
||||
}
|
||||
})
|
||||
|
||||
@@ -109,13 +109,13 @@ func PageDots(c *ui.Context, text string, page *int, total int, o PageDotsOption
|
||||
{ui.KeyLeft, func() int { return *page - 1 }}, {ui.KeyRight, func() int { return *page + 1 }},
|
||||
{ui.KeyHome, func() int { return 1 }}, {ui.KeyEnd, func() int { return total }},
|
||||
} {
|
||||
if base.Shortcut(0, m.key) {
|
||||
if pixel.Shortcut(base, 0, m.key) {
|
||||
*page = max(1, min(total, m.to()))
|
||||
}
|
||||
}
|
||||
// Taking Enter and Space keeps keys from clicking.
|
||||
base.Shortcut(0, ui.KeyEnter)
|
||||
base.Shortcut(0, ui.KeySpace)
|
||||
pixel.Shortcut(base, 0, ui.KeyEnter)
|
||||
pixel.Shortcut(base, 0, ui.KeySpace)
|
||||
if base.Clicked() {
|
||||
x, _, _ := base.PointerPosition()
|
||||
*page = max(1, min(total, int(x/k.Dot())/8+1))
|
||||
|
||||
+10
-12
@@ -26,14 +26,13 @@ type NavRailResult struct {
|
||||
func NavRail(c *ui.Context, text string, items []NavItem, selected *int, o NavRailOptions) NavRailResult {
|
||||
k := pixel.K(c)
|
||||
res := NavRailResult{}
|
||||
// The bases turn before they learn they are off.
|
||||
v := *selected
|
||||
res.Element = ui.RadioGroup(c, func() {
|
||||
for i, it := range items {
|
||||
shell, base, st := pixel.Shell(c, func() *ui.Element {
|
||||
return ui.RadioBase(c, selected, i).Label(it.Label).Disabled(o.Disabled)
|
||||
shell, _, st := pixel.Shell(c, func() *ui.Element {
|
||||
return ui.RadioBase(c, &v, i).Label(it.Label).Disabled(o.Disabled)
|
||||
})
|
||||
if base.Changed() {
|
||||
res.Changed = true
|
||||
}
|
||||
shell.Size(k.D(22), k.D(22)).Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
g := k.Canvas(p, r)
|
||||
ink := pixel.Ink
|
||||
@@ -56,7 +55,7 @@ func NavRail(c *ui.Context, text string, items []NavItem, selected *int, o NavRa
|
||||
g.Rect(0, 0, g.W, g.H, pixel.Face)
|
||||
g.VLine(g.W-1, 0, g.H, pixel.Edge)
|
||||
})
|
||||
if res.Changed {
|
||||
if res.Changed = keep(c, v, selected, o.Disabled); res.Changed {
|
||||
slog.Debug("8bit: nav rail", "label", text, "selected", *selected)
|
||||
}
|
||||
return res
|
||||
@@ -88,14 +87,13 @@ type BottomNavResult struct {
|
||||
func BottomNav(c *ui.Context, text string, items []NavItem, selected *int, o BottomNavOptions) BottomNavResult {
|
||||
k := pixel.K(c)
|
||||
res := BottomNavResult{}
|
||||
// The bases turn before they learn they are off.
|
||||
v := *selected
|
||||
res.Element = ui.RadioGroup(c, func() {
|
||||
for i, it := range items {
|
||||
shell, base, st := pixel.Shell(c, func() *ui.Element {
|
||||
return ui.RadioBase(c, selected, i).Label(it.Label).Disabled(o.Disabled)
|
||||
shell, _, st := pixel.Shell(c, func() *ui.Element {
|
||||
return ui.RadioBase(c, &v, i).Label(it.Label).Disabled(o.Disabled)
|
||||
})
|
||||
if base.Changed() {
|
||||
res.Changed = true
|
||||
}
|
||||
w := max(pixel.Measure(it.Label), 12) + 12
|
||||
shell.Size(k.D(w), k.D(16+pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
g := k.Canvas(p, r)
|
||||
@@ -116,7 +114,7 @@ func BottomNav(c *ui.Context, text string, items []NavItem, selected *int, o Bot
|
||||
g.Rect(0, 0, g.W, g.H, pixel.Face)
|
||||
g.HLine(0, 0, g.W, pixel.Edge)
|
||||
})
|
||||
if res.Changed {
|
||||
if res.Changed = keep(c, v, selected, o.Disabled); res.Changed {
|
||||
slog.Debug("8bit: bottom nav", "label", text, "selected", *selected)
|
||||
}
|
||||
return res
|
||||
|
||||
@@ -36,7 +36,8 @@ func Sidebar(c *ui.Context, text string, sections []SidebarSection, selected *in
|
||||
k := pixel.K(c)
|
||||
w := pixel.Or("Sidebar Width", o.Width, 140)
|
||||
res := SidebarResult{}
|
||||
n := 0
|
||||
// The bases turn before they learn they are off.
|
||||
n, v := 0, *selected
|
||||
res.Element = ui.RadioGroup(c, func() {
|
||||
for _, sec := range sections {
|
||||
if sec.Title != "" {
|
||||
@@ -45,19 +46,16 @@ func Sidebar(c *ui.Context, text string, sections []SidebarSection, selected *in
|
||||
for _, it := range sec.Items {
|
||||
i := n
|
||||
n++
|
||||
shell, base, st := pixel.Shell(c, func() *ui.Element {
|
||||
return ui.RadioBase(c, selected, i).Label(it.Label).Disabled(o.Disabled)
|
||||
shell, _, st := pixel.Shell(c, func() *ui.Element {
|
||||
return ui.RadioBase(c, &v, i).Label(it.Label).Disabled(o.Disabled)
|
||||
})
|
||||
if base.Changed() {
|
||||
res.Changed = true
|
||||
}
|
||||
shell.Size(k.D(w), k.D(pixel.LineHeight+2)).Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
entry(k.Canvas(p, r), it, i == *selected, st)
|
||||
})
|
||||
}
|
||||
}
|
||||
}).Gap(k.D(1)).Label(text)
|
||||
if res.Changed {
|
||||
if res.Changed = keep(c, v, selected, o.Disabled); res.Changed {
|
||||
slog.Debug("8bit: sidebar", "label", text, "selected", *selected)
|
||||
}
|
||||
return res
|
||||
|
||||
+24
-4
@@ -23,9 +23,18 @@ func Tabs(c *ui.Context, text string, labels []string, selected *int, o TabsOpti
|
||||
return tabs(c, text, labels, selected, false, o, panel)
|
||||
}
|
||||
|
||||
type VerticalTabsOptions struct {
|
||||
Disabled bool
|
||||
}
|
||||
|
||||
type VerticalTabsResult struct {
|
||||
Element *ui.Element
|
||||
Changed bool
|
||||
}
|
||||
|
||||
// VerticalTabs are tabs down the panel's left edge.
|
||||
func VerticalTabs(c *ui.Context, text string, labels []string, selected *int, o TabsOptions, panel func()) TabsResult {
|
||||
return tabs(c, text, labels, selected, true, o, panel)
|
||||
func VerticalTabs(c *ui.Context, text string, labels []string, selected *int, o VerticalTabsOptions, panel func()) VerticalTabsResult {
|
||||
return VerticalTabsResult(tabs(c, text, labels, selected, true, TabsOptions(o), panel))
|
||||
}
|
||||
|
||||
// tabs lays tabs beside a panel; side means left.
|
||||
@@ -49,7 +58,9 @@ func tabs(c *ui.Context, text string, labels []string, selected *int, side bool,
|
||||
}
|
||||
res := TabsResult{Element: outer}
|
||||
outer.Children(func() {
|
||||
p := ui.TabsBase(c, selected, len(labels))
|
||||
// The base turns before it learns it is off.
|
||||
v := *selected
|
||||
p := ui.TabsBase(c, &v, len(labels))
|
||||
if side {
|
||||
p.List.Column().Width(k.D(tw))
|
||||
}
|
||||
@@ -65,7 +76,7 @@ func tabs(c *ui.Context, text string, labels []string, selected *int, side bool,
|
||||
})
|
||||
}
|
||||
})
|
||||
res.Changed = p.List.Changed()
|
||||
res.Changed = keep(c, v, selected, o.Disabled)
|
||||
at := 0
|
||||
for i := range *selected {
|
||||
at += size(i)
|
||||
@@ -123,3 +134,12 @@ func folder(g pixel.Canvas, l string, on, side bool, st pixel.State) {
|
||||
}
|
||||
g.Finish(st)
|
||||
}
|
||||
|
||||
// keep takes v as *selected while the control is on.
|
||||
func keep(c *ui.Context, v int, selected *int, disabled bool) bool {
|
||||
if v == *selected || disabled || pixel.Current(c).Inert {
|
||||
return false
|
||||
}
|
||||
*selected = v
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package navigation_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/navigation"
|
||||
)
|
||||
|
||||
func TestPageDotsKeysDoNotClickWhereThePointerRests(t *testing.T) {
|
||||
page := 1
|
||||
tt := view(400, 200, newClock(), false, func(c *ui.Context) {
|
||||
navigation.PageDots(c, "Pages", &page, 9, navigation.PageDotsOptions{})
|
||||
})
|
||||
r := find(t, tt, "Pages")
|
||||
tt.Move(r.X+r.W-4, r.Y+r.H/2)
|
||||
tt.Key(0, ui.KeyTab)
|
||||
tt.Key(0, ui.KeySpace)
|
||||
tt.Key(0, ui.KeyEnter)
|
||||
if page != 1 {
|
||||
t.Fatalf("Space and Enter turned to page %d", page)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSpaceTakesTheCursorNotThePointer(t *testing.T) {
|
||||
chosen := -1
|
||||
tt := view(400, 300, newClock(), false, func(c *ui.Context) {
|
||||
if r := navigation.CommandMenu(c, "Battle", []navigation.MenuItem{{Label: "FIGHT"}, {Label: "MAGIC"}, {Label: "RUN"}}, navigation.CommandMenuOptions{}); r.Chosen >= 0 {
|
||||
chosen = r.Chosen
|
||||
}
|
||||
})
|
||||
r := find(t, tt, "Battle")
|
||||
tt.Move(r.X+r.W/2, r.Y+r.H-6)
|
||||
tt.Key(0, ui.KeyTab)
|
||||
tt.Key(0, ui.KeySpace)
|
||||
if chosen != 0 {
|
||||
t.Fatalf("Space in the menu chose %d", chosen)
|
||||
}
|
||||
at, entered := 0, 0
|
||||
nodes := []navigation.MapNode{{Name: "Town", X: 20, Y: 20}, {Name: "Cave", X: 120, Y: 20}}
|
||||
tt = view(600, 400, newClock(), false, func(c *ui.Context) {
|
||||
if navigation.WorldMap(c, "Map", nodes, [][2]int{{0, 1}}, &at, navigation.WorldMapOptions{}).Entered {
|
||||
entered++
|
||||
}
|
||||
})
|
||||
r = find(t, tt, "Map")
|
||||
// The pointer rests on the Cave; the hero is home.
|
||||
tt.Move(r.X+2*120, r.Y+2*20)
|
||||
tt.Key(0, ui.KeyTab)
|
||||
tt.Key(0, ui.KeySpace)
|
||||
if at != 0 || entered != 1 {
|
||||
t.Fatalf("Space walked to %d, entered %d times", at, entered)
|
||||
}
|
||||
}
|
||||
+5
-6
@@ -38,14 +38,13 @@ func TableOfContents(c *ui.Context, text string, items []TocItem, current *int,
|
||||
k := pixel.K(c)
|
||||
w := pixel.Or("TableOfContents Width", o.Width, 160)
|
||||
res := TableOfContentsResult{}
|
||||
// The bases turn before they learn they are off.
|
||||
v := *current
|
||||
res.Element = ui.RadioGroup(c, func() {
|
||||
for i, it := range items {
|
||||
shell, base, st := pixel.Shell(c, func() *ui.Element {
|
||||
return ui.RadioBase(c, current, i).Label(it.Title).Disabled(o.Disabled)
|
||||
shell, _, st := pixel.Shell(c, func() *ui.Element {
|
||||
return ui.RadioBase(c, &v, i).Label(it.Title).Disabled(o.Disabled)
|
||||
})
|
||||
if base.Changed() {
|
||||
res.Changed = true
|
||||
}
|
||||
shell.Size(k.D(w), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
g := k.Canvas(p, r)
|
||||
x, ink := 8+it.Level*10, pixel.Faint
|
||||
@@ -60,7 +59,7 @@ func TableOfContents(c *ui.Context, text string, items []TocItem, current *int,
|
||||
})
|
||||
}
|
||||
}).Gap(0).Label(text)
|
||||
if res.Changed {
|
||||
if res.Changed = keep(c, v, current, o.Disabled); res.Changed {
|
||||
slog.Debug("8bit: toc", "label", text, "section", items[*current].Title)
|
||||
}
|
||||
return res
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package navigation_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/navigation"
|
||||
)
|
||||
|
||||
func TestKeysInAVeilsFirstFrameTurnNothing(t *testing.T) {
|
||||
nav := []navigation.NavItem{{Icon: icon.Home, Label: "Home"}, {Icon: icon.Star, Label: "Stars"}}
|
||||
cases := map[string]struct {
|
||||
build func(c *ui.Context, v *int)
|
||||
key ui.Key
|
||||
}{
|
||||
"NavRail": {func(c *ui.Context, v *int) { navigation.NavRail(c, "Rail", nav, v, navigation.NavRailOptions{}) }, ui.KeyDown},
|
||||
"BottomNav": {func(c *ui.Context, v *int) { navigation.BottomNav(c, "Nav", nav, v, navigation.BottomNavOptions{}) }, ui.KeyRight},
|
||||
"TableOfContents": {func(c *ui.Context, v *int) {
|
||||
navigation.TableOfContents(c, "Contents", []navigation.TocItem{{Title: "Intro"}, {Title: "Rules"}}, v, navigation.TableOfContentsOptions{})
|
||||
}, ui.KeyDown},
|
||||
"Sidebar": {func(c *ui.Context, v *int) {
|
||||
navigation.Sidebar(c, "Side", []navigation.SidebarSection{{Title: "Game", Items: []navigation.SidebarItem{{Label: "Play"}, {Label: "Load"}}}}, v, navigation.SidebarOptions{})
|
||||
}, ui.KeyDown},
|
||||
"Tabs": {func(c *ui.Context, v *int) {
|
||||
navigation.Tabs(c, "Tabs", []string{"Items", "Magic"}, v, navigation.TabsOptions{}, func() {})
|
||||
}, ui.KeyRight},
|
||||
"DocumentTabs": {func(c *ui.Context, v *int) {
|
||||
navigation.DocumentTabs(c, "Docs", []string{"a.txt", "b.txt"}, v, navigation.DocumentTabsOptions{})
|
||||
}, ui.KeyRight},
|
||||
}
|
||||
for name, tc := range cases {
|
||||
for _, veil := range []bool{false, true} {
|
||||
v, inert := 0, false
|
||||
tt := veiled(&inert, func(c *ui.Context) { tc.build(c, &v) })
|
||||
tt.Key(0, ui.KeyTab)
|
||||
inert = veil
|
||||
tt.Key(0, tc.key)
|
||||
if turned := v != 0; turned == veil {
|
||||
t.Errorf("%s: veiled %v, the key made %s", name, veil, fmt.Sprint(v))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteInAVeilsFirstFrameClosesNoTab(t *testing.T) {
|
||||
for _, veil := range []bool{false, true} {
|
||||
v, closed, inert := 0, -1, false
|
||||
tt := veiled(&inert, func(c *ui.Context) {
|
||||
if r := navigation.DocumentTabs(c, "Docs", []string{"a.txt", "b.txt"}, &v, navigation.DocumentTabsOptions{}); r.Closed >= 0 {
|
||||
closed = r.Closed
|
||||
}
|
||||
})
|
||||
tt.Key(0, ui.KeyTab)
|
||||
inert = veil
|
||||
tt.Key(0, ui.KeyDelete)
|
||||
if (closed == 0) == veil {
|
||||
t.Errorf("veiled %v, Delete closed %d", veil, closed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentTabFocusFollowsItsTab(t *testing.T) {
|
||||
titles, sel, closed := []string{"a.txt", "b.txt", "c.txt"}, 1, ""
|
||||
tt := view(600, 300, newClock(), false, func(c *ui.Context) {
|
||||
if r := navigation.DocumentTabs(c, "Docs", titles, &sel, navigation.DocumentTabsOptions{}); r.Closed >= 0 {
|
||||
closed = titles[r.Closed]
|
||||
}
|
||||
})
|
||||
tt.Key(0, ui.KeyTab)
|
||||
// a.txt closes elsewhere; b.txt keeps its focus.
|
||||
titles, sel = titles[1:], 0
|
||||
tt.Frame()
|
||||
tt.Key(0, ui.KeyDelete)
|
||||
if closed != "b.txt" {
|
||||
t.Fatalf("Delete after a.txt left closed %q", closed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTwinDocumentTabsPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if r := fmt.Sprint(recover()); !strings.HasPrefix(r, "navigation: DocumentTabs") {
|
||||
t.Fatalf("two a.txt tabs gave %q", r)
|
||||
}
|
||||
}()
|
||||
sel := 0
|
||||
view(400, 200, newClock(), false, func(c *ui.Context) {
|
||||
navigation.DocumentTabs(c, "Docs", []string{"a.txt", "a.txt"}, &sel, navigation.DocumentTabsOptions{})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package navigation_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/navigation"
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
||||
)
|
||||
|
||||
// veiled builds under a veil the test raises and lifts.
|
||||
func veiled(inert *bool, build func(c *ui.Context)) *ui.Tester {
|
||||
return view(600, 400, newClock(), false, func(c *ui.Context) {
|
||||
s := pixel.Current(c)
|
||||
s.Inert = *inert
|
||||
pixel.Scope(c, s, func() { build(c) })
|
||||
})
|
||||
}
|
||||
|
||||
func TestOpenMenusGiveFocusBackAfterAVeil(t *testing.T) {
|
||||
items := []navigation.MenuItem{{Label: "One"}, {Label: "Two"}}
|
||||
for name, build := range map[string]func(c *ui.Context){
|
||||
"dropdown": func(c *ui.Context) { navigation.DropdownMenu(c, "File", items, navigation.DropdownMenuOptions{}) },
|
||||
"menubar": func(c *ui.Context) {
|
||||
navigation.MenuBar(c, "Bar", []navigation.Menu{{Title: "Edit", Items: items}}, navigation.MenuBarOptions{})
|
||||
},
|
||||
} {
|
||||
inert := false
|
||||
tt := veiled(&inert, build)
|
||||
tt.Key(0, ui.KeyTab)
|
||||
tt.Key(0, ui.KeyDown)
|
||||
if !tt.HasText("Two") {
|
||||
t.Fatalf("%s did not open: %q", name, tt.Texts())
|
||||
}
|
||||
inert = true
|
||||
tt.Frame()
|
||||
inert = false
|
||||
tt.Frame()
|
||||
// Only focus on the opener lets Down open it again.
|
||||
tt.Key(0, ui.KeyDown)
|
||||
if !tt.HasText("Two") {
|
||||
t.Errorf("%s did not get its focus back after the veil", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,13 +80,13 @@ func WorldMap(c *ui.Context, text string, nodes []MapNode, paths [][2]int, at *i
|
||||
key ui.Key
|
||||
dx, dy int
|
||||
}{{ui.KeyLeft, -1, 0}, {ui.KeyRight, 1, 0}, {ui.KeyUp, 0, -1}, {ui.KeyDown, 0, 1}} {
|
||||
if base.Shortcut(0, m.key) {
|
||||
if pixel.Shortcut(base, 0, m.key) {
|
||||
if j := toward(nodes, paths, *at, m.dx, m.dy); j >= 0 {
|
||||
walkTo(j)
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
res.Entered = enter || space
|
||||
// Enter and Space are taken, so clicks come from pointers.
|
||||
if base.Clicked() {
|
||||
|
||||
Reference in new issue
Block a user