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.9 KiB
Go
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
|
|
}
|