Files
8bitMyGoUI/datetime/alarm.go
T
ZacharyZhang-NY a2f2796455 Add datetime components 196-215; Kit.At; inert stepper arrows
Calendars, date and time inputs, pickers, clocks, timers, a sun dial,
a season wheel, an agenda and a day schedule. Kit.At rebuilds at a
moment (relative times, midnight). Stepper and NumberInput arrows stop
under Inert. DateInput parks a popup focus as its own.
2026-10-11 23:56:10 -04:00

67 lines
1.6 KiB
Go

package datetime
import (
"fmt"
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// AlarmEntry is an alarm: time, days, and if it rings.
type AlarmEntry struct {
Hour, Minute int
// Days ring on, Sunday first.
Days [7]bool
On bool
}
type AlarmOptions struct {
Disabled bool
}
type AlarmResult struct {
Element *ui.Element
Changed bool
}
// Alarm lists alarms: time, days and a lever each.
func Alarm(c *ui.Context, text string, alarms []AlarmEntry, o AlarmOptions) AlarmResult {
k := pixel.K(c)
col := ui.Column(c).Gap(k.D(4)).Role(ui.RoleList).Label(text)
res := AlarmResult{Element: col}
names := weekdays(k)
col.Children(func() {
for i := range alarms {
a := &alarms[i]
if a.Hour < 0 || a.Hour > 23 || a.Minute < 0 || a.Minute > 59 {
panic(fmt.Sprintf("datetime: alarm %02d:%02d is no time", a.Hour, a.Minute))
}
at := fmt.Sprintf("%02d:%02d", a.Hour, a.Minute)
ui.Row(c).Gap(k.D(8)).AlignItems(ui.Center).Role(ui.RoleListItem).Label(at).Children(func() {
ink := pixel.Faint
if a.On {
ink = pixel.Ink
}
pixel.Text(c, at, pixel.TextOptions{Slot: ink, Scale: 2})
ui.Row(c).Gap(k.D(3)).Children(func() {
for d, n := range names {
day := pixel.Faint
if a.Days[d] {
day = pixel.Accent
}
pixel.Text(c, n, pixel.TextOptions{Slot: day})
}
})
if input.Switch(c, "", &a.On, input.SwitchOptions{Label: at, Disabled: o.Disabled}).Changed {
res.Changed = true
slog.Debug("8bit: alarm", "label", text, "at", at, "on", a.On)
}
})
}
})
return res
}