Files
MujicaUI/finance/market_overview.go
T
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

94 lines
2.9 KiB
Go

package finance
import (
"fmt"
"log/slog"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// MarketBreadth counts the issues that rose, fell and stayed flat.
type MarketBreadth struct{ Advancing, Declining, Unchanged int }
// MarketOverviewOptions configure a MarketOverview.
type MarketOverviewOptions struct {
Breadth MarketBreadth
AsOf time.Time
Decimals int
Disabled bool
}
type MarketOverviewView struct {
Element ui.Element
Chosen int
}
func MarketOverview(c *ui.Context, indices []IndexQuote, opts MarketOverviewOptions) MarketOverviewView {
b := opts.Breadth
if b.Advancing < 0 || b.Declining < 0 || b.Unchanged < 0 {
panic("mujica: MarketOverview breadth counts must not be negative")
}
k := core.Tokens(c)
u := core.Density(c).Unit()
v := MarketOverviewView{Chosen: -1}
v.Element = ui.Column(c).Gap(u * 3).Disabled(opts.Disabled)
v.Element.Children(func() {
ui.Row(c).Wrap().Gap(u * 3).Children(func() {
for i, q := range indices {
ui.Column(c.Key(q.Name)).Children(func() {
if IndexCard(c, q, IndexCardOptions{Decimals: opts.Decimals, Clickable: true, Disabled: opts.Disabled}).Changed() {
v.Chosen = i
}
})
}
})
if total := b.Advancing + b.Declining + b.Unchanged; total > 0 {
marketBreadthBar(c, b, total)
}
if !opts.AsOf.IsZero() {
ui.Text(c, fmt.Sprintf(core.Msg(c, "market_overview.as_of"), opts.AsOf.Format("2006-01-02 15:04"))).
FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted)
}
})
if v.Chosen >= 0 {
slog.Debug("mujica: market_overview choose", "index", indices[v.Chosen].Name)
}
return v
}
func marketBreadthBar(c *ui.Context, b MarketBreadth, total int) {
k := core.Tokens(c)
u := core.Density(c).Unit()
up, down := core.TrendColor(c, 1), core.TrendColor(c, -1)
label := fmt.Sprintf(core.Msg(c, "market_overview.breadth"), b.Advancing, b.Declining, b.Unchanged)
ui.Column(c).Gap(u).MaxWidth(640).FillWidth().Role(ui.RoleMeter).Label(label).Children(func() {
ui.Box(c).FillWidth().Height(10).Draw(func(p *ui.Painter, r ui.Rect) {
x := r.X
for _, part := range []struct {
n int
col ui.Color
}{{b.Advancing, up}, {b.Unchanged, k.Border}, {b.Declining, down}} {
w := r.W * float32(part.n) / float32(total)
p.Fill(ui.Rect{X: x, Y: r.Y, W: max(w-1, 0), H: r.H}, part.col, 1)
x += w
}
})
ui.Row(c).Gap(u * 4).Children(func() {
for _, part := range []struct {
n int
sign int
key string
}{{b.Advancing, 1, "market_overview.up"}, {b.Unchanged, 0, "market_overview.flat"}, {b.Declining, -1, "market_overview.down"}} {
ui.Row(c).Gap(u).AlignItems(ui.Center).TextColor(core.TrendColor(c, part.sign)).Children(func() {
ui.Icon(c, core.TrendIcon(part.sign)).Size(12, 12)
ui.Text(c, fmt.Sprintf(core.Msg(c, part.key), part.n)).FontSize(core.FontSize(c, theme.CaptionSize)).FontFeatures("tnum")
})
}
})
})
}