package mujica import ( "log/slog" "github.com/egoist/mygo/ui" "mujicaui/internal" "mujicaui/theme" ) // ChartFrame is what every chart shares. type ChartFrame struct { // Label names the chart for assistive technology; required. Label string // Height is the plot height in DIPs; 0 means 240. Height float32 // Status shows loading or failure in the plot's place. Status DataStatus Error string // Empty replaces the built-in "No data" text. Empty string } // ChartResult is a built chart and this frame's events. type ChartResult struct { Element *ui.Element // Point is the hovered or focused mark, -1 for none. Point int changed, retry bool toggled string value bool } // Changed reports that the readout moved to another mark. func (r ChartResult) Changed() bool { return r.changed } // Retried reports a click on a failed load's retry. func (r ChartResult) Retried() bool { return r.retry } // ValueChanged reports a changed selection, path, year or pause. func (r ChartResult) ValueChanged() bool { return r.value } // Toggled returns the legend series switched in this frame. func (r ChartResult) Toggled() (string, bool) { return r.toggled, r.toggled != "" } // chartSpec is one chart's drawing and behavior. type chartSpec struct { // n counts the marks keys step through; 0 is empty. n int keys []ChartLegendItem // toggle lets legend entries hide and show series. toggle bool draw func(p *ui.Painter, r ui.Rect, active int) // pick returns the mark at (x, y), or -1. pick func(r ui.Rect, x, y float32) int readout func(i int) string // move steps from mark i; nil steps in order. move func(i int, key ui.Key) int // activate runs on Enter, Space or a click. activate func(i int) // anchor places mark i's tooltip in plot space; nil uses the plot's top middle. anchor func(r ui.Rect, i int) (x, y float32) // tip writes mark i's tooltip; nil splits the readout. tip func(i int) []ChartTooltipLine } type chartState struct { focus, last int hidden map[string]bool } // chart is a chart being built. type chart struct { c *ui.Context frame ChartFrame st *chartState res ChartResult } // newChart starts a chart; ask it which series are hidden. func newChart(c *ui.Context, frame ChartFrame) *chart { if frame.Label == "" || frame.Height < 0 { panic("mujica: chart needs a Label and a non-negative Height") } if frame.Height == 0 { frame.Height = 240 } ch := &chart{c: c, frame: frame} ch.res.Element = ui.Column(c).Gap(Density(c).Unit() * 2).MinWidth(0) ch.st = ui.Local(ch.res.Element, "mujica.chart", func() chartState { return chartState{focus: -1, last: -1, hidden: map[string]bool{}} }) return ch } // hidden reports whether the legend switched series name off. func (ch *chart) hidden(name string) bool { return ch.st.hidden[name] } // build lays out legend, plot and readout; returns the plot. func (ch *chart) build(spec chartSpec) (ChartResult, *ui.Element) { c, k, st := ch.c, Tokens(ch.c), ch.st u := Density(c).Unit() var plot *ui.Element ch.res.Element.Children(func() { if len(spec.keys) > 0 { var hidden *map[string]bool if spec.toggle { hidden = &st.hidden } if name, ok := ChartLegend(c, hidden, spec.keys, ChartLegendOptions{}).Toggled(); ok { ch.res.toggled = name } } if ch.frame.Status == DataLoading || ch.frame.Status == DataFailed || spec.n == 0 { st.focus = -1 ch.res.retry = ChartEmptyState(c, ch.frame.Status, ChartEmptyStateOptions{Height: ch.frame.Height, Error: ch.frame.Error, Empty: ch.frame.Empty}).Changed() ch.res.Point = -1 ui.Box(c).Height(u * 5) return } box := ui.Box(c).Height(ch.frame.Height).FillWidth().MinWidth(0) plot = box.Focusable().Label(ch.frame.Label).Role(ui.RoleGroup) internal.Ring(plot, k.Focus, theme.ControlRadius) st.focus = min(st.focus, spec.n-1) ch.input(plot, spec) active := -1 b := plot.Bounds() local := ui.Rect{W: b.W, H: b.H} x, y, over := plot.PointerPosition() if over && !plot.IsDisabled() { active = spec.pick(local, x, y) } if active < 0 && plot.Focused() { active = st.focus x, y = local.W/2, 0 if spec.anchor != nil { x, y = spec.anchor(local, active) } } ch.res.Point = active if active >= 0 && b.W > 0 { lines := chartTipLines(spec.readout(active)) if spec.tip != nil { lines = spec.tip(active) } ChartTooltip(c, b.X+x, b.Y+y, lines) } plot.Draw(func(p *ui.Painter, r ui.Rect) { spec.draw(p, r, active) }) text, col := msg(c, "chart.hint"), k.TextMuted if active >= 0 { text, col = spec.readout(active), k.Text } ui.Text(c, text).FontSize(FontSize(c, theme.CaptionSize)).TextColor(col).Role(ui.RoleStatus).SingleLine().Height(u * 5) }) ch.res.changed = ch.res.Point != st.last st.last = ch.res.Point return ch.res, plot } // input moves the keyboard mark, and takes clicks on marks. func (ch *chart) input(plot *ui.Element, spec chartSpec) { st := ch.st if plot.IsDisabled() { return } to := st.focus for _, key := range []ui.Key{ui.KeyLeft, ui.KeyRight, ui.KeyUp, ui.KeyDown} { if !plot.Shortcut(0, key) { continue } switch { case st.focus < 0: to = 0 case spec.move != nil: to = spec.move(st.focus, key) case key == ui.KeyLeft || key == ui.KeyUp: to = max(st.focus-1, 0) default: to = min(st.focus+1, spec.n-1) } } if plot.Shortcut(0, ui.KeyHome) { to = 0 } if plot.Shortcut(0, ui.KeyEnd) { to = spec.n - 1 } if plot.Shortcut(0, ui.KeyEscape) { to = -1 } if to != st.focus { st.focus = to if to >= 0 { ch.c.Announce(spec.readout(to)) } slog.Debug("mujica: chart focus", "label", ch.frame.Label, "mark", to) } act := st.focus >= 0 && (plot.Shortcut(0, ui.KeyEnter) || plot.Shortcut(0, ui.KeySpace)) if plot.Clicked() { plot.Focus() x, y, _ := plot.PointerPosition() b := plot.Bounds() if i := spec.pick(ui.Rect{W: b.W, H: b.H}, x, y); i >= 0 { st.focus, act = i, true } } if act && spec.activate != nil { spec.activate(st.focus) } } // chartText draws s anchored at (x, y) by ax, ay. func chartText(p *ui.Painter, s string, x, y, size float32, col ui.Color, ax, ay float32) (w, h float32) { w, h = p.MeasureText(0, ui.Span{Text: s, Size: size}) p.Text(x-w*ax, y-h*ay, s, size, col) return w, h } // chartInset is r less room for axis labels. func chartInset(r ui.Rect, left, bottom float32) ui.Rect { return ui.Rect{X: r.X + left, Y: r.Y + 8, W: max(r.W-left-12, 1), H: max(r.H-bottom-8, 1)} } // chartInk is Text or Surface, whichever reads better on bg. func chartInk(k theme.Tokens, bg ui.Color) ui.Color { if theme.Contrast(k.Text, bg) >= theme.Contrast(k.Surface, bg) { return k.Text } return k.Surface }