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.
82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
package input
|
|
|
|
import (
|
|
"log/slog"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/display"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
type TokenFieldOptions struct {
|
|
Placeholder string
|
|
// Width is in dots; zero means 200.
|
|
Width int
|
|
Disabled bool
|
|
}
|
|
|
|
type TokenFieldResult struct {
|
|
Element *ui.Element
|
|
Changed bool
|
|
}
|
|
|
|
type tokenKey struct{}
|
|
|
|
// editorSlot keys the editor so added tags keep its focus.
|
|
type editorSlot struct{}
|
|
|
|
// TokenField shows tokens as tags; Enter or comma adds.
|
|
func TokenField(c *ui.Context, text string, tokens *[]string, o TokenFieldOptions) TokenFieldResult {
|
|
k := pixel.K(c)
|
|
e := ui.Row(c).Wrap().Width(k.D(pixel.Or("TokenField Width", o.Width, 200))).Padding(k.D(3), k.D(4)).Gap(k.D(3)).AlignItems(ui.Center)
|
|
e.Role(ui.RoleGroup).Label(text).Description(strings.Join(*tokens, ", "))
|
|
draft := ui.Local(e, tokenKey{}, func() string { return "" })
|
|
// The row stays enabled, so off is checked here.
|
|
off := o.Disabled || pixel.Current(c).Inert
|
|
res := TokenFieldResult{Element: e}
|
|
add := func(s string) {
|
|
if s = strings.TrimSpace(s); s != "" && !slices.Contains(*tokens, s) {
|
|
*tokens, res.Changed = append(*tokens, s), true
|
|
}
|
|
}
|
|
// A comma typed since the last frame cuts the draft.
|
|
if parts := strings.Split(*draft, ","); len(parts) > 1 && !off {
|
|
for _, p := range parts[:len(parts)-1] {
|
|
add(p)
|
|
}
|
|
*draft = parts[len(parts)-1]
|
|
}
|
|
if !off && pixel.Shortcut(e, 0, ui.KeyEnter) {
|
|
add(*draft)
|
|
*draft = ""
|
|
}
|
|
if !off && *draft == "" && len(*tokens) > 0 && pixel.Shortcut(e, 0, ui.KeyBackspace) {
|
|
*tokens, res.Changed = (*tokens)[:len(*tokens)-1], true
|
|
}
|
|
var ed pixel.Edit
|
|
e.Children(func() {
|
|
for i := 0; i < len(*tokens); i++ {
|
|
tok, removed := (*tokens)[i], false
|
|
ui.Box(c).Key(tok).Children(func() {
|
|
removed = display.Tag(c, tok, display.TagOptions{Removable: true, Disabled: o.Disabled}).Removed
|
|
})
|
|
if removed {
|
|
*tokens, res.Changed = slices.Delete(*tokens, i, i+1), true
|
|
i--
|
|
}
|
|
}
|
|
ui.Box(c).Key(editorSlot{}).Grow(1).MinWidth(k.D(40)).Children(func() {
|
|
ed = pixel.Editor(c, draft, pixel.EditorOptions{Label: text, Placeholder: o.Placeholder, Disabled: o.Disabled})
|
|
})
|
|
})
|
|
focus := ed.Focused
|
|
e.Draw(func(p *ui.Painter, r ui.Rect) { well(k.Canvas(p, r), pixel.Edge, focus) })
|
|
if res.Changed {
|
|
slog.Debug("8bit: tokens", "label", text, "tokens", len(*tokens))
|
|
}
|
|
return res
|
|
}
|