Files
8bitMyGoUI/chat/linkpreview.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

62 lines
1.7 KiB
Go

package chat
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type LinkPreviewOptions struct {
// Width is in dots; zero means 200.
Width int
Disabled bool
}
type LinkPreviewResult struct {
Element ui.Element
Clicked bool
}
// LinkPreview: thumbnail, title and site; a click opens url.
func LinkPreview(c *ui.Context, url, title, site string, o LinkPreviewOptions) LinkPreviewResult {
k := pixel.K(c)
w := pixel.Or("LinkPreview Width", o.Width, 200)
if w < 100 {
panic("chat: a LinkPreview needs 100 dots")
}
lines := pixel.Wrap(title, w-56, 2)
// The site sits under the title; the card fits both.
sy := 4 + len(lines)*pixel.LineHeight
h := max(40, sy+12)
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleLink).Label(title).Description(url).Disabled(o.Disabled).Cursor(ui.CursorPointer).FocusRing(false)
})
shell.Size(k.D(w), k.D(h)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
fill := pixel.Face
if st.Hover {
fill = pixel.Lit
}
g.Frame(0, 0, w, h, pixel.Window, fill)
// The thumbnail is a placeholder: dithered, with a picture icon.
g.Frame(4, 4, 40, 32, pixel.Sunken, pixel.Well)
g.Dither(6, 6, 36, 28, pixel.Well, pixel.Shade, 4)
g.Sprite(18, 14, icon.Image, 0, pixel.Faint, 1)
for i, l := range lines {
g.Text(50, 3+i*pixel.LineHeight, l, pixel.Ink, 1)
}
tag(g, 50, sy, line(site, w-56), pixel.Faint)
g.Finish(st)
})
res := LinkPreviewResult{Element: shell}
if base.Clicked() {
c.OpenURL(url)
res.Clicked = true
slog.Debug("8bit: link opened", "url", url)
}
return res
}