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>
153 lines
5.1 KiB
Go
153 lines
5.1 KiB
Go
package finance
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"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/input"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
|
|
)
|
|
|
|
type SymbolSearchState struct {
|
|
Query, Market string
|
|
focusFirst bool
|
|
cursor int
|
|
}
|
|
|
|
// SymbolSearchOptions configure a SymbolSearch.
|
|
type SymbolSearchOptions struct {
|
|
// Markets are the filter choices; nil hides the filter.
|
|
Markets []string
|
|
// Recent shows while the query is empty.
|
|
Recent []MarketSymbol
|
|
// Loading and Error are the caller's search for the query.
|
|
Loading bool
|
|
Error string
|
|
Disabled bool
|
|
}
|
|
|
|
// SymbolSearchView is a built SymbolSearch and this frame's events.
|
|
type SymbolSearchView struct {
|
|
Element ui.Element
|
|
Changed bool
|
|
// Chosen is the symbol picked when Picked is set.
|
|
Chosen MarketSymbol
|
|
Picked bool
|
|
Retry bool
|
|
}
|
|
|
|
func SymbolSearch(c *ui.Context, s *SymbolSearchState, results []MarketSymbol, opts SymbolSearchOptions) SymbolSearchView {
|
|
if s == nil {
|
|
panic("mujica: SymbolSearch needs a state")
|
|
}
|
|
k := core.Tokens(c)
|
|
u := core.Density(c).Unit()
|
|
var v SymbolSearchView
|
|
v.Element = ui.Column(c).Gap(u * 2).Disabled(opts.Disabled)
|
|
opts.Disabled = v.Element.IsDisabled()
|
|
if opts.Disabled {
|
|
copy := *s
|
|
s = ©
|
|
}
|
|
v.Element.Children(func() {
|
|
ui.Row(c).Gap(u * 2).AlignItems(ui.Center).Children(func() {
|
|
in := input.SearchInput(c, &s.Query, input.SearchInputOptions{Placeholder: core.Msg(c, "symbol_search.placeholder"), Disabled: opts.Disabled})
|
|
in.Element.Grow(1)
|
|
v.Changed = in.Changed()
|
|
if in.Input.Shortcut(0, ui.KeyDown) && !in.Input.IsDisabled() {
|
|
s.focusFirst = true
|
|
}
|
|
if opts.Markets != nil {
|
|
items := []input.SelectOption[string]{{Value: "", Label: core.Msg(c, "symbol_search.all_markets")}}
|
|
for _, m := range opts.Markets {
|
|
items = append(items, input.SelectOption[string]{Value: m, Label: m})
|
|
}
|
|
r := input.Select(c, &s.Market, items, input.SelectOptions{Label: core.Msg(c, "symbol_search.market"), Disabled: opts.Disabled})
|
|
r.Element.MinWidth(140)
|
|
v.Changed = v.Changed || r.Changed()
|
|
}
|
|
})
|
|
list, title := results, ""
|
|
switch {
|
|
case opts.Error != "":
|
|
ui.Row(c).Gap(u * 2).AlignItems(ui.Center).TextColor(k.Danger).Children(func() {
|
|
ui.Icon(c, icons.Must("circle-alert")).Size(16, 16)
|
|
ui.Text(c, opts.Error).Role(ui.RoleStatus).Grow(1)
|
|
v.Retry = input.Button(c, core.Msg(c, "symbol_search.retry"), input.ButtonOptions{Variant: input.Secondary}).Clicked()
|
|
})
|
|
return
|
|
case opts.Loading && len(results) == 0:
|
|
core.Spinner(c, core.SpinnerOptions{Label: core.Msg(c, "symbol_search.searching"), ShowLabel: true})
|
|
return
|
|
case s.Query == "":
|
|
list, title = opts.Recent, core.Msg(c, "symbol_search.recent")
|
|
case len(results) == 0:
|
|
ui.Text(c, core.Msg(c, "symbol_search.no_results")).TextColor(k.TextMuted).Role(ui.RoleStatus)
|
|
return
|
|
}
|
|
if title != "" && len(list) > 0 {
|
|
ui.Text(c, title).Font(theme.SerifFont).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted)
|
|
}
|
|
rows := ui.Local(v.Element, "mujica.symbol.rows", func() ui.ListState { return ui.ListState{} })
|
|
rows.Key = func(i int) any { return list[i].Market + ":" + list[i].Code }
|
|
rows.Label = func(i int) string { return symbolBadgeName(list[i]) }
|
|
rows.Selected = &s.cursor
|
|
if s.focusFirst {
|
|
s.cursor = 0
|
|
rows.ScrollTo(0, ui.Start)
|
|
}
|
|
restore := data.DataSelectTheme(c, 0)
|
|
resultList := ui.List(c, rows, len(list), func(i int) {
|
|
if symbolSearchRow(c, list[i], i == s.cursor) {
|
|
s.cursor = i
|
|
v.Chosen, v.Picked = list[i], true
|
|
}
|
|
}).Height(320).MaxHeight(320)
|
|
restore()
|
|
internal.Ring(resultList, k.Focus, theme.ControlRadius)
|
|
if s.focusFirst || v.Picked {
|
|
resultList.Focus()
|
|
}
|
|
if !resultList.IsDisabled() && resultList.Submitted() && s.cursor >= 0 && s.cursor < len(list) {
|
|
v.Chosen, v.Picked = list[s.cursor], true
|
|
}
|
|
|
|
s.focusFirst = false
|
|
})
|
|
if v.Element.IsDisabled() {
|
|
v.Changed, v.Picked, v.Retry = false, false, false
|
|
}
|
|
if v.Picked {
|
|
slog.Debug("mujica: symbol_search pick", "code", v.Chosen.Code)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func symbolSearchRow(c *ui.Context, sym MarketSymbol, selected bool) bool {
|
|
k := core.Tokens(c)
|
|
d := core.Density(c)
|
|
b := ui.ButtonBase(c.Key(sym.Market+":"+sym.Code)).MinHeight(d.ControlHeight()).Padding(0, d.Unit()*2).Gap(d.Unit() * 3).
|
|
Justify(ui.Start).Radius(theme.ControlRadius).Label(symbolBadgeName(sym)).Role(ui.RoleListItem)
|
|
internal.Ring(b, k.Focus, theme.ControlRadius)
|
|
|
|
if b.Hovered() {
|
|
b.Background(k.SurfaceHover)
|
|
}
|
|
b.Children(func() {
|
|
ui.Text(c, sym.Code).Font(theme.MonoFont).FontWeight(700).Width(80).SingleLine()
|
|
ui.Text(c, sym.Name).SingleLine().Grow(1).MinWidth(0).TextColor(k.Text)
|
|
if selected {
|
|
ui.Icon(c, icons.Must("check")).Size(14, 14).TextColor(k.AccentText)
|
|
}
|
|
if sym.Market != "" {
|
|
ui.Text(c, sym.Market).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted)
|
|
}
|
|
})
|
|
return b.Clicked() && !b.IsDisabled()
|
|
}
|