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

94 lines
3.3 KiB
Go

package chat
import (
"fmt"
"log/slog"
"math"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// Attachment describes a caller-uploaded file.
type Attachment struct {
// Name is required.
Name string
// Type supplies MIME metadata; empty uses the filename.
Type string
Size int64
// Uploading shows Progress, 0 to 1.
Uploading bool
Progress float64
// Err shows a failed upload with retry.
Err error
}
// AttachmentChipResult carries the chip and requested action.
type AttachmentChipResult struct {
Element ui.Element
removed, retried bool
}
// Removed reports a press of the remove button.
func (r AttachmentChipResult) Removed() bool { return r.removed }
// Retried reports a press of retry on a failed upload.
func (r AttachmentChipResult) Retried() bool { return r.retried }
// AttachmentChip displays upload status, retry and removal controls.
func AttachmentChip(c *ui.Context, a Attachment) AttachmentChipResult {
if a.Name == "" || a.Size < 0 {
panic("mujica: Attachment needs a Name and a non-negative Size")
}
if a.Uploading && !(a.Progress >= 0 && a.Progress <= 1) {
panic("mujica: Attachment Progress must be from 0 to 1")
}
k := core.Tokens(c)
u := core.Density(c).Unit()
var r AttachmentChipResult
border := k.Border
if a.Err != nil {
border = k.Danger
}
r.Element = ui.Row(c).Gap(u*2).AlignItems(ui.Center).Padding(u, u, u, u*2).Width(220).
Radius(theme.ControlRadius).Background(k.Surface).Border(theme.BorderWidth, border).Role(ui.RoleGroup).Label(a.Name)
disabled := r.Element.IsDisabled()
r.Element.Children(func() {
icon, color := fileMessageIcon(a.Name, a.Type), k.AccentText
if a.Err != nil {
icon, color = "circle-alert", k.Danger
}
ui.Icon(c, icons.Must(icon)).Size(20, 20).Shrink(0).TextColor(color)
ui.Column(c).Grow(1).MinWidth(0).Gap(u / 2).Children(func() {
ui.Text(c, a.Name).SingleLine().FontSize(core.FontSize(c, theme.CaptionSize)).FontWeight(600)
small := core.FontSize(c, theme.CaptionSize)
switch {
case a.Err != nil:
ui.Text(c, core.Msg(c, "attachment_chip.failed")).SingleLine().FontSize(small).TextColor(k.Danger)
case a.Uploading:
pct := int(math.Round(a.Progress * 100))
ui.Text(c, fmt.Sprintf(core.Msg(c, "attachment_chip.uploading"), pct)).SingleLine().FontSize(small).TextColor(k.TextMuted).FontFeatures("tnum")
frac := float32(a.Progress)
ui.Box(c).Height(3).Radius(1.5).Background(k.Border).Role(ui.RoleProgress).Label(fmt.Sprintf(core.Msg(c, "attachment_chip.uploading"), pct)).
Draw(func(p *ui.Painter, b ui.Rect) {
p.Fill(ui.Rect{X: b.X, Y: b.Y, W: b.W * frac, H: b.H}, k.AccentText, 1.5)
})
default:
ui.Text(c, fileMessageSize(c, a.Size)).SingleLine().FontSize(small).TextColor(k.TextMuted)
}
})
if a.Err != nil && core.IconAction(c, icons.Must("refresh-cw"), fmt.Sprintf(core.Msg(c, "attachment_chip.retry"), a.Name)).Clicked() && !disabled {
r.retried = true
slog.Debug("mujica: attachment_chip retry")
}
if core.IconAction(c, icons.Must("x"), fmt.Sprintf(core.Msg(c, "attachment_chip.remove"), a.Name)).Clicked() && !disabled {
r.removed = true
slog.Debug("mujica: attachment_chip remove")
}
})
return r
}