Files
8bitMyGoUI/navigation/sidebar.go
T
ZacharyZhang-NY b4b4d67b92 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.
2026-10-12 16:03:04 -04:00

87 lines
2.1 KiB
Go

package navigation
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type SidebarItem struct {
Icon *pixel.Sprite
Label string
// Badge is a short count or word at the right.
Badge string
}
type SidebarSection struct {
Title string
Items []SidebarItem
}
type SidebarOptions struct {
// Width is in dots; zero means 140.
Width int
Disabled bool
}
type SidebarResult struct {
Element *ui.Element
Changed bool
}
// Sidebar lists sections of items; Up and Down choose.
func Sidebar(c *ui.Context, text string, sections []SidebarSection, selected *int, o SidebarOptions) SidebarResult {
k := pixel.K(c)
w := pixel.Or("Sidebar Width", o.Width, 140)
res := SidebarResult{}
// 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 != "" {
pixel.Text(c, sec.Title, pixel.TextOptions{Slot: pixel.Faint})
}
for _, it := range sec.Items {
i := n
n++
shell, _, st := pixel.Shell(c, func() *ui.Element {
return ui.RadioBase(c, &v, i).Label(it.Label).Disabled(o.Disabled)
})
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 = keep(c, v, selected, o.Disabled); res.Changed {
slog.Debug("8bit: sidebar", "label", text, "selected", *selected)
}
return res
}
// entry draws a sidebar row: icon, label, badge.
func entry(g pixel.Canvas, it SidebarItem, on bool, st pixel.State) {
ink := pixel.Ink
switch {
case on:
g.Rect(0, 0, g.W, g.H, pixel.Accent)
ink = pixel.AccentInk
case st.Hover:
g.Rect(0, 0, g.W, g.H, pixel.Lit)
}
x := 4
if it.Icon != nil {
g.Sprite(x, (g.H-it.Icon.H)/2, it.Icon, 0, ink, 1)
x += it.Icon.W + 4
}
g.Text(x, 1, it.Label, ink, 1)
if it.Badge != "" {
bw := pixel.Measure(it.Badge) + 6
g.Frame(g.W-bw-3, 2, bw, g.H-4, pixel.Notched, pixel.Bad)
g.Text(g.W-bw, 1, it.Badge, pixel.AccentInk, 1)
}
g.Finish(st)
}