Files
8bitMyGoUI/chat/linkpreview.go
T
ZacharyZhang-NY 455f5f2a05 Add chat components 321-345; named List rows, inert scroll views
Speech bubbles, a chat log that follows its end, a composer that sends
on Enter, typing dots, chat and channel lists, read receipts,
reactions, emoji and sticker pickers, mentions, date and unread rules,
system lines, member lists, user cards, a mailbox, mail reader and
letter, a notification bell, call controls, a voice meter, attachment
chips, link previews and polls.

pixel's editor takes EnterSends; display's Bubble takes a Fill and
exports EmojiSprite. data.List names its rows, which also lets typing
choose one. layout.ScrollArea and input.TextArea hold still under
Inert, and an inert scroll view leaves the Tab order until it wakes.
2026-10-12 05:02:36 -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
}