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

98 lines
2.6 KiB
Go

package chart
import (
"strings"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// ChartTooltipLine pairs readout text with an optional marker.
type ChartTooltipLine struct {
Text string
Color ui.Color
Shape int
}
// ChartTooltip shows readouts beside a window point, avoiding edges.
func ChartTooltip(c *ui.Context, x, y float32, lines []ChartTooltipLine) ui.Element {
if len(lines) == 0 {
panic("mujica: ChartTooltip needs a line")
}
k := core.Tokens(c)
u := core.Density(c).Unit()
size := core.FontSize(c, theme.CaptionSize)
pad, gap := u*2, float32(12)
w, h := float32(0), pad*2
lh := make([]float32, len(lines))
for i, l := range lines {
tw, th := c.MeasureText(0, ui.Span{Text: l.Text, Size: size, Weight: chartTipWeight(i)})
if l.Color.A > 0 {
tw += 14
}
w, lh[i] = max(w, tw), th
h += th
}
w += pad * 2
ww, wh := c.Size()
w, h = min(w, max(1, ww-8)), min(h, max(1, wh-8))
left, top := core.PlaceOverlay(c, ui.Rect{X: x, Y: y}, w, h, gap)
if x+gap+w <= ww-4 {
left = max(4, x+gap)
} else {
left = max(4, min(x-gap-w, ww-4-w))
}
var tip ui.Element
ui.Overlay(c, func() {
tip = ui.Column(c).Absolute().Left(left).Top(top).Size(w, h).Role(ui.RoleNone).PassThrough()
core.OverlayStyle(c, tip, core.OverlayEnter(c, tip))
tip.Children(func() {
inner := max(1, w-2*pad)
content := float32(0)
for i, l := range lines {
width := inner
if l.Color.A > 0 {
width = max(1, width-14)
}
_, lh[i] = c.MeasureText(width, ui.Span{Text: l.Text, Size: size, Weight: chartTipWeight(i)})
content += lh[i]
}
ui.Scroll(c).Fill().Padding(pad).PassThrough().Children(func() {
ui.Box(c).Width(inner).Height(content).Shrink(0).PassThrough().Draw(func(p *ui.Painter, r ui.Rect) {
at := r.Y
for i, l := range lines {
tx, width := r.X, inner
if l.Color.A > 0 {
internal.Marker(p, l.Shape, tx+5, at+lh[i]/2, 9, l.Color)
tx, width = tx+14, max(1, width-14)
}
p.RichText(tx, at, width, ui.Span{Text: l.Text, Size: size, Weight: chartTipWeight(i), Color: k.Text})
at += lh[i]
}
})
})
})
})
return tip
}
// chartTipWeight sets the first line, the heading, in semibold.
func chartTipWeight(i int) int {
if i == 0 {
return 600
}
return 400
}
// chartTipLines splits a readout into tooltip lines.
func chartTipLines(readout string) []ChartTooltipLine {
var out []ChartTooltipLine
for _, s := range strings.Split(readout, " · ") {
out = append(out, ChartTooltipLine{Text: s})
}
return out
}