Files
8bitMyGoUI/desk/clipboardpanel.go
T
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

89 lines
2.4 KiB
Go

package desk
import (
"fmt"
"log/slog"
"strings"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type ClipboardPanelOptions struct {
// Width is in dots; zero means 200.
Width int
Disabled bool
}
type ClipboardPanelResult struct {
Element ui.Element
// Copied is the clip copied this frame, else -1.
Copied int
}
type clipKey struct{}
// copied is the clip last copied and when.
type copied struct {
clip string
at time.Time
}
// ClipboardPanel lists recent clips; a click copies one back.
func ClipboardPanel(c *ui.Context, text string, clips []string, o ClipboardPanelOptions) ClipboardPanelResult {
k := pixel.K(c)
seen := map[string]bool{}
for i, s := range clips {
if s == "" || seen[s] {
panic(fmt.Sprintf("desk: ClipboardPanel clip %d is blank or twice", i))
}
seen[s] = true
}
w := pixel.Or("ClipboardPanel Width", o.Width, 200)
col := ui.Column(c).Width(k.D(w)).Gap(k.D(2)).Role(ui.RoleList).Label(text)
last := ui.Local(col, clipKey{}, func() copied { return copied{} })
// COPIED! shows for two seconds on the clip copied.
shown := ""
if until := last.at.Add(2 * time.Second); k.Now().Before(until) {
shown = last.clip
k.At(c, until)
}
res := ClipboardPanelResult{Copied: -1}
res.Element = col.Children(func() {
if len(clips) == 0 {
pixel.Text(c, k.Msg("empty"), pixel.TextOptions{Slot: pixel.Faint})
}
for i, clip := range clips {
// Keyed by the clip: new ones come in on top.
ui.Box(c).Key(clip).FillWidth().Children(func() {
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleButton).Label(clip).Description(k.Msg("copy")).Disabled(o.Disabled)
})
if !base.IsDisabled() && base.Clicked() {
c.WriteClipboard(clip)
res.Copied, *last = i, copied{clip, k.Now()}
}
done := clip == shown || res.Copied == i
line := strings.Fields(clip)
shell.FillWidth().Height(k.D(pixel.LineHeight + 6)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Frame(0, 0, g.W, g.H, pixel.Raised, pixel.Face)
mark := ""
if done {
mark = k.Msg("copied")
g.Text(g.W-4-pixel.Measure(mark), 3, mark, pixel.Good, 1)
}
g.Text(4, 3, pixel.Wrap(strings.Join(line, " "), g.W-12-pixel.Measure(mark), 1)[0], pixel.Ink, 1)
g.Finish(st)
})
})
}
})
if res.Copied >= 0 {
slog.Debug("8bit: clip copied", "label", text, "clip", res.Copied)
}
return res
}