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.
111 lines
2.8 KiB
Go
111 lines
2.8 KiB
Go
package navigation
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
type Menu struct {
|
|
Title string
|
|
Items []MenuItem
|
|
}
|
|
|
|
type MenuBarOptions struct {
|
|
Disabled bool
|
|
}
|
|
|
|
type MenuBarResult struct {
|
|
Element *ui.Element
|
|
// Menu and Item are this frame's choice, else -1.
|
|
Menu, Item int
|
|
}
|
|
|
|
type barKey struct{}
|
|
|
|
// bar is the open menu, its state and the pointer.
|
|
type bar struct {
|
|
open int
|
|
m menu
|
|
ptr [2]float32
|
|
}
|
|
|
|
// MenuBar is a bar of menus; arrows go across, down.
|
|
func MenuBar(c *ui.Context, text string, menus []Menu, o MenuBarOptions) MenuBarResult {
|
|
k := pixel.K(c)
|
|
off := o.Disabled || pixel.Current(c).Inert
|
|
row := ui.Row(c).Gap(k.D(2)).Padding(k.D(2), k.D(4)).AlignItems(ui.Center).FocusGroup(ui.Horizontal).Role(ui.RoleToolbar).Label(text)
|
|
b := ui.Local(row, barKey{}, func() bar { return bar{open: -1} })
|
|
res := MenuBarResult{Element: row, Menu: -1, Item: -1}
|
|
open := func(i int) {
|
|
b.open = i
|
|
b.m.show(menus[i].Items)
|
|
}
|
|
// The title keeps the focus and parks it while off.
|
|
if off && b.open >= 0 {
|
|
b.open = -1
|
|
}
|
|
if b.open >= 0 && !b.m.open {
|
|
b.open = -1
|
|
}
|
|
n := len(menus)
|
|
// Left and Right hand an open menu to its neighbor.
|
|
if b.open >= 0 {
|
|
left, right := pixel.Shortcut(row, 0, ui.KeyLeft), pixel.Shortcut(row, 0, ui.KeyRight)
|
|
switch {
|
|
case left:
|
|
open((b.open + n - 1) % n)
|
|
case right:
|
|
open((b.open + 1) % n)
|
|
}
|
|
}
|
|
// Only a moving pointer hands the bar to another title.
|
|
px, py, over := row.PointerPosition()
|
|
moved := over && [2]float32{px, py} != b.ptr
|
|
b.ptr = [2]float32{px, py}
|
|
var anchor *ui.Element
|
|
row.Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
g.Rect(0, 0, g.W, g.H, pixel.Face)
|
|
g.HLine(0, g.H-1, g.W, pixel.Edge)
|
|
}).Children(func() {
|
|
for i, mn := range menus {
|
|
t := input.Button(c, mn.Title, input.ButtonOptions{Variant: input.Ghost, Size: input.Small, Disabled: o.Disabled})
|
|
if _, _, on := t.Element.PointerPosition(); on && moved && b.open >= 0 && i != b.open {
|
|
open(i)
|
|
}
|
|
if i == b.open {
|
|
anchor = t.Element
|
|
}
|
|
switch {
|
|
case t.Clicked && b.open == i:
|
|
b.open = -1
|
|
case t.Clicked:
|
|
open(i)
|
|
anchor = t.Element
|
|
case b.open < 0 && !off && t.Element.FocusWithin() && pixel.Shortcut(row, 0, ui.KeyDown):
|
|
open(i)
|
|
anchor = t.Element
|
|
}
|
|
}
|
|
})
|
|
if b.open >= 0 && anchor != nil {
|
|
items := menus[b.open].Items
|
|
res.Item = b.m.keys(row.Shortcut, items)
|
|
if _, clicked := b.m.list(c, anchor, items); clicked >= 0 {
|
|
res.Item = clicked
|
|
}
|
|
if res.Item >= 0 {
|
|
res.Menu = b.open
|
|
b.open = -1
|
|
}
|
|
}
|
|
if res.Item >= 0 {
|
|
slog.Debug("8bit: menu bar", "menu", menus[res.Menu].Title, "item", menus[res.Menu].Items[res.Item].Label)
|
|
}
|
|
return res
|
|
}
|