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>
107 lines
2.9 KiB
Go
107 lines
2.9 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/data"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/display"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
|
|
)
|
|
|
|
// GridImage is one picture of an ImageGrid; the caller loads it.
|
|
type GridImage struct {
|
|
Source ui.ImageSource
|
|
Alt string
|
|
// Loading and Err show the caller's load in progress or failure.
|
|
Loading bool
|
|
Err error
|
|
}
|
|
|
|
// ImageGridResult is an ImageGrid's element and a retry the user asked for.
|
|
type ImageGridResult struct {
|
|
Element ui.Element
|
|
retry int
|
|
}
|
|
|
|
// Retried returns the index of the failed picture whose retry was pressed this frame.
|
|
func (r ImageGridResult) Retried() (int, bool) { return r.retry, r.retry >= 0 }
|
|
|
|
// imageGridState is the viewer opened from the grid.
|
|
type imageGridState struct {
|
|
open bool
|
|
index int
|
|
}
|
|
|
|
// ImageGrid lays out 1 to 9 pictures; a click on a loaded one opens the ImageViewer.
|
|
func ImageGrid(c *ui.Context, images []GridImage) ImageGridResult {
|
|
n := len(images)
|
|
if n < 1 || n > 9 {
|
|
panic("mujica: ImageGrid shows 1 to 9 images")
|
|
}
|
|
k := core.Tokens(c)
|
|
u := core.Density(c).Unit()
|
|
cols := 3
|
|
switch n {
|
|
case 1:
|
|
cols = 1
|
|
case 2, 4:
|
|
cols = 2
|
|
}
|
|
cell := float32(120)
|
|
if n == 1 {
|
|
cell = 280
|
|
}
|
|
r := ImageGridResult{retry: -1}
|
|
r.Element = ui.Grid(c).Columns(cols).Gap(u).AlignSelf(ui.Start)
|
|
st := ui.Local(r.Element, "viewer", func() imageGridState { return imageGridState{} })
|
|
var viewable []data.ViewerImage
|
|
var at []int
|
|
for i, im := range images {
|
|
if im.Source != nil && !im.Loading && im.Err == nil {
|
|
at = append(at, i)
|
|
viewable = append(viewable, data.ViewerImage{Source: im.Source, Alt: im.Alt})
|
|
}
|
|
}
|
|
disabled := r.Element.IsDisabled()
|
|
r.Element.Children(func() {
|
|
for i, im := range images {
|
|
loaded := im.Source != nil && !im.Loading && im.Err == nil
|
|
// Only a loaded picture is a button; a failed one keeps its retry.
|
|
var b ui.Element
|
|
if loaded {
|
|
b = ui.ButtonBase(c).Label(im.Alt)
|
|
internal.Ring(b, k.Focus, theme.ControlRadius)
|
|
} else {
|
|
b = ui.Box(c)
|
|
}
|
|
b.Size(cell, cell).Radius(theme.ControlRadius).Clip().Children(func() {
|
|
v := display.Image(c, im.Source, display.ImageOptions{Width: cell, Height: cell, Crop: true, Alt: im.Alt, Loading: im.Loading, Err: im.Err})
|
|
if v.Retry && !disabled {
|
|
r.retry = i
|
|
slog.Debug("mujica: image_grid retry", "index", i)
|
|
}
|
|
})
|
|
if loaded && b.Clicked() && !disabled {
|
|
for vi, ii := range at {
|
|
if ii == i {
|
|
st.open, st.index = true, vi
|
|
}
|
|
}
|
|
slog.Debug("mujica: image_grid open", "index", i)
|
|
}
|
|
}
|
|
})
|
|
if disabled {
|
|
st.open = false
|
|
}
|
|
if st.open && len(viewable) > 0 {
|
|
st.index = min(st.index, len(viewable)-1)
|
|
data.ImageViewer(c, &st.open, &st.index, viewable)
|
|
}
|
|
return r
|
|
}
|