Files
MujicaUI/finance/quick_trade_buttons.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

132 lines
4.3 KiB
Go

package finance
import (
"fmt"
"log/slog"
"slices"
"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/internal"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/navigation"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/overlay"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// QuickTradeButtonsOptions configure QuickTradeButtons. Symbol is required.
type QuickTradeButtonsOptions struct {
Symbol string
// Presets are the quantities offered; nil means 1, 10, 100.
Presets []float64
// Decimals of the quantities shown.
Decimals int
// Confirm asks before sending, by the button pressed.
Confirm bool
Disabled bool
}
// QuickTradeResult is built QuickTradeButtons and their trade.
type QuickTradeResult struct {
Element ui.Element
side OrderSide
quantity float64
}
// Traded returns the side and quantity the user traded.
func (r QuickTradeResult) Traded() (OrderSide, float64, bool) { return r.side, r.quantity, r.side != 0 }
type quickTradeState struct{ asking OrderSide }
// QuickTradeButtons trade *quantity at market with one press.
func QuickTradeButtons(c *ui.Context, quantity *float64, opts QuickTradeButtonsOptions) QuickTradeResult {
if opts.Symbol == "" {
panic("mujica: QuickTradeButtons needs a Symbol")
}
if opts.Decimals < 0 {
panic("mujica: QuickTradeButtons with negative Decimals")
}
presets := opts.Presets
if presets == nil {
presets = []float64{1, 10, 100}
}
if *quantity == 0 {
*quantity = presets[0]
}
at := slices.Index(presets, *quantity)
if at < 0 {
panic(fmt.Sprintf("mujica: QuickTradeButtons quantity %v is not one of Presets", *quantity))
}
u := core.Density(c).Unit()
var r QuickTradeResult
r.Element = ui.Column(c).Gap(u * 2).Disabled(opts.Disabled).Role(ui.RoleGroup).Label(core.Msg(c, "quick_trade_buttons.label"))
st := ui.Local(r.Element, "quick", func() quickTradeState { return quickTradeState{} })
disabled := r.Element.IsDisabled()
if disabled {
st.asking = 0
}
qty := data.StatisticFormat(*quantity, opts.Decimals)
r.Element.Children(func() {
var segs []navigation.Segment
for _, p := range presets {
segs = append(segs, navigation.Segment{Label: data.StatisticFormat(p, opts.Decimals)})
}
if navigation.SegmentedControl(c, &at, segs, navigation.SegmentedControlOptions{Equal: true, Label: core.Msg(c, "quick_trade_buttons.quantity"), Disabled: disabled}).Changed() {
*quantity = presets[at]
qty = data.StatisticFormat(*quantity, opts.Decimals)
}
ui.Row(c).Gap(u * 2).Children(func() {
for _, side := range []OrderSide{SideBuy, SideSell} {
label := fmt.Sprintf(core.Msg(c, fmt.Sprintf("quick_trade_buttons.side%d", side)), qty)
b := quickTradeButton(c, label, side)
if b.Clicked() && !disabled {
if opts.Confirm {
st.asking = side
} else {
r.side, r.quantity = side, *quantity
}
}
open := st.asking == side
res := overlay.Popconfirm(c, b, &open, overlay.PopconfirmOptions{
Title: fmt.Sprintf(core.Msg(c, fmt.Sprintf("quick_trade_buttons.confirm%d", side)), qty, opts.Symbol),
Description: core.Msg(c, "quick_trade_buttons.market"),
ConfirmLabel: core.Msg(c, fmt.Sprintf("order_entry.side%d", side)),
Danger: true,
})
if res.Confirmed && !disabled {
r.side, r.quantity = side, *quantity
}
if !open && st.asking == side {
st.asking = 0
}
}
})
})
if r.side != 0 {
slog.Debug("mujica: quick_trade_buttons traded", "side", r.side)
}
return r
}
// quickTradeButton is a large button tinted by its side.
func quickTradeButton(c *ui.Context, label string, side OrderSide) ui.Element {
k := core.Tokens(c)
d := core.Density(c)
col := core.TrendColor(c, side.sign())
b := ui.ButtonBase(c).Grow(1).Basis(0).MinHeight(d.ControlHeight()+d.Unit()*2).Radius(theme.ControlRadius).
Border(theme.BorderWidth*1.5, col).TextColor(col).Background(col.Alpha(0.1)).Label(label)
internal.Ring(b, k.Focus, theme.ControlRadius)
switch {
case b.Pressed():
b.Background(col.Alpha(0.28))
case b.Hovered():
b.Background(col.Alpha(0.18))
}
b.Children(func() {
ui.Icon(c, core.TrendIcon(side.sign())).Size(16, 16)
ui.Text(c, label).FontWeight(700).SingleLine()
})
return b
}