Files
8bitMyGoUI/datetime/weekstrip.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.9 KiB
Go

package datetime
import (
"log/slog"
"strconv"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type WeekStripOptions struct {
Disabled bool
}
type WeekStripResult struct {
Element ui.Element
Changed bool
}
// day is a week strip cell in dots.
const dayW, dayH = 22, 2 * pixel.LineHeight
// WeekStrip shows *date's week; Left and Right step days.
func WeekStrip(c *ui.Context, text string, date *time.Time, o WeekStripOptions) WeekStripResult {
k := pixel.K(c)
old := *date
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleGroup).Label(text).Disabled(o.Disabled)
})
if pixel.Shortcut(base, 0, ui.KeyLeft) {
*date = date.AddDate(0, 0, -1)
}
if pixel.Shortcut(base, 0, ui.KeyRight) {
*date = date.AddDate(0, 0, 1)
}
first := date.AddDate(0, 0, -int(date.Weekday()))
// Enter and Space are taken, so clicks come from pointers.
pixel.Shortcut(base, 0, ui.KeyEnter)
pixel.Shortcut(base, 0, ui.KeySpace)
if base.Clicked() {
x, _, _ := base.PointerPosition()
if i := int(x / k.D(dayW)); x >= 0 && i < 7 {
*date = first.AddDate(0, 0, i)
}
}
base.Description(date.Format("2006-01-02"))
names, chosen, now := weekdays(k), *date, today(c, k)
shell.Size(k.D(7*dayW), k.D(dayH)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
for i := range 7 {
d, x := first.AddDate(0, 0, i), i*dayW
if sameDay(d, chosen) {
g.Frame(x, 0, dayW, dayH, pixel.Sunken, pixel.Well)
}
n := strconv.Itoa(d.Day())
g.Text(x+(dayW-pixel.Measure(names[i]))/2, 0, names[i], pixel.Faint, 1)
g.Text(x+(dayW-pixel.Measure(n))/2, pixel.LineHeight-2, n, pixel.Ink, 1)
if sameDay(d, now) {
g.HLine(x+4, dayH-2, dayW-8, pixel.Accent)
}
}
g.Finish(st)
})
res := WeekStripResult{Element: shell, Changed: !date.Equal(old)}
if res.Changed {
slog.Debug("8bit: week strip", "label", text, "date", date.Format("2006-01-02"))
}
return res
}