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>
85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
package finance
|
|
|
|
import (
|
|
"log/slog"
|
|
"time"
|
|
|
|
"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/theme"
|
|
)
|
|
|
|
// priceTextFlash is the tint's fade, three state steps long.
|
|
const priceTextFlash = 3 * theme.StateDuration
|
|
|
|
// PriceTextOptions configure a PriceText.
|
|
type PriceTextOptions struct {
|
|
// Decimals shown; zero means 2.
|
|
Decimals int
|
|
// NoGrouping drops the thousands separators.
|
|
NoGrouping bool
|
|
// Compare marks the price's move from Reference.
|
|
Compare bool
|
|
Reference float64
|
|
// NoFlash keeps the price still when it changes.
|
|
NoFlash bool
|
|
// Size is the font size; zero is the body size.
|
|
Size float32
|
|
}
|
|
|
|
type priceTextState struct {
|
|
last float64
|
|
seen bool
|
|
at time.Time
|
|
dir int
|
|
}
|
|
|
|
// PriceText shows a price; a change tints it once.
|
|
func PriceText(c *ui.Context, value float64, opts PriceTextOptions) ui.Element {
|
|
dec := core.PriceDecimals("PriceText", opts.Decimals)
|
|
if opts.Size < 0 {
|
|
panic("mujica: PriceText with negative Size")
|
|
}
|
|
size := opts.Size
|
|
if size == 0 {
|
|
size = theme.BodySize
|
|
}
|
|
text := data.StatisticFormat(value, dec)
|
|
if opts.NoGrouping {
|
|
text = core.PriceUngrouped(value, dec)
|
|
}
|
|
e := ui.Row(c).Gap(core.Density(c).Unit()).AlignItems(ui.Center).Padding(0, 2).Radius(theme.ControlRadius).Shrink(0)
|
|
st := ui.Local(e, "price", func() priceTextState { return priceTextState{} })
|
|
if st.seen && value != st.last {
|
|
st.at, st.dir = c.Now(), core.TrendSign(value-st.last)
|
|
slog.Debug("mujica: price_text changed", "dir", st.dir)
|
|
}
|
|
st.last, st.seen = value, true
|
|
sign := 0
|
|
if opts.Compare {
|
|
sign = core.TrendSign(value - opts.Reference)
|
|
}
|
|
col := core.Tokens(c).Text
|
|
spoken := text
|
|
if opts.Compare {
|
|
col = core.TrendColor(c, sign)
|
|
spoken += ", " + core.TrendWord(c, sign)
|
|
}
|
|
if d := core.Motion(c, priceTextFlash); d > 0 && !opts.NoFlash && !st.at.IsZero() {
|
|
if t := c.Now().Sub(st.at); t < d {
|
|
e.Background(core.TrendColor(c, st.dir).Alpha(0.2 * float32(1-float64(t)/float64(d))))
|
|
c.AnimationFrame()
|
|
}
|
|
}
|
|
e.Role(ui.RoleText).Label(spoken).TextColor(col)
|
|
e.Children(func() {
|
|
if opts.Compare {
|
|
ui.Icon(c, core.TrendIcon(sign)).Size(size*0.85, size*0.85)
|
|
}
|
|
ui.Text(c, text).FontSize(core.FontSize(c, size)).FontFeatures("tnum").FontWeight(500).SingleLine()
|
|
})
|
|
return e
|
|
}
|