Split into category packages; module github.com/ZacharyZhang-NY/MujicaUI
This commit is contained in:
1822 files changed
+24568
-21314
No files matched your search
@@ -0,0 +1,152 @@
|
||||
package finance
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/core"
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/data"
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/icons"
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/input"
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/internal"
|
||||
"github.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()
|
||||
}
|
||||
Reference in new issue
Block a user