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>
105 lines
3.4 KiB
Go
105 lines
3.4 KiB
Go
package chart
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
|
|
)
|
|
|
|
// ChartCell holds one chart's symbol, interval, and window.
|
|
type ChartCell struct {
|
|
Symbol string
|
|
Interval Interval
|
|
View MarketView
|
|
}
|
|
|
|
// MultiChartState preserves six cells across layout changes.
|
|
type MultiChartState struct {
|
|
// Count is 1, 2, 4 or 6; zero is 1.
|
|
Count int
|
|
// Cells preserve their settings while hidden.
|
|
Cells [6]ChartCell
|
|
Active int
|
|
}
|
|
|
|
// MultiChartLayoutOptions configure a MultiChartLayout.
|
|
type MultiChartLayoutOptions struct {
|
|
// Height of the grid; zero is 480.
|
|
Height float32
|
|
}
|
|
|
|
// MultiChartLayout builds 1, 2, 4, or 6 independent charts.
|
|
func MultiChartLayout(c *ui.Context, state *MultiChartState, opts MultiChartLayoutOptions, cell func(i int, cell *ChartCell)) core.ChoiceResult {
|
|
if state == nil || cell == nil {
|
|
panic("mujica: MultiChartLayout needs a state and a cell builder")
|
|
}
|
|
root := ui.Column(c).Gap(core.Density(c).Unit() * 2).FillWidth()
|
|
if root.IsDisabled() {
|
|
copy := *state
|
|
state = ©
|
|
}
|
|
if opts.Height < 0 {
|
|
panic("mujica: MultiChartLayout Height must be non-negative")
|
|
}
|
|
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 := core.Tokens(c)
|
|
u := core.Density(c).Unit()
|
|
h := opts.Height
|
|
if h == 0 {
|
|
h = 480
|
|
}
|
|
changed := false
|
|
root.Children(func() {
|
|
sel := []int{state.Count}
|
|
var items []input.ToggleItem[int]
|
|
for _, n := range []int{1, 2, 4, 6} {
|
|
items = append(items, input.ToggleItem[int]{Value: n, Label: fmt.Sprint(n), Name: fmt.Sprintf(core.Msg(c, "multi_chart_layout.count"), n)})
|
|
}
|
|
ui.Row(c).Children(func() {
|
|
if input.ToggleGroup(c, &sel, items, input.ToggleGroupOptions{Label: core.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)
|
|
}
|
|
})
|
|
cols = map[int]int{1: 1, 2: 2, 4: 2, 6: 3}[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(core.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.IsDisabled() && (box.FocusWithin() || box.Clicked()) {
|
|
if state.Active != i {
|
|
state.Active, changed = i, true
|
|
}
|
|
}
|
|
box.Children(func() { cell(i, &state.Cells[i]) })
|
|
}
|
|
})
|
|
})
|
|
return core.NewChoiceResult(root, changed)
|
|
}
|