Files
MujicaUI/data/description_list.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

116 lines
3.5 KiB
Go

package data
import (
"log/slog"
"math"
"time"
"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"
)
// DescriptionItem is a name and its value.
type DescriptionItem struct {
Label string
Value string
// Span is how many columns the item takes; negative spans the whole row.
Span int
// Copyable adds a button copying Value to the clipboard.
Copyable bool
}
// DescriptionListOptions configure a DescriptionList.
type DescriptionListOptions struct {
// Columns is the most columns shown; 0 means 2.
Columns int
// MinColumnWidth drops columns when narrower; 0 means 220 DIPs.
MinColumnWidth float32
}
// DescriptionListView is a built DescriptionList and what happened to it in this frame.
type DescriptionListView struct {
Element ui.Element
copied int
}
// Copied returns the index of the item whose value was copied in this frame.
func (v DescriptionListView) Copied() (int, bool) { return v.copied, v.copied >= 0 }
// descriptionCopied shows the copied mark for this long.
const descriptionCopied = 1500 * time.Millisecond
type descriptionState struct {
item int
until time.Time
}
// DescriptionList shows names and values in columns, one column when narrow.
func DescriptionList(c *ui.Context, items []DescriptionItem, opts DescriptionListOptions) DescriptionListView {
if opts.Columns < 0 || opts.MinColumnWidth < 0 {
panic("mujica: DescriptionList with negative Columns or MinColumnWidth")
}
most, minW := opts.Columns, opts.MinColumnWidth
if most == 0 {
most = 2
}
if minW == 0 {
minW = 220
}
k := core.Tokens(c)
u := core.Density(c).Unit()
gap := u * 6
v := DescriptionListView{Element: ui.Grid(c).GapX(gap).GapY(u * 4).FillWidth(), copied: -1}
st := ui.Local(v.Element, "state", func() descriptionState { return descriptionState{item: -1} })
fit := func(w float32) int {
if w <= 0 {
return most
}
return max(1, min(most, int(math.Floor(float64((w+gap)/(minW+gap))))))
}
cols := fit(v.Element.Bounds().W)
v.Element.Draw(func(p *ui.Painter, r ui.Rect) {
if fit(r.W) != cols {
// The new width needs other columns: build again.
c.Invalidate()
}
})
now := c.Now()
v.Element.Columns(cols).Children(func() {
for i, it := range items {
cell := ui.Column(c).Gap(u).MinWidth(0)
if it.Span < 0 || it.Span > cols {
cell.ColumnSpan(-1)
} else if it.Span > 1 {
cell.ColumnSpan(it.Span)
}
cell.Children(func() {
ui.Text(c, it.Label).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted)
ui.Row(c).Gap(u).MinHeight(theme.MinHitSize).AlignItems(ui.Center).Children(func() {
ui.Text(c, it.Value).Shrink(1).MinWidth(0)
if !it.Copyable {
return
}
done := st.item == i && now.Before(st.until)
icon, label := icons.Must("copy"), core.Msg(c, "description_list.copy")+" "+it.Label
if done {
icon, label = icons.Must("check"), core.Msg(c, "description_list.copied")
c.After(st.until.Sub(now))
}
if input.Button(c, "", input.ButtonOptions{Variant: input.Ghost, Icon: icon, Label: label}).MinHeight(theme.MinHitSize).Padding(0, u).Clicked() {
c.WriteClipboard(it.Value)
c.Announce(core.Msg(c, "description_list.copied"))
*st = descriptionState{item: i, until: now.Add(descriptionCopied)}
v.copied = i
slog.Debug("mujica: description_list copy", "item", i)
}
})
})
}
})
return v
}