Files
8bitMyGoUI/chart/calendarheatmap.go
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

63 lines
1.8 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package chart
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type CalendarHeatmapOptions struct {
// Slot is the busiest days' color; zero means Good.
Slot pixel.Slot
}
// day is a 4×4 square plus its gap, in dots.
const day = 5
// CalendarHeatmap rates a year's UTC days 0–4, week by week.
func CalendarHeatmap(c *ui.Context, text string, year int, level func(d time.Time) int, o CalendarHeatmapOptions) ui.Element {
k := pixel.K(c)
ink := o.Slot
if ink == 0 {
ink = pixel.Good
}
first := time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC)
skip := int(first.Weekday())
levels := make([]int, first.AddDate(1, 0, 0).Sub(first)/(24*time.Hour))
sums := make([]float64, 12)
for i := range levels {
d := first.AddDate(0, 0, i)
l := level(d)
if l < 0 || l > 4 {
panic(fmt.Sprintf("chart: CalendarHeatmap level %d outside 0–4", l))
}
levels[i], sums[d.Month()-1] = l, sums[d.Month()-1]+float64(l)
}
months := strings.Fields(k.Msg("months"))
sm := small(months...)
head := labelH(months)
weeks := (skip + len(levels) + 6) / 7
// Assistive technology hears each month's summed levels.
return ui.Box(c).Size(k.D(weeks*day), k.D(head+7*day)).Role(ui.RoleImage).Label(text + " " + strconv.Itoa(year)).Description(read(months, sums)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
// Month names top their first week, room permitting.
next := 0
for m, name := range months {
x := (skip + time.Date(year, time.Month(m+1), 1, 0, 0, 0, 0, time.UTC).YearDay() - 1) / 7 * day
if x >= next {
mark(g, x, 1, name, sm, pixel.Faint)
next = x + markW(name, sm) + 3
}
}
for i, l := range levels {
n := skip + i
g.Dither(n/7*day, head+n%7*day, day-1, day-1, pixel.Shade, ink, l*4)
}
})
}