Files
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

105 lines
2.7 KiB
Go

package input
import (
"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"
)
// ButtonVariant is a button's emphasis. The zero value is Primary.
type ButtonVariant int
const (
Primary ButtonVariant = iota
Secondary
Outline
Ghost
Danger
)
// ButtonOptions configure a Button.
type ButtonOptions struct {
Variant ButtonVariant
// Icon goes before the label.
Icon *ui.SVG
Loading bool
Disabled bool
// Label names an icon-only button for assistive technology; required without a label.
Label string
}
// buttonSpinnerSize is the leading icon slot, which the spinner takes while loading.
const buttonSpinnerSize float32 = 16
// Button is a push button. Loading keeps its size and blocks clicks.
func Button(c *ui.Context, label string, opts ButtonOptions) ui.Element {
if label == "" && opts.Label == "" {
panic("mujica: Button without a label needs Label to name it")
}
k := core.Tokens(c)
d := core.Density(c)
pad, gap := d.Unit()*4, d.Unit()*2
if opts.Loading && opts.Icon == nil && label != "" {
// The spinner slot comes out of the padding, so the width holds.
pad -= (buttonSpinnerSize + gap) / 2
}
b := ui.ButtonBase(c).MinHeight(d.ControlHeight()).MinWidth(theme.MinHitSize).
Padding(0, pad).Gap(gap).Radius(theme.ControlRadius).
Disabled(opts.Disabled || opts.Loading)
internal.Ring(b, k.Focus, theme.ControlRadius)
if opts.Label != "" {
b.Label(opts.Label)
}
bg, fg, border := ButtonColors(k, opts.Variant, b.Hovered() && !opts.Disabled, b.Pressed())
b.Background(bg).TextColor(fg)
if border.A > 0 {
b.Border(theme.BorderWidth, border)
}
b.Children(func() {
if opts.Loading {
core.Spinner(c, core.SpinnerOptions{Size: buttonSpinnerSize, Color: fg})
} else if opts.Icon != nil {
ui.Icon(c, opts.Icon).Size(buttonSpinnerSize, buttonSpinnerSize)
}
if label != "" {
ui.Text(c, label).SingleLine()
}
})
return b
}
func ButtonColors(k theme.Tokens, v ButtonVariant, hover, pressed bool) (bg, fg, border ui.Color) {
switch v {
case Secondary:
bg, fg, border = k.Surface, k.Text, k.ControlBorder
case Outline:
bg, fg, border = ui.Transparent, k.AccentText, k.AccentText
case Ghost:
bg, fg = ui.Transparent, k.AccentText
case Danger:
bg, fg = k.DangerFill, k.OnDanger
if pressed {
bg = k.DangerFill.Mix(ui.RGB(0, 0, 0), 0.25)
} else if hover {
bg = k.DangerFill.Mix(ui.RGB(0, 0, 0), 0.12)
}
return
default:
bg, fg = k.Accent, k.OnAccent
if pressed {
bg = k.AccentPressed
} else if hover {
bg = k.AccentHover
}
return
}
if pressed {
bg = k.Selection
} else if hover {
bg = k.SurfaceHover
}
return
}