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>
97 lines
3.6 KiB
Go
97 lines
3.6 KiB
Go
package chat
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"math"
|
|
"strconv"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
|
|
)
|
|
|
|
// ModelParam is one generation parameter, as temperature or top-p.
|
|
type ModelParam struct {
|
|
// Label and Value are required.
|
|
Label string
|
|
Value *float64
|
|
// Min, Max and Step define the grid; Default selects reset.
|
|
Min, Max, Step, Default float64
|
|
}
|
|
|
|
// ParameterPanel displays bounded generation sliders and reset actions.
|
|
func ParameterPanel(c *ui.Context, params []ModelParam) core.ChoiceResult {
|
|
for _, p := range params {
|
|
if p.Label == "" || p.Value == nil || !(p.Min < p.Max) || !(p.Step > 0) || !(p.Default >= p.Min && p.Default <= p.Max) {
|
|
panic("mujica: ModelParam needs Label, Value, Min < Max, Step > 0 and Default in range")
|
|
}
|
|
for _, v := range []float64{p.Min, p.Max, p.Step, p.Default, *p.Value} {
|
|
if math.IsNaN(v) || math.IsInf(v, 0) {
|
|
panic("mujica: ModelParam values must be finite")
|
|
}
|
|
}
|
|
if *p.Value < p.Min || *p.Value > p.Max {
|
|
panic("mujica: ModelParam value must be in range")
|
|
}
|
|
spec := input.NumberInputResolve(input.NumberInputOptions{Min: p.Min, Max: p.Max, Step: p.Step, Precision: input.SliderDecimals(p.Min, p.Step)})
|
|
if spec.StepBy(p.Default, 0) != p.Default {
|
|
panic("mujica: ModelParam default must lie on the step grid")
|
|
}
|
|
if spec.StepBy(*p.Value, 0) != *p.Value {
|
|
panic("mujica: ModelParam value must lie on the step grid")
|
|
}
|
|
}
|
|
k := core.Tokens(c)
|
|
u := core.Density(c).Unit()
|
|
small := core.FontSize(c, theme.CaptionSize)
|
|
var r core.ChoiceResult
|
|
r.Element = ui.Column(c).Gap(u * 4).MinWidth(240).Role(ui.RoleGroup).Label(core.Msg(c, "parameter_panel.label"))
|
|
disabled := r.Element.IsDisabled()
|
|
set := func(p ModelParam, v float64) {
|
|
if *p.Value != v && !disabled {
|
|
*p.Value, r = v, core.NewChoiceResult(r.Element, true)
|
|
slog.Debug("mujica: parameter_panel set", "param", p.Label, "value", v)
|
|
}
|
|
}
|
|
r.Element.Children(func() {
|
|
for _, p := range params {
|
|
format := func(v float64) string {
|
|
return strconv.FormatFloat(v, 'f', input.SliderDecimals(p.Min, p.Step, p.Default), 64)
|
|
}
|
|
ui.Column(c.Key(p.Label)).Gap(u).Children(func() {
|
|
ui.Row(c).Gap(u * 2).AlignItems(ui.Center).Children(func() {
|
|
ui.Text(c, p.Label).FontWeight(600).Grow(1).SingleLine()
|
|
ui.Text(c, format(*p.Value)).FontFeatures("tnum").TextColor(k.AccentText).SingleLine()
|
|
ui.Row(c).Disabled(*p.Value == p.Default).Children(func() {
|
|
if core.IconAction(c, icons.Must("rotate-ccw"), fmt.Sprintf(core.Msg(c, "parameter_panel.reset"), p.Label)).
|
|
Tooltip(fmt.Sprintf(core.Msg(c, "parameter_panel.default"), format(p.Default))).Clicked() {
|
|
set(p, p.Default)
|
|
}
|
|
})
|
|
})
|
|
v := *p.Value
|
|
if input.Slider(c, &v, input.SliderOptions{Min: p.Min, Max: p.Max, Step: p.Step, Label: p.Label, Disabled: disabled}).Changed() {
|
|
set(p, v)
|
|
}
|
|
ui.Row(c).Children(func() {
|
|
ui.Text(c, format(p.Min)).FontSize(small).TextColor(k.TextMuted).Grow(1)
|
|
ui.Text(c, fmt.Sprintf(core.Msg(c, "parameter_panel.default"), format(p.Default))).FontSize(small).TextColor(k.TextMuted)
|
|
ui.Text(c, format(p.Max)).FontSize(small).TextColor(k.TextMuted).Grow(1).TextAlign(ui.End)
|
|
})
|
|
})
|
|
}
|
|
ui.Row(c).Justify(ui.End).Children(func() {
|
|
if input.Button(c, core.Msg(c, "parameter_panel.reset_all"), input.ButtonOptions{Variant: input.Ghost, Icon: icons.Must("rotate-ccw")}).Clicked() {
|
|
for _, p := range params {
|
|
set(p, p.Default)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
return r
|
|
}
|