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.
119 lines
3.2 KiB
Go
119 lines
3.2 KiB
Go
package navigation
|
|
|
|
import (
|
|
"log/slog"
|
|
"strings"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
type CommandPaletteOptions struct {
|
|
Disabled bool
|
|
}
|
|
|
|
type CommandPaletteResult struct {
|
|
Element *ui.Element
|
|
// Ran is the command run this frame, or -1.
|
|
Ran int
|
|
}
|
|
|
|
type paletteKey struct{}
|
|
|
|
// palette is the query, lit match and first shown row.
|
|
type palette struct {
|
|
query string
|
|
lit, top int
|
|
}
|
|
|
|
// paletteRows is how many matches show at once.
|
|
const paletteRows = 8
|
|
|
|
// CommandPalette filters commands as you type; Cmd+K opens it.
|
|
func CommandPalette(c *ui.Context, text string, commands []MenuItem, open *bool, o CommandPaletteOptions) CommandPaletteResult {
|
|
k := pixel.K(c)
|
|
off := o.Disabled || pixel.Current(c).Inert
|
|
if !off && c.Shortcut(ui.Cmd, ui.KeyK) {
|
|
*open = !*open
|
|
}
|
|
if off {
|
|
*open = false
|
|
}
|
|
home := ui.Box(c)
|
|
st := ui.Local(home, paletteKey{}, func() palette { return palette{} })
|
|
res := CommandPaletteResult{Element: home, Ran: -1}
|
|
if !*open {
|
|
*st = palette{}
|
|
return res
|
|
}
|
|
shown := filter(commands, st.query)
|
|
run := func(i int) {
|
|
if !commands[i].Disabled {
|
|
res.Ran, *open = i, false
|
|
}
|
|
}
|
|
res.Element = ui.DialogBase(c, open, func(back, panel *ui.Element) {
|
|
back.Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
g.Checker(0, 0, g.W, g.H, pixel.Back)
|
|
})
|
|
panel.Width(k.D(240)).Padding(k.D(6)).Gap(k.D(4)).Label(text).Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
g.Frame(0, 0, g.W, g.H, pixel.Window, pixel.Face)
|
|
}).Children(func() {
|
|
f := input.TextInput(c, text, &st.query, input.TextInputOptions{Placeholder: "> …", Width: 228})
|
|
f.Input.AutoFocus()
|
|
if f.Changed {
|
|
st.lit = 0
|
|
}
|
|
if pixel.Shortcut(f.Element, 0, ui.KeyDown) {
|
|
st.lit++
|
|
}
|
|
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 pixel.Shortcut(f.Element, 0, ui.KeyEnter) && len(shown) > 0 {
|
|
run(shown[st.lit])
|
|
}
|
|
ui.Column(c).AlignItems(ui.Stretch).Children(func() {
|
|
for j := st.top; j < min(len(shown), st.top+paletteRows); j++ {
|
|
it, lit := commands[shown[j]], j == st.lit
|
|
row := ui.Row(c).Role(ui.RoleListItem).Label(it.Label).Height(k.D(pixel.LineHeight))
|
|
if it.Disabled {
|
|
row.Description(k.Msg("disabled"))
|
|
}
|
|
if row.Clicked() {
|
|
run(shown[j])
|
|
}
|
|
row.Draw(func(p *ui.Painter, r ui.Rect) { menuRow(k.Canvas(p, r), it, lit) })
|
|
}
|
|
})
|
|
})
|
|
})
|
|
if res.Ran >= 0 {
|
|
slog.Debug("8bit: command palette", "label", text, "ran", commands[res.Ran].Label)
|
|
}
|
|
return res
|
|
}
|
|
|
|
// filter indexes commands holding q, prefix matches first.
|
|
func filter(commands []MenuItem, q string) []int {
|
|
q = strings.ToLower(strings.TrimSpace(q))
|
|
var starts, holds []int
|
|
for i, it := range commands {
|
|
switch l := strings.ToLower(it.Label); {
|
|
case it.Separator:
|
|
case strings.HasPrefix(l, q):
|
|
starts = append(starts, i)
|
|
case strings.Contains(l, q):
|
|
holds = append(holds, i)
|
|
}
|
|
}
|
|
return append(starts, holds...)
|
|
}
|