Files
MujicaUI/media/image_compare.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

78 lines
2.6 KiB
Go

package media
import (
"log/slog"
"math"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/data"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// ImageCompareOptions configure an ImageCompare.
type ImageCompareOptions struct {
// BeforeLabel and AfterLabel caption the sides; empty uses defaults.
BeforeLabel, AfterLabel string
Disabled bool
}
// ImageCompare shows before left of *split, after right.
func ImageCompare(c *ui.Context, before, after ui.ImageSource, split *float64, opts ImageCompareOptions) core.ChoiceResult {
if *split < 0 || *split > 1 || math.IsNaN(*split) {
panic("mujica: ImageCompare split outside 0..1")
}
bw, bh := data.ViewerSize(before)
data.ViewerSize(after)
k := core.Tokens(c)
bl, al := opts.BeforeLabel, opts.AfterLabel
if bl == "" {
bl = core.Msg(c, "image_compare.before")
}
if al == "" {
al = core.Msg(c, "image_compare.after")
}
g, _ := input.SliderSetup(0, 1, 0.01, nil, false, c)
old := *split
var s ui.Element
var next float64
ui.Box(c).Disabled(opts.Disabled).Children(func() {
s, next, _ = input.SliderControl(c, old, g, 0, 1)
})
s.FillWidth().AspectRatio(bw / bh).FocusRing(false).Cursor(ui.CursorResizeEW).Clip().Radius(theme.ControlRadius).
Label(bl + " / " + al)
internal.Ring(s, k.Focus, theme.ControlRadius)
if !s.IsDisabled() {
*split = next
}
changed := *split != old
if changed {
slog.Debug("mujica: image_compare split", "split", *split)
}
v, size := float32(*split), core.FontSize(c, theme.CaptionSize)
s.Draw(func(p *ui.Painter, r ui.Rect) {
x := internal.Snap(p, r.X+r.W*v)
p.Image(after, r, ui.Cover)
p.Clip(ui.Rect{X: r.X, Y: r.Y, W: x - r.X, H: r.H}, 0, func() { p.Image(before, r, ui.Cover) })
compareTag(p, k, r.X+8, r.Y+8, bl, size)
w, _ := p.MeasureText(1000, ui.Span{Text: al, Size: size})
compareTag(p, k, r.X+r.W-w-20, r.Y+8, al, size)
p.Fill(ui.Rect{X: x - 1, Y: r.Y, W: 2, H: r.H}, k.Surface, 0)
cy := internal.Snap(p, r.Y+r.H/2)
p.FillPath(internal.Diamond(x, cy, 22), k.Surface)
p.FillPath(internal.Diamond(x, cy, 16), k.AccentText)
})
return core.NewChoiceResult(s, changed)
}
// compareTag draws a caption chip at (x, y).
func compareTag(p *ui.Painter, k theme.Tokens, x, y float32, s string, size float32) {
w, h := p.MeasureText(1000, ui.Span{Text: s, Size: size})
b := ui.Rect{X: x, Y: y, W: w + 12, H: h + 4}
p.Fill(b, k.Surface.Alpha(0.88), theme.ControlRadius)
p.Text(b.X+6, b.Y+2, s, size, k.Text)
}