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

121 lines
4.2 KiB
Go

package files
import (
"fmt"
"log/slog"
"strings"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/code"
"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/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/layout"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// PreviewFile holds caller-provided preview content.
type PreviewFile struct {
Name string
Size int64
// Image previews a picture; Text previews text or code.
Image ui.ImageSource
Text string
// Language highlights code; empty guesses it from the extension.
Language string
}
// FilePreviewOptions configure a FilePreview.
type FilePreviewOptions struct {
// Status shows the caller's read in progress or failed.
Status data.DataStatus
Error string
}
// FilePreviewResult exposes the panel and requested actions.
type FilePreviewResult struct {
Element ui.Element
// Closed, Opened and Retried report requested actions.
Closed, Opened, Retried bool
}
var filePreviewLanguages = map[string]string{"go": "go", "js": "javascript", "ts": "typescript", "py": "python", "json": "json", "sh": "shell", "sql": "sql"}
type previewCodeState struct {
viewer code.CodeViewerState
text string
lines []string
}
// FilePreview presents images, text, code or unsupported-type copy.
func FilePreview(c *ui.Context, f PreviewFile, opts FilePreviewOptions) FilePreviewResult {
if f.Name == "" {
panic("mujica: FilePreview needs a Name")
}
k := core.Tokens(c)
u := core.Density(c).Unit()
var res FilePreviewResult
res.Element = code.CodePanel(c).Role(ui.RoleGroup).Label(fmt.Sprintf(core.Msg(c, "file_preview.label"), f.Name))
codeState := ui.Local(res.Element, "file_preview.code", func() previewCodeState { return previewCodeState{} })
if res.Element.Shortcut(0, ui.KeyEscape) && !res.Element.IsDisabled() {
res.Closed = true
}
res.Element.Children(func() {
code.CodeHeader(c, func() {
FileIcon(c, f.Name, FileIconOptions{})
ui.Text(c, f.Name).FontWeight(600).SingleLine().MinWidth(0).Shrink(1)
if f.Size > 0 {
ui.Text(c, code.ProcessListBytes(uint64(f.Size))).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted).Shrink(0)
}
ui.Spacer(c)
if input.Button(c, core.Msg(c, "file_preview.open"), input.ButtonOptions{Variant: input.Ghost}).MinHeight(theme.MinHitSize).Clicked() {
res.Opened = true
}
if core.IconAction(c, icons.Must("x"), core.Msg(c, "file_preview.close")).Clicked() {
res.Closed = true
}
})
body := ui.Column(c).Grow(1).MinHeight(u * 40).Padding(u * 3)
body.Children(func() {
if opts.Status != data.DataReady {
res.Retried = data.DataPlaceholder(c, opts.Status, opts.Error, "", "")
return
}
lang := f.Language
if lang == "" {
lang = filePreviewLanguages[fileExt(f.Name)]
}
switch {
case f.Image != nil:
ui.Box(c).Grow(1).Center().Clip().Children(func() {
display.Image(c, f.Image, display.ImageOptions{Alt: f.Name}).Element.MaxWidthPercent(100).MaxHeightPercent(100)
})
case lang != "" || FileKindOf(f.Name) == FileCode:
if codeState.lines == nil || codeState.text != f.Text {
codeState.text, codeState.lines = f.Text, strings.Split(f.Text, "\n")
}
if lang == "" {
lang = fileExt(f.Name)
}
code.CodeViewer(c, codeState.lines, &codeState.viewer, code.CodeViewerOptions{Language: lang, Label: f.Name}).Element.Grow(1)
case f.Text != "" || FileKindOf(f.Name) == FileText:
layout.ScrollArea(c, nil, layout.ScrollAreaOptions{Label: f.Name}, func() {
ui.Text(c, f.Text).Font(theme.MonoFont).FontSize(core.FontSize(c, code.CodeSize)).Selectable()
}).Grow(1)
default:
ui.Column(c).Grow(1).Center().Gap(u * 2).Children(func() {
FileIcon(c, f.Name, FileIconOptions{Size: 48})
ui.Text(c, fmt.Sprintf(core.Msg(c, "file_preview.unsupported"), strings.ToUpper(fileExt(f.Name)))).TextColor(k.TextMuted).Role(ui.RoleStatus)
})
}
})
})
if res.Closed || res.Opened {
slog.Debug("mujica: file_preview action", "closed", res.Closed, "opened", res.Opened)
}
return res
}