Files
MujicaUI/chart/indicator_selector.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

119 lines
4.7 KiB
Go

package chart
import (
"fmt"
"log/slog"
"slices"
"strings"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/overlay"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// IndicatorSelectorOptions configure an IndicatorSelector.
type IndicatorSelectorOptions struct {
Disabled bool
}
// IndicatorSelector searches, adds, removes, and configures chart indicators.
func IndicatorSelector(c *ui.Context, overlays *[]IndicatorOverlay, panes *IndicatorPanes, opts IndicatorSelectorOptions) core.ChoiceResult {
if overlays == nil || panes == nil {
panic("mujica: IndicatorSelector needs overlays and panes")
}
k := core.Tokens(c)
u := core.Density(c).Unit()
changed := false
box := ui.Row(c).Disabled(opts.Disabled)
box.Children(func() {
open := ui.Local(box, "indicator_selector.open", func() bool { return false })
query := ui.Local(box, "indicator_selector.query", func() string { return "" })
if box.IsDisabled() {
*open = false
}
label := fmt.Sprintf(core.Msg(c, "indicator_selector.button"), len(*overlays)+len(panes.Panes))
trigger := input.Button(c, label, input.ButtonOptions{Variant: input.Secondary, Icon: icons.Must("plus")})
if trigger.Clicked() {
*open = !*open
}
overlay.Popover(c, trigger, open, overlay.PopoverOptions{Title: core.Msg(c, "indicator_selector.title"), Width: 340}, func() {
input.TextInput(c, query, input.TextInputOptions{Placeholder: core.Msg(c, "indicator_selector.search"), Label: core.Msg(c, "indicator_selector.search"), Clearable: true})
ui.Text(c, core.Msg(c, "indicator_selector.available")).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted).Margin(u, 0, 0, 0)
found := 0
for kind := IndicatorMA; kind <= IndicatorOBV; kind++ {
name := kind.String()
if q := strings.TrimSpace(*query); q != "" && !strings.Contains(strings.ToLower(name), strings.ToLower(q)) {
continue
}
found++
ui.Row(c.Key(name)).AlignItems(ui.Center).Gap(u * 2).MinHeight(core.Density(c).ControlHeight()).Children(func() {
ui.Text(c, name).FontWeight(600).Width(56)
where := "indicator_selector.pane"
if indicatorInfo[kind].overlay {
where = "indicator_selector.overlay"
}
ui.Text(c, core.Msg(c, where)).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted).Grow(1)
if input.Button(c, core.Msg(c, "indicator_selector.add"), input.ButtonOptions{Variant: input.Ghost, Label: fmt.Sprintf(core.Msg(c, "indicator_selector.add_named"), name)}).Clicked() {
if indicatorInfo[kind].overlay {
*overlays = append(slices.Clone(*overlays), IndicatorOverlay{Kind: kind, Params: kind.DefaultParams()})
} else {
panes.Panes = append(slices.Clone(panes.Panes), IndicatorPaneSpec{Kind: kind, Params: kind.DefaultParams()})
}
changed = true
slog.Debug("mujica: indicator_selector add", "kind", name)
}
})
}
if found == 0 {
ui.Text(c, core.Msg(c, "indicator_selector.none")).TextColor(k.TextMuted)
}
if len(*overlays)+len(panes.Panes) == 0 {
return
}
ui.Text(c, core.Msg(c, "indicator_selector.active")).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted).Margin(u*2, 0, 0, 0)
remove := -1
for i := range *overlays {
if indicatorSelectorActive(c, u, fmt.Sprint("o", i), (*overlays)[i].Kind, &(*overlays)[i].Params, &changed) {
remove = i
}
}
if remove >= 0 {
*overlays = slices.Delete(slices.Clone(*overlays), remove, remove+1)
changed = true
}
remove = -1
for i := range panes.Panes {
if indicatorSelectorActive(c, u, fmt.Sprint("p", i), panes.Panes[i].Kind, &panes.Panes[i].Params, &changed) {
remove = i
}
}
if remove >= 0 {
panes.Panes = slices.Delete(slices.Clone(panes.Panes), remove, remove+1)
changed = true
}
})
})
return core.NewChoiceResult(box, changed)
}
// indicatorSelectorActive edits parameters and reports removal of an indicator.
func indicatorSelectorActive(c *ui.Context, u float32, key string, kind IndicatorKind, params *[]float64, changed *bool) bool {
removed := false
ui.Row(c.Key(key)).AlignItems(ui.Center).Gap(u * 2).Wrap().Children(func() {
ui.Text(c, kind.String()).FontWeight(600).Width(56)
if len(kind.DefaultParams()) > 0 && IndicatorParamsField(c, kind, params).Changed() {
*changed = true
}
ui.Spacer(c)
if core.IconAction(c, icons.Must("x"), fmt.Sprintf(core.Msg(c, "indicator_pane.remove"), indicatorName(kind, *params))).Clicked() {
removed = true
slog.Debug("mujica: indicator_selector remove", "kind", kind)
}
})
return removed
}