Files
MujicaUI/multi_chart_layout.go
T
2026-10-05 15:01:38 -04:00

95 lines
3.0 KiB
Go

package mujica
import (
"fmt"
"log/slog"
"github.com/egoist/mygo/ui"
"mujicaui/internal"
"mujicaui/theme"
)
// ChartCell is one chart of a MultiChartLayout: its symbol, period and view.
type ChartCell struct {
Symbol string
Interval Interval
View MarketView
}
// MultiChartState is the caller-held grid: how many cells show, all six cells' settings, and the active one.
type MultiChartState struct {
// Count is 1, 2, 4 or 6; zero is 1.
Count int
// Cells keep their settings while hidden, so changing the layout loses nothing.
Cells [6]ChartCell
Active int
}
// MultiChartLayoutOptions configure a MultiChartLayout.
type MultiChartLayoutOptions struct {
// Height of the grid; zero is 480.
Height float32
}
// MultiChartLayout shows 1, 2, 4 or 6 charts in a grid, each built by cell with its own settings.
func MultiChartLayout(c *ui.Context, state *MultiChartState, opts MultiChartLayoutOptions, cell func(i int, cell *ChartCell)) ChoiceResult {
if state == nil || cell == nil {
panic("mujica: MultiChartLayout needs a state and a cell builder")
}
if state.Count == 0 {
state.Count = 1
}
cols := map[int]int{1: 1, 2: 2, 4: 2, 6: 3}[state.Count]
if cols == 0 {
panic(fmt.Sprintf("mujica: MultiChartLayout Count %d must be 1, 2, 4 or 6", state.Count))
}
if state.Active < 0 || state.Active >= state.Count {
panic(fmt.Sprintf("mujica: MultiChartLayout Active %d outside %d cells", state.Active, state.Count))
}
k := Tokens(c)
u := Density(c).Unit()
h := opts.Height
if h == 0 {
h = 480
}
changed := false
root := ui.Column(c).Gap(u * 2).FillWidth()
root.Children(func() {
sel := []int{state.Count}
var items []ToggleItem[int]
for _, n := range []int{1, 2, 4, 6} {
items = append(items, ToggleItem[int]{Value: n, Label: fmt.Sprint(n), Name: fmt.Sprintf(msg(c, "multi_chart_layout.count"), n)})
}
ui.Row(c).Children(func() {
if ToggleGroup(c, &sel, items, ToggleGroupOptions{Label: msg(c, "multi_chart_layout.label")}).Changed() {
state.Count, changed = sel[0], true
state.Active = min(state.Active, state.Count-1)
slog.Debug("mujica: multi_chart_layout count", "count", state.Count)
}
})
rows := (state.Count + cols - 1) / cols
grid := ui.Box(c).Grid().Columns(cols).Gap(u * 2).Height(h).FillWidth()
grid.Children(func() {
for i := range state.Count {
box := ui.Column(c).Key(i).MinWidth(0).MinHeight(0).Height((h - u*2*float32(rows-1)) / float32(rows)).Padding(u * 2).
Background(k.Surface).Radius(theme.CardRadius).Clip().Role(ui.RoleGroup).
Label(fmt.Sprintf(msg(c, "multi_chart_layout.cell"), i+1, state.Cells[i].Symbol))
if i == state.Active && state.Count > 1 {
box.Border(2, k.AccentText)
box.DrawOver(func(p *ui.Painter, r ui.Rect) { internal.Frame(p, r, 3, 5, k.Ornament) })
} else {
box.Border(theme.BorderWidth, k.Border)
}
if box.FocusWithin() || box.Clicked() {
if state.Active != i {
state.Active, changed = i, true
}
}
box.Children(func() { cell(i, &state.Cells[i]) })
}
})
})
return ChoiceResult{root, changed}
}