Files
MujicaUI/input/signature_pad.go
ZacharyZhang-NYandClaude Opus 5.5 bcce34cda1 Upgrade MyGo to v0.5.5
MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:

- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
  elements expire and Context methods are inert: read IsDisabled into a
  local during the build, keep a ui.Handle for Focus, use c.Services()
  for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
  content builds, so same-frame autofocus lost focus restore on close.
  It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
  c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
  split button tooltip no longer reorders its open menu; disabled
  menubar rows pass presses to the panel; Form defers blur-started
  async checks so the growing message no longer moves Submit away
  from the release; Combobox drops its disabled copy now that MyGo
  blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
  time.Sleep/time.Now and read element state inside the view. The web
  mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.

Snapshot goldens are darwin-only and were not re-verified here.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:37:54 -04:00

157 lines
4.8 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package input
import (
"cmp"
"log/slog"
"math"
"strconv"
"strings"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// SignaturePoint is a point of a stroke, in DIPs from the pad's top left.
type SignaturePoint struct{ X, Y float32 }
// SignaturePadOptions configure a SignaturePad.
type SignaturePadOptions struct {
// Height is the drawing area's height; zero is 160. The pad fills its parent's width.
Height float32
// Label names the pad; empty uses "Signature".
Label string
Disabled bool
}
// SignaturePath writes strokes as SVG path data: "M x y L x y …" per stroke.
func SignaturePath(strokes [][]SignaturePoint) string {
var b strings.Builder
num := func(v float32) string { return strconv.FormatFloat(float64(v), 'f', -1, 32) }
for _, st := range strokes {
for i, p := range st {
if b.Len() > 0 {
b.WriteByte(' ')
}
if i == 0 {
b.WriteString("M ")
} else {
b.WriteString("L ")
}
b.WriteString(num(p.X) + " " + num(p.Y))
}
}
return b.String()
}
// signaturePadDrawing is the stroke in progress.
type signaturePadDrawing struct {
active, ended bool
pad ui.Handle
}
// SignaturePad records pointer strokes into *strokes; Changed reports a finished stroke or a clear.
func SignaturePad(c *ui.Context, strokes *[][]SignaturePoint, opts SignaturePadOptions) core.ChoiceResult {
if strokes == nil {
panic("mujica: SignaturePad needs strokes")
}
if !(opts.Height >= 0) || math.IsInf(float64(opts.Height), 1) {
panic("mujica: SignaturePad Height must be finite and >= 0")
}
k := core.Tokens(c)
u := core.Density(c).Unit()
name := cmp.Or(opts.Label, core.Msg(c, "signature_pad.label"))
col := ui.Column(c).FillWidth().Gap(u * 2).Disabled(opts.Disabled)
disabled := col.IsDisabled()
changed := false
col.Children(func() {
pad := ui.Box(c).FillWidth().Height(cmp.Or(opts.Height, 160)).Focusable().Radius(theme.CardRadius).Clip().
Background(k.Surface).Border(theme.BorderWidth, k.ControlBorder).Cursor(ui.CursorCrosshair).
Role(ui.RoleImage).Label(name).Description(core.Msg(c, "signature_pad.hint"))
internal.Ring(pad, k.Focus, theme.CardRadius)
d := ui.Local(pad, "signature_pad", func() *signaturePadDrawing { return &signaturePadDrawing{} })
s := *d
pad.Bind(&s.pad)
pad.HandleInput(func(ev ui.InputEvent) bool {
if disabled {
return false
}
pt := SignaturePoint{ev.X, ev.Y}
switch {
case ev.Kind == ui.InputPointerDown && ev.Button == 0:
*strokes = append(*strokes, []SignaturePoint{pt})
s.active = true
s.pad.Focus()
case ev.Kind == ui.InputPointerMove && s.active && len(*strokes) > 0:
last := &(*strokes)[len(*strokes)-1]
if q := (*last)[len(*last)-1]; math.Hypot(float64(pt.X-q.X), float64(pt.Y-q.Y)) >= 1 {
*last = append(*last, pt)
}
case ev.Kind == ui.InputPointerUp && s.active:
s.active, s.ended = false, true
default:
return false
}
return true
})
if s.ended {
s.ended, changed = false, true
slog.Debug("mujica: signature_pad stroke", "strokes", len(*strokes))
}
clear := func() {
if len(*strokes) > 0 {
*strokes, changed = nil, true
slog.Debug("mujica: signature_pad clear")
}
}
if !disabled && (pad.Shortcut(0, ui.KeyDelete) || pad.Shortcut(0, ui.KeyBackspace)) {
clear()
}
ink := signaturePadClone(*strokes)
empty := len(ink) == 0
size := core.FontSize(c, theme.CaptionSize)
pad.Draw(func(p *ui.Painter, r ui.Rect) {
line := r.Y + r.H - 28
p.Fill(ui.Rect{X: r.X + 16, Y: line, W: r.W - 32, H: 1}, k.Border, 0)
p.Text(r.X+16, line+4, "×", size, k.TextMuted)
if empty {
w, _ := p.MeasureText(0, ui.Span{Text: core.Msg(c, "signature_pad.empty"), Size: size})
p.Text(r.X+(r.W-w)/2, r.Y+r.H/2-size, core.Msg(c, "signature_pad.empty"), size, k.TextMuted)
}
for _, st := range ink {
var path ui.Path
for i, q := range st {
x, y := internal.Snap(p, r.X+q.X), internal.Snap(p, r.Y+q.Y)
if i == 0 {
path.MoveTo(x, y)
} else {
path.LineTo(x, y)
}
}
if len(st) == 1 {
path.Circle(internal.Snap(p, r.X+st[0].X), internal.Snap(p, r.Y+st[0].Y), 1)
}
p.StrokePath(&path, 2, k.Text)
}
})
ui.Row(c).Children(func() {
if Button(c, core.Msg(c, "signature_pad.clear"), ButtonOptions{Variant: Ghost, Icon: icons.Must("trash-2"), Disabled: empty}).Clicked() && !disabled {
clear()
}
})
})
return core.NewChoiceResult(col, changed)
}
// signaturePadClone copies strokes so painting never sees later edits.
func signaturePadClone(s [][]SignaturePoint) [][]SignaturePoint {
out := make([][]SignaturePoint, len(s))
for i, st := range s {
out[i] = append([]SignaturePoint(nil), st...)
}
return out
}