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

72 lines
1.8 KiB
Go

package chat
import (
"fmt"
"log/slog"
"strconv"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// Reaction is an emoji count; Mine marks your own.
type Reaction struct {
Emoji string
Count int
Mine bool
}
type ReactionsOptions struct {
Disabled bool
}
type ReactionsResult struct {
Element ui.Element
// Toggled is the chip clicked this frame, else -1.
Toggled int
}
// Reactions shows emoji chips with counts; a click toggles yours.
func Reactions(c *ui.Context, text string, rs []Reaction, o ReactionsOptions) ReactionsResult {
k := pixel.K(c)
res := ReactionsResult{Toggled: -1}
res.Element = ui.Row(c).Gap(k.D(2)).FocusGroup(ui.Horizontal).Role(ui.RoleGroup).Label(text).Children(func() {
for i := range rs {
r := &rs[i]
if r.Count < 0 || r.Mine && r.Count == 0 {
panic(fmt.Sprintf("chat: reaction %q counts %d, mine %v", r.Emoji, r.Count, r.Mine))
}
art := emoji(r.Emoji)
// The base turns before it learns it is off.
v := r.Mine
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.ToggleBase(c, &v).Label(fmt.Sprintf("%s %d", r.Emoji, r.Count)).Disabled(o.Disabled).FocusRing(false)
})
if v != r.Mine && !base.IsDisabled() {
r.Mine = v
if r.Mine {
r.Count++
} else {
r.Count--
}
res.Toggled = i
slog.Debug("8bit: reaction toggled", "label", text, "emoji", r.Emoji, "mine", r.Mine)
}
n, mine := strconv.Itoa(r.Count), r.Mine
shell.Size(k.D(20+pixel.MicroWidth(n)), k.D(16)).Draw(func(p *ui.Painter, rc ui.Rect) {
g := k.Canvas(p, rc)
fill, ink := pixel.Face, pixel.Ink
if mine {
fill, ink = pixel.Accent, pixel.AccentInk
}
g.Frame(0, 0, g.W, g.H, pixel.Notched, fill)
g.Sprite(2, 2, art.sp, 0, art.ink, 1)
g.Micro(16, 6, n, ink, 1)
g.Finish(st)
})
}
})
return res
}