Files
8bitMyGoUI/code/codeblock.go
T
ZacharyZhang-NY b4b4d67b92 Audit keyboard, focus, veils, motion and clocks across all 500
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.
2026-10-12 16:03:04 -04:00

66 lines
1.9 KiB
Go

package code
import (
"log/slog"
"strconv"
"strings"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/layout"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type CodeBlockOptions struct {
Disabled bool
}
type CodeBlockResult struct {
Element *ui.Element
Copied bool
}
type copiedKey struct{}
// CodeBlock colors Go-like source by slot, numbers lines, copies.
func CodeBlock(c *ui.Context, text, src string, o CodeBlockOptions) CodeBlockResult {
k := pixel.K(c)
src = tidy(src)
lines, slots := strings.Split(src, "\n"), paint(src)
gutter := pixel.Measure(strconv.Itoa(len(lines))) + 6
var res CodeBlockResult
res.Element = layout.Panel(c, layout.PanelOptions{Style: pixel.Sunken, Fill: pixel.Well, Padding: 3}, func() {
ui.Row(c).FillWidth().AlignItems(ui.Center).Children(func() {
pixel.Text(c, text, pixel.TextOptions{Slot: pixel.Faint}).Grow(1)
e := ui.Box(c)
at := ui.Local(e, copiedKey{}, func() time.Time { return time.Time{} })
// The button says COPIED! for two seconds.
label := k.Msg("copy")
if k.Since(c, *at, 4, 8) < 8 {
label = k.Msg("copied")
}
e.Children(func() {
if input.Button(c, label, input.ButtonOptions{Size: input.Small, Disabled: o.Disabled}).Clicked {
c.WriteClipboard(src)
*at, res.Copied = k.Now(), true
slog.Debug("8bit: code block copied", "label", text, "lines", len(lines))
}
})
})
off := 0
for i, l := range lines {
from := off
off += len(l) + 1
ui.Box(c).Size(k.D(gutter+pixel.Measure(l)), k.D(pixel.LineHeight)).Role(ui.RoleText).Label(l).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
n := strconv.Itoa(i + 1)
g.Text(gutter-6-pixel.Measure(n), 0, n, pixel.Faint, 1)
colored(g, gutter, 0, l, slots[from:from+len(l)])
})
}
})
return res
}