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

141 lines
4.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package display
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"
)
// ImageOptions configure an Image.
type ImageOptions struct {
// Width, Height in DIPs; zero uses src size or 160×120.
Width, Height float32
// Crop covers and crops the box; false fits inside.
Crop bool
// Alt describes the picture for assistive technology.
Alt string
// Loading shows the caller's load in progress.
Loading bool
// Err shows the caller's load failure with a retry button.
Err error
}
// ImageView is a shown Image and this frame's events.
type ImageView struct {
Element ui.Element
// Retry reports the error state's retry button was pressed.
Retry bool
}
// imageBlank sizes the box of a nil src: 160×120.
var imageBlank = ui.MustParseSVG([]byte(`<svg xmlns="http://www.w3.org/2000/svg" width="160" height="120"/>`))
type imageRoomKey struct{}
// imageErrorTextWidth is the room the error text needs.
const imageErrorTextWidth = 120
// Image shows a bitmap or SVG; nil shows a placeholder.
func Image(c *ui.Context, src ui.ImageSource, opts ImageOptions) ImageView {
if opts.Width < 0 || opts.Height < 0 {
panic("mujica: Image Width and Height must not be negative")
}
loaded := opts.Err == nil && !opts.Loading && src != nil
var v ImageView
v.Element = imageSized(c, src, opts, func(box, pic ui.Element) {
if loaded {
if opts.Alt != "" {
pic.Label(opts.Alt)
}
return
}
pic.Invisible()
v.Retry = imageLayers(c, box, opts)
})
return v
}
// imageLayers builds the state layers over the hidden picture.
func imageLayers(c *ui.Context, box ui.Element, opts ImageOptions) (retry bool) {
k := core.Tokens(c)
u := core.Density(c).Unit()
room := box.Bounds()
// Build again when the box changed, settling the error text.
if last := ui.Local(box, imageRoomKey{}, func() ui.Rect { return ui.Rect{W: -1} }); *last != room {
*last = room
c.AnimationFrame()
}
{
// The clipped backdrop; Retry sits above it, unclipped.
back := ui.Box(c).Absolute().Left(0).Top(0).Right(0).Bottom(0).Center().Clip().
Radius(theme.ControlRadius).Background(k.SurfaceHover).Border(theme.BorderWidth, k.Border)
switch {
case opts.Err != nil:
box.Role(ui.RoleGroup).Label(core.Msg(c, "image.error")).Description(opts.Alt).Error(opts.Err.Error())
ui.Row(c).Absolute().Left(0).Top(0).Right(0).Bottom(0).Center().Gap(u * 2).Children(func() {
if imageRetry(c, k, opts.Err.Error()).Clicked() {
retry = true
slog.Debug("mujica: image retry", "alt", opts.Alt)
}
if room.W >= theme.MinHitSize+u*4+imageErrorTextWidth && room.H >= theme.MinHitSize {
ui.Text(c, core.Msg(c, "image.error")).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.Danger).SingleLine()
}
})
case opts.Loading:
box.Role(ui.RoleImage).Label(core.Msg(c, "image.loading")).Description(opts.Alt)
back.Children(func() {
core.Spinner(c, core.SpinnerOptions{Size: max(8, min(20, room.H-4)), Label: core.Msg(c, "image.loading")})
})
default:
box.Role(ui.RoleImage).Label(core.Msg(c, "image.placeholder")).Description(opts.Alt)
back.Children(func() { Icon(c, "image", IconOptions{Size: 24, Tone: IconMuted}) })
}
}
return retry
}
// imageSized builds the outer box and its picture, sized alike in every state.
func imageSized(c *ui.Context, src ui.ImageSource, opts ImageOptions, layers func(box, pic ui.Element)) ui.Element {
if src == nil {
src = imageBlank
}
box := ui.Box(c)
box.Children(func() {
pic := ui.Image(c, src).Radius(theme.ControlRadius)
if opts.Crop {
pic.Fit(ui.Cover)
}
if opts.Width > 0 {
box.Width(opts.Width)
pic.Width(opts.Width)
}
if opts.Height > 0 {
box.Height(opts.Height)
pic.Height(opts.Height)
}
layers(box, pic)
})
return box
}
// imageRetry is the 28 DIP icon-only retry button.
func imageRetry(c *ui.Context, k theme.Tokens, problem string) ui.Element {
b := ui.ButtonBase(c).Size(theme.MinHitSize, theme.MinHitSize).Shrink(0).Radius(theme.ControlRadius).
Label(core.Msg(c, "image.retry")).Description(problem).Tooltip(problem)
bg := k.Surface
if b.Pressed() {
bg = k.Selection
} else if b.Hovered() {
bg = k.SurfaceHover
}
b.Background(bg).Border(theme.BorderWidth, k.ControlBorder).TextColor(k.Danger)
internal.Ring(b, k.Focus, theme.ControlRadius)
b.Transition(core.ColorFade)
b.Children(func() { Icon(c, "refresh-cw", IconOptions{Size: 16}) })
return b
}