Files
MujicaUI/finance/margin_indicator.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

74 lines
3.0 KiB
Go

package finance
import (
"fmt"
"math"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/data"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// MarginIndicatorOptions configure a MarginIndicator. Zero lines mean 80% and 100%.
type MarginIndicatorOptions struct {
// Warning and Liquidation are margin ratios, Warning below Liquidation.
Warning, Liquidation float64
// Label names the indicator; empty uses the built-in "Margin ratio".
Label string
}
// MarginIndicator shows maintenance over equity against its two lines.
func MarginIndicator(c *ui.Context, ratio float64, opts MarginIndicatorOptions) ui.Element {
warn, liq := opts.Warning, opts.Liquidation
if warn == 0 && liq == 0 {
warn, liq = 0.8, 1
}
if !(warn > 0 && warn < liq) || ratio < 0 || math.IsNaN(ratio) {
panic(fmt.Sprintf("mujica: MarginIndicator ratio %v, lines %v and %v: need 0 < Warning < Liquidation", ratio, warn, liq))
}
k := core.Tokens(c)
u := core.Density(c).Unit()
label := opts.Label
if label == "" {
label = core.Msg(c, "margin_indicator.label")
}
status, col, icon := core.Msg(c, "margin_indicator.safe"), k.Success, "circle-check"
switch {
case ratio >= liq:
status, col, icon = core.Msg(c, "margin_indicator.liquidation"), k.Danger, "circle-x"
case ratio >= warn:
status, col, icon = core.Msg(c, "margin_indicator.warning"), k.Warning, "triangle-alert"
}
pct := func(v float64) string { return data.StatisticFormat(v*100, 1) + "%" }
top := math.Max(liq*1.25, ratio)
e := ui.Column(c).Gap(u * 2).MinWidth(220).Role(ui.RoleMeter).Label(label).
Description(fmt.Sprintf(core.Msg(c, "margin_indicator.spoken"), pct(ratio), status, pct(warn), pct(liq)))
e.Children(func() {
ui.Row(c).Gap(u * 2).AlignItems(ui.Center).Children(func() {
ui.Text(c, pct(ratio)).FontSize(core.FontSize(c, theme.H3Size)).FontFeatures("tnum").FontWeight(600).SingleLine()
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, status).FontSize(core.FontSize(c, theme.CaptionSize)).FontWeight(600).SingleLine()
})
})
ui.Box(c).Height(14).FillWidth().Draw(func(p *ui.Painter, r ui.Rect) {
bar := ui.Rect{X: r.X, Y: r.Y + 3, W: r.W, H: 8}
p.Fill(bar, k.SurfaceHover, theme.ControlRadius)
p.Fill(ui.Rect{X: bar.X, Y: bar.Y, W: bar.W * float32(ratio/top), H: bar.H}, col, theme.ControlRadius)
// The liquidation line is drawn heavier than the warning line.
for i, line := range []float64{warn, liq} {
x := bar.X + bar.W*float32(line/top)
p.Line(x, r.Y, x, r.Y+r.H, 1.5+float32(i)*1.5, k.Text)
}
})
ui.Row(c).Gap(u * 4).Wrap().Children(func() {
ui.Text(c, core.Msg(c, "margin_indicator.warning_line")+" "+pct(warn)).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted)
ui.Text(c, core.Msg(c, "margin_indicator.liquidation_line")+" "+pct(liq)).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted)
})
})
return e
}