Files
8bitMyGoUI/input/pininput.go
T
ZacharyZhang-NY c745b082a9 Add account components 386-405; Reduce setting, PinInput Cmd+V
Twenty account components: SettingsList, OptionCycler, KeyBindings,
ControllerMap, ProfileCard, CharacterCreator, LoginForm, PasswordGrid,
SessionList, LanguagePicker, PalettePicker, VolumeMixer,
DifficultySelect, AccessibilityOptions, AccountSwitcher, PlayerSelect,
TwoFactor, About, PatchNotes and FeedbackForm.

The components never check credentials, and no secret reaches the log:
LoginForm reports Submitted and the caller counts Failures; each new
failure clears the password, refocuses it, shakes the form and names no
field. TwoFactor builds on input.PinInput and drains an epoch-aligned
period bar. PalettePicker draws each tile in its own palette on a
pixel.Pad grid.

pixel.Settings.Reduce stills motion like the system preference, the
micro font gains '?', display.Face draws avatar faces anywhere, and
input.PinInput pastes on Cmd+V as native hosts send it.
2026-10-12 08:21:39 -04:00

84 lines
2.3 KiB
Go

package input
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type PinInputOptions struct {
// Length is the digit count; zero means 4.
Length int
Disabled bool
}
type PinInputResult struct {
Element *ui.Element
// Complete reports the last box filled.
Changed, Complete bool
}
type pinKey struct{}
const pinBox, pinGap = 12, 4
// PinInput types digits into boxes; Backspace goes back, paste fills.
func PinInput(c *ui.Context, text string, value *string, o PinInputOptions) PinInputResult {
k := pixel.K(c)
n := pixel.Or("PinInput Length", o.Length, 4)
shell, base, st := pixel.Shell(c, func() *ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleTextField).Label(text).Description(*value).Disabled(o.Disabled)
})
changed := ui.Local(base, pinKey{}, func() bool { return false })
put := func(s string) {
for _, r := range s {
if r >= '0' && r <= '9' && len(*value) < n {
*value += string(r)
*changed = true
}
}
}
base.HandleInput(func(ev ui.InputEvent) bool {
switch {
case ev.Kind == ui.InputText:
put(ev.Text)
// Browsers paste by command, native hosts by Cmd+V.
case ev.Kind == ui.InputCommand && ev.Text == "paste", ev.Kind == ui.InputKeyDown && ev.Key == ui.KeyV && ev.Mods == ui.Cmd:
put(c.ReadClipboard())
case ev.Kind == ui.InputKeyDown && ev.Key == ui.KeyBackspace && ev.Mods == 0:
if len(*value) > 0 {
*value, *changed = (*value)[:len(*value)-1], true
}
default:
return false
}
return true
})
h := pixel.LineHeight + 4
base.TextCaret(ui.Rect{X: k.D(len(*value) * (pinBox + pinGap)), W: k.D(1), H: k.D(h)})
res := PinInputResult{Element: shell, Changed: *changed, Complete: *changed && len(*value) == n}
*changed = false
focused := base.Focused()
shell.Size(k.D(n*(pinBox+pinGap)-pinGap), k.D(h)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
for i := range n {
x := i * (pinBox + pinGap)
g.Frame(x, 0, pinBox, h, pixel.Sunken, pixel.Well)
switch {
case i < len(*value):
d := (*value)[i : i+1]
g.Text(x+(pinBox-pixel.Measure(d))/2, 2, d, pixel.Ink, 1)
case i == len(*value) && focused:
g.HLine(x+2, h-4, pinBox-4, pixel.Accent)
}
}
g.Finish(st)
})
if res.Complete {
slog.Debug("8bit: pin complete", "label", text)
}
return res
}