WIP components (as built)

This commit is contained in:
ZacharyZhang-NY committed 2026-10-05 15:43:44 -04:00
1 parent 71bd0479d2
commit 48a32c1bec
112 files changed
+3607 -1241

No files matched your search

+49 -14
View File
@@ -11,7 +11,7 @@ import (
"mujicaui/internal"
)
// PriceAlertLine is a caller-held price alert drawn as a dashed line that the pointer drags.
// PriceAlertLine holds a draggable, caller-owned price threshold.
type PriceAlertLine struct {
ID string
Price float64
@@ -19,40 +19,63 @@ type PriceAlertLine struct {
Label string
}
// CompareSymbol is another symbol's closes, aligned to the chart's candles, shown in percent.
// CompareSymbol holds aligned closes for percent comparison.
type CompareSymbol struct {
Name string
Closes []float64
}
// compareLines adds the compare symbols' percent lines and sets percent mode.
// compareLines normalizes prices and compared symbols into percentages.
func (m *mktCore) compareLines() {
if m.opts.Compare == nil || len(*m.opts.Compare) == 0 || m.to <= m.from {
return
}
m.base = m.candles[m.from].Close
if m.base == 0 {
panic("mujica: percent comparison needs a nonzero base close")
}
pal := internal.Palette(m.k)
for i := range m.overlays {
line := &m.overlays[i]
values := line.values
base := m.base
line.values = chartMemo(m.ch, fmt.Sprint("compare.overlay.", i), [2]any{chartID(values), base}, func() []float64 {
out := make([]float64, len(values))
for j, v := range values {
out[j] = (v/base - 1) * 100
}
return out
})
}
for i, s := range *m.opts.Compare {
if s.Name == "" || len(s.Closes) != len(m.candles) {
panic(fmt.Sprintf("mujica: CompareSymbol %q needs a Name and one close per candle", s.Name))
}
chartMemo(m.ch, "compare.check."+s.Name, chartID(s.Closes), func() bool {
for _, v := range s.Closes {
if math.IsInf(v, 0) || (!math.IsNaN(v) && v <= 0) {
panic("mujica: compared closes must be positive or NaN")
}
}
return true
})
base := math.NaN()
for j := m.from; j < m.to && math.IsNaN(base); j++ {
base = s.Closes[j]
}
closes := s.Closes
vals := chartMemo(m.ch, "compare."+s.Name, [2]any{chartID(closes), base}, func() []float64 {
vals := chartMemo(m.ch, "compare."+s.Name, [2]any{chartID(closes), math.Float64bits(base)}, func() []float64 {
out := make([]float64, len(closes))
for j, v := range closes {
out[j] = (v/base - 1) * 100
}
return out
})
m.overlays = append(m.overlays, mktLine{name: s.Name, values: vals, color: pal[(i+1)%len(pal)], shape: len(m.overlays)})
m.overlays = append(m.overlays, mktLine{name: s.Name, values: vals, color: pal[(i+1)%len(pal)], shape: i + 1})
}
}
// CompareLegend lists the compare symbols with their colors and a remove button each; Changed reports a removal.
// CompareLegend shows series markers and removal actions.
func CompareLegend(c *ui.Context, symbols *[]CompareSymbol) ChoiceResult {
if symbols == nil {
panic("mujica: CompareLegend needs symbols")
@@ -81,13 +104,22 @@ func CompareLegend(c *ui.Context, symbols *[]CompareSymbol) ChoiceResult {
return ChoiceResult{row, removed >= 0}
}
// alertsCheck runs OnAlert for each alert the newest close crossed since the last frame.
// alertsCheck fires callbacks when the latest close crosses thresholds.
func (m *mktCore) alertsCheck() {
if m.opts.Alerts != nil {
seen := map[string]bool{}
for _, a := range *m.opts.Alerts {
if a.ID == "" || seen[a.ID] || math.IsNaN(a.Price) || math.IsInf(a.Price, 0) {
panic("mujica: alerts need unique IDs and finite prices")
}
seen[a.ID] = true
}
}
if m.opts.Alerts == nil || len(m.candles) == 0 {
return
}
last := m.candles[len(m.candles)-1]
if m.st.alertSeen && last.Close != m.st.lastClose && m.opts.OnAlert != nil {
if !m.ch.res.Element.IsDisabled() && m.st.alertSeen && last.Close != m.st.lastClose && m.opts.OnAlert != nil {
for _, a := range *m.opts.Alerts {
if (m.st.lastClose-a.Price)*(last.Close-a.Price) <= 0 && m.st.lastClose != a.Price {
slog.Debug("mujica: price_alert_line crossed", "id", a.ID)
@@ -98,7 +130,7 @@ func (m *mktCore) alertsCheck() {
m.st.lastClose, m.st.alertSeen = last.Close, true
}
// alertsInput drags an alert line that the press started on; it reports taking the drag.
// alertsInput drags the threshold captured by the initial press.
func (m *mktCore) alertsInput(g mktGeom, x, y float32, held bool, dy float32) bool {
if m.opts.Alerts == nil || m.base != 0 {
return false
@@ -110,7 +142,7 @@ func (m *mktCore) alertsInput(g mktGeom, x, y float32, held bool, dy float32) bo
}
if m.st.grab < 0 {
for i, a := range alerts {
if math.Abs(float64(g.ys.Map(a.Price)-(y-dy))) <= 5 {
if g.plot.Contains(x, y-dy) && math.Abs(float64(g.ys.Map(a.Price)-(y-dy))) <= 14 {
m.st.grab = i
}
}
@@ -118,19 +150,22 @@ func (m *mktCore) alertsInput(g mktGeom, x, y float32, held bool, dy float32) bo
if m.st.grab < 0 || m.st.grab >= len(alerts) {
return false
}
p := math.Pow(10, float64(m.dec))
alerts[m.st.grab].Price = math.Round(g.ys.Invert(y)*p) / p
spec := numberInputResolve(NumberInputOptions{Step: math.Pow10(-m.dec), Precision: m.dec})
alerts[m.st.grab].Price = spec.round(g.ys.Invert(y))
slog.Debug("mujica: price_alert_line moved", "id", alerts[m.st.grab].ID)
return true
}
// paintAlerts draws each alert as a dashed line with its price tag.
// paintAlerts draws dashed thresholds and price labels.
func (m *mktCore) paintAlerts(p *ui.Painter, g mktGeom, size float32) {
if m.opts.Alerts == nil || m.base != 0 {
return
}
for _, a := range *m.opts.Alerts {
for i, a := range *m.opts.Alerts {
y := internal.Snap(p, g.ys.Map(a.Price))
if i == m.st.alertFocus {
p.FillPath(internal.Diamond(internal.Snap(p, g.plot.X+8), y, 9), m.k.Warning)
}
for x := g.plot.X; x < g.plot.X+g.plot.W; x += 8 {
p.Line(x, y, min(x+4, g.plot.X+g.plot.W), y, 1, m.k.Warning)
}