Files
8bitMyGoUI/navigation/contextmenu.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

82 lines
2.0 KiB
Go

package navigation
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type ContextMenuOptions struct {
Disabled bool
}
type ContextMenuResult struct {
Element *ui.Element
// Chosen is the item taken this frame, or -1.
Chosen int
}
type contextKey struct{}
// spot is the menu and where it opened, in DIPs.
type spot struct {
m menu
x, y float32
}
// ContextMenu opens on right click or the menu key.
func ContextMenu(c *ui.Context, text string, items []MenuItem, o ContextMenuOptions, content func()) ContextMenuResult {
k := pixel.K(c)
off := o.Disabled || pixel.Current(c).Inert
area := ui.Box(c).FocusRing(false).Role(ui.RoleGroup).Label(text)
// The area takes focus, so the menu key reaches it.
if off {
area.Description(k.Msg("disabled"))
} else {
area.Focusable()
}
s := ui.Local(area, contextKey{}, func() spot { return spot{m: menu{lit: -1}} })
pixel.Park(c, area, off)
if off {
s.m.open = false
}
res := ContextMenuResult{Element: area, Chosen: -1}
if area.RightClicked() && !off {
s.x, s.y, _ = area.PointerPosition()
s.m.show(items)
}
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()
if !over {
x, y = 0, 0
}
s.x, s.y = x, y
s.m.show(items)
}
// Right clicks keep the focus, so the window takes keys.
res.Chosen = s.m.keys(c.Shortcut, items)
focus := area.FocusVisible()
area.DrawOver(func(p *ui.Painter, r ui.Rect) {
if focus {
g := k.Canvas(p, r)
g.FocusMark(0, 0, g.W, g.H)
}
}).Children(content)
panel, clicked := s.m.list(c, area, items)
if clicked >= 0 {
res.Chosen = clicked
}
if panel != nil {
b := area.Bounds()
panel.Left(b.X + s.x).Top(b.Y + s.y)
}
if res.Chosen >= 0 {
slog.Debug("8bit: context menu", "item", items[res.Chosen].Label)
}
return res
}