Files
MujicaUI/chat/capability_cards.go
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

96 lines
3.0 KiB
Go

package chat
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// CapabilityCard explains one assistant capability with example prompts.
type CapabilityCard struct {
Title, Description string
Icon *ui.SVG
Examples []string
}
// CapabilityCardsOptions configure CapabilityCards.
type CapabilityCardsOptions struct {
// Columns is the most cards per row; zero is 3.
Columns int
Disabled bool
}
// CapabilityCardsView is built CapabilityCards and the example clicked this frame.
type CapabilityCardsView struct {
Element ui.Element
// Example is the example prompt chosen, empty for none.
Example string
}
// CapabilityCards shows capabilities; a clicked example goes to the caller.
func CapabilityCards(c *ui.Context, cards []CapabilityCard, opts CapabilityCardsOptions) CapabilityCardsView {
if opts.Columns < 0 {
panic("mujica: CapabilityCards Columns is negative")
}
cols := opts.Columns
if cols == 0 {
cols = 3
}
k := core.Tokens(c)
d := core.Density(c)
g := ui.Grid(c).Columns(cols).Gap(d.Unit() * 3).Disabled(opts.Disabled)
v := CapabilityCardsView{Element: g}
g.Children(func() {
for i, card := range cards {
if card.Title == "" {
panic("mujica: CapabilityCard needs a Title")
}
box := ui.Column(c.Key(i)).Gap(d.Unit()*2).Padding(d.Unit()*4).Background(k.Surface).
Border(theme.BorderWidth, k.Border).Radius(theme.CardRadius).Role(ui.RoleGroup).Label(card.Title)
box.Children(func() {
ui.Row(c).Gap(d.Unit() * 2).AlignItems(ui.Center).Children(func() {
if card.Icon != nil {
ui.Icon(c, card.Icon).Size(20, 20).TextColor(k.AccentText)
}
ui.Text(c, card.Title).Font(theme.SerifFont).FontSize(core.FontSize(c, theme.H3Size)).TextColor(k.Text)
})
if card.Description != "" {
ui.Text(c, card.Description).TextColor(k.TextMuted)
}
ui.Column(c).Gap(d.Unit()).FocusGroup(ui.Vertical).Children(func() {
for _, ex := range card.Examples {
if capabilityExample(c, k, ex) {
v.Example = ex
}
}
})
})
}
})
if v.Example != "" {
slog.Debug("mujica: capability_cards example", "len", len(v.Example))
}
return v
}
// capabilityExample is a quoted example button reporting a click.
func capabilityExample(c *ui.Context, k theme.Tokens, text string) bool {
d := core.Density(c)
b := ui.ButtonBase(c).MinHeight(d.ControlHeight()).Padding(d.Unit(), d.Unit()*3).Justify(ui.Start).
Radius(theme.ControlRadius).Background(k.Background).Border(theme.BorderWidth, k.Border).Label(text)
internal.Ring(b, k.Focus, theme.ControlRadius)
switch {
case b.Pressed():
b.Background(k.Selection)
case b.Hovered():
b.Background(k.SurfaceHover).Border(theme.BorderWidth, k.ControlBorder)
}
b.Transition(core.ColorFade)
b.Children(func() { ui.Text(c, "“"+text+"”").TextColor(k.Text) })
return b.Clicked() && !b.IsDisabled()
}