Files
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

115 lines
3.4 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 data
import (
"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/theme"
)
// StatisticOptions configure a Statistic. It formats values; it computes none.
type StatisticOptions struct {
Title string
// Precision is the number of decimals, 0 or more.
Precision int
// Prefix and Suffix wrap the number, as "$" or "×"; Unit follows it, smaller.
Prefix, Suffix, Unit string
// Format replaces the built-in grouped decimal formatting.
Format func(v float64) string
// Change, when set, shows the caller's change with a sign and an arrow.
Change *float64
ChangeUnit string
// ChangeNote explains the change, as "vs last week".
ChangeNote string
}
// Statistic shows a headline number with its title, unit and change.
func Statistic(c *ui.Context, value float64, opts StatisticOptions) ui.Element {
if opts.Precision < 0 {
panic("mujica: Statistic with negative Precision")
}
k := core.Tokens(c)
u := core.Density(c).Unit()
format := opts.Format
if format == nil {
format = func(v float64) string { return StatisticFormat(v, opts.Precision) }
}
num := opts.Prefix + format(value) + opts.Suffix
spoken := strings.TrimSpace(opts.Title + " " + num + " " + opts.Unit)
e := ui.Column(c).Gap(u).Role(ui.RoleGroup)
e.Children(func() {
if opts.Title != "" {
ui.Text(c, opts.Title).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted)
}
ui.Row(c).Gap(u).AlignItems(ui.End).Children(func() {
ui.Text(c, num).Font(theme.SerifFont).FontSize(core.FontSize(c, theme.H1Size)).FontWeight(600).LineHeight(1.2).SingleLine()
if opts.Unit != "" {
ui.Text(c, opts.Unit).TextColor(k.TextMuted).Padding(0, 0, u, 0)
}
})
if opts.Change == nil {
return
}
ch := *opts.Change
col, icon, word := k.TextMuted, "minus", core.Msg(c, "statistic.flat")
switch {
case ch > 0:
col, icon, word = k.Success, "arrow-up", core.Msg(c, "statistic.up")
case ch < 0:
col, icon, word = k.Danger, "arrow-down", core.Msg(c, "statistic.down")
}
text := StatisticSigned(ch, opts.Precision) + opts.ChangeUnit
spoken += ", " + word + " " + text
ui.Row(c).Gap(u).AlignItems(ui.Center).Wrap().Children(func() {
ui.Row(c).Gap(u / 2).AlignItems(ui.Center).TextColor(col).Children(func() {
ui.Icon(c, icons.Must(icon)).Size(14, 14)
ui.Text(c, text).FontWeight(600).SingleLine()
})
if opts.ChangeNote != "" {
ui.Text(c, opts.ChangeNote).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted)
}
})
})
return e.Label(spoken)
}
// StatisticFormat writes v with p decimals and comma-grouped thousands.
func StatisticFormat(v float64, p int) string {
if math.IsNaN(v) || math.IsInf(v, 0) {
return strconv.FormatFloat(v, 'f', -1, 64)
}
s := strconv.FormatFloat(math.Abs(v), 'f', p, 64)
whole, frac, _ := strings.Cut(s, ".")
var b strings.Builder
if v < 0 && strings.Trim(s, "0.") != "" {
b.WriteString("−")
}
for i, r := range whole {
if i > 0 && (len(whole)-i)%3 == 0 {
b.WriteByte(',')
}
b.WriteRune(r)
}
if frac != "" {
b.WriteString("." + frac)
}
return b.String()
}
// StatisticSigned writes a change with an explicit sign.
func StatisticSigned(v float64, p int) string {
s := StatisticFormat(math.Abs(v), p)
switch {
case v > 0:
return "+" + s
case v < 0:
return "−" + s
}
return s
}