Files
ZacharyZhang-NYandClaude Opus 5.5 03e1f91706 Upgrade MyGo to v0.5.5
Elements are now pass-scoped value handles: tests that read bounds
after a frame bind a ui.Handle. pixel tells frames apart by the build
pass's root, since the Tester clock no longer moves on its own.

Fix the regressions the upgrade brought:
- Clipboard reads in input handlers go through c.Services().
- MyGo notes an overlay's opener after building it, so AutoFocus
  inside lost it; pixel.AutoFocus focuses through a Handle, and
  archtest bans the raw call. Dialogs hand the focus back again.
- Elements around a press stay hovered: Tooltip tells a press-given
  focus by focus arriving under the pointer.
- The web host moves the Tester clock with real time, at most 500 ms
  a call, and gives components the real clock.

The gallery's source index also accepts CRLF checkouts.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:31:08 -04:00

63 lines
1.9 KiB
Go

package files
import (
"log/slog"
"strings"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/data"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type FileGridOptions struct {
// Height is in dots; zero means 140.
Height int
Disabled bool
}
type FileGridResult struct {
Element ui.Element
// Opened is Enter or a double click on a file.
Changed, Opened bool
}
const gridCellW, gridCellH = 64, 64
// FileGrid shows files as big icons over their names.
func FileGrid(c *ui.Context, text string, files []File, selected *int, o FileGridOptions) FileGridResult {
k := pixel.K(c)
pick("FileGrid", selected, len(files))
for _, f := range files {
check("FileGrid", f)
}
g := data.GridView(c, text, len(files), selected, data.GridViewOptions{CellWidth: gridCellW, CellHeight: gridCellH, Height: o.Height, Disabled: o.Disabled}, func(i int) {
ui.Column(c).FillWidth().Padding(k.D(4), 0, 0, 0).Gap(k.D(2)).AlignItems(ui.Center).Children(func() {
FileIcon(c, files[i].Kind, FileIconOptions{Scale: 2})
lines := label(files[i].Name, gridCellW-4)
ui.Box(c).Size(k.D(gridCellW), k.D(2*pixel.LineHeight)).Role(ui.RoleText).Label(files[i].Name).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
for j, l := range lines {
g.Text((gridCellW-pixel.Measure(l))/2, j*pixel.LineHeight, l, pixel.Ink, 1)
}
})
})
})
res := FileGridResult{Element: g.Element, Changed: g.Changed, Opened: g.Submitted && *selected >= 0}
if res.Opened {
slog.Debug("8bit: file grid open", "label", text, "file", *selected)
}
return res
}
// label fits a name in two lines, extension whole.
func label(name string, w int) []string {
if pixel.Measure(name) <= w {
return []string{name}
}
if i := strings.LastIndex(name, "."); i > 0 {
return []string{pixel.Wrap(name[:i], w, 1)[0], pixel.Wrap(name[i:], w, 1)[0]}
}
return pixel.Wrap(name, w, 2)
}