A cross-cutting pass, each fix with a test that failed first. Veils: MyGo still hands a key to a base that turned off this frame, so every element key now goes through pixel.Shortcut, which an archtest enforces. Input handlers record and the build applies them only while enabled; the editor turns away a veil-frame letter or copy; value bases get a copy and keep only enabled turns. Popups give a held focus back to their opener when the veil lifts. Motion: S5 is enforced at the clock. Every Tick, Since, Step, Toward and Shot runs at 4-15 fps; blinks keep 1 Hz on 4 fps steps, fast animations step at 15, and drawn time is quantized to frames. Clocks stepping back: Since holds at zero without overflow, and k.Elapsed moves a stored start to now, so nothing goes negative, runs backward or indexes out of range. Also: logs count typed text instead of holding it, gallery names use glyphs the font has, removable rows are keyed, components take their own Options (archtest), and sweeps over all 508 demos check veiled keys, clicks, wheels, the clock stepping back and names.
72 lines
1.8 KiB
Go
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
|
|
}
|