Files
8bitMyGoUI/code/memorymap.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

60 lines
1.7 KiB
Go

package code
import (
"fmt"
"strings"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// Bank is a stretch of memory and its use.
type Bank struct {
Name string
Used, Size int
}
type MemoryMapOptions struct {
// Columns of blocks; zero means 4.
Columns int
}
// MemoryMap draws each bank as a block filled by use.
func MemoryMap(c *ui.Context, text string, banks []Bank, o MemoryMapOptions) ui.Element {
k := pixel.K(c)
cols := pixel.Or("MemoryMap Columns", o.Columns, 4)
for _, b := range banks {
name := strings.ToUpper(b.Name)
if b.Size <= 0 || b.Used < 0 || b.Used > b.Size || !pixel.MicroOK(name) || pixel.MicroWidth(name) > 36 {
panic(fmt.Sprintf("code: MemoryMap bank %+v", b))
}
}
return ui.Grid(c).Columns(cols).Width(k.D(cols * 44)).Gap(k.D(4)).Role(ui.RoleGroup).Label(text).Children(func() {
for _, b := range banks {
pct := b.Used * 100 / b.Size
label := fmt.Sprintf("%s, %d of %d KB, %d%%", b.Name, b.Used/1024, b.Size/1024, pct)
ui.Box(c).Size(k.D(40), k.D(45)).Role(ui.RoleImage).Label(label).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Frame(0, 0, 40, 32, pixel.Raised, pixel.Face)
g.Frame(3, 3, 34, 26, pixel.Sunken, pixel.Well)
fill := pixel.Good
switch {
case pct >= 80:
fill = pixel.Bad
case pct >= 50:
fill = pixel.Warn
}
// The fill rises from the floor, dithered on top.
if n := b.Used * 22 / b.Size; n > 0 {
g.Rect(5, 27-n, 30, n, fill)
g.Dither(5, 27-n, 30, 1, fill, pixel.Well, 8)
}
g.Micro(20-pixel.MicroWidth(strings.ToUpper(b.Name))/2, 34, strings.ToUpper(b.Name), pixel.Ink, 1)
s := fmt.Sprintf("%d%%", pct)
g.Micro(20-pixel.MicroWidth(s)/2, 40, s, pixel.Faint, 1)
})
}
})
}