Date & Time: review r2 fixes

This commit is contained in:
ZacharyZhang-NY committed 2026-10-05 13:01:34 -04:00
1 parent cfdccfec5c
commit cb5a77a472
5 files changed
+275 -86

No files matched your search

+20 -84
View File
@@ -259,11 +259,11 @@ func timeSegment(c *ui.Context, draft *TimeOfDay, part timePart, cfg timeConfig,
internal.Ring(seg, k.Focus, theme.ControlRadius)
typed := ui.Local(seg, "time-typed", func() string { return "" })
badText := ui.Local(seg, "time-bad", func() string { return "" })
badFor := ui.Local(seg, "time-bad-for", func() TimeOfDay { return TimeOfDay{} })
badFor := ui.Local(seg, "time-bad-for", func() int { return 0 })
if !seg.Focused() {
*typed = ""
}
if *badFor != *draft {
if *badFor != timePartValue(*draft, part, cfg) {
*badText = ""
}
set := func(t TimeOfDay) {
@@ -275,7 +275,7 @@ func timeSegment(c *ui.Context, draft *TimeOfDay, part timePart, cfg timeConfig,
}
t := *draft
lo, hi := timeLimits(part, cfg)
if !cfg.readOnly {
if !cfg.readOnly && !seg.IsDisabled() {
switch {
case seg.Shortcut(0, ui.KeyUp):
set(timeStepPart(t, part, cfg, 1))
@@ -299,7 +299,7 @@ func timeSegment(c *ui.Context, draft *TimeOfDay, part timePart, cfg timeConfig,
set(timeWithPart(t, part, v, cfg))
done = complete
case complete:
*badText, *badFor = fmt.Sprintf("%02d", v), *draft
*badText, *badFor = fmt.Sprintf("%02d", v), timePartValue(*draft, part, cfg)
slog.Debug("mujica: time_picker invalid segment", "part", int(part), "typed", v)
}
}
@@ -349,6 +349,22 @@ func timeSegmentDescription(c *ui.Context, part timePart, text string, lo, hi in
return fmt.Sprintf(msg(c, "time_picker.segment"), strings.TrimSpace(text), lo, hi)
}
// timePartValue is the value a segment shows, so other segments' edits keep its error.
func timePartValue(t TimeOfDay, part timePart, cfg timeConfig) int {
switch part {
case timeHourPart:
if cfg.hour12 {
return timeHour12(t.Hour)
}
return t.Hour
case timeMinutePart:
return t.Minute
case timeSecondPart:
return t.Second
}
return t.Hour / 12
}
// timeStepPart steps one segment of t by dir, wrapping round its cycle.
func timeStepPart(t TimeOfDay, part timePart, cfg timeConfig, dir int) TimeOfDay {
switch part {
@@ -408,83 +424,3 @@ func timeType(typed *string, n, lo, hi int) (v int, ok, complete bool) {
*typed = s
return v, v >= lo && v <= hi, complete
}
// timeColumns lists choosable values per segment and reports whether a choice edited *draft.
func timeColumns(c *ui.Context, draft *TimeOfDay, cfg timeConfig, fresh bool) (edited bool) {
k := Tokens(c)
d := Density(c)
h := d.ControlHeight() - 4
within := func(lo, hi TimeOfDay) bool { return hi.Compare(cfg.min) >= 0 && lo.Compare(cfg.max) <= 0 }
ui.Row(c).Gap(d.Unit()).Children(func() {
for _, part := range timeParts(cfg) {
t := *draft
var options []TimeOfDay
switch part {
case timeHourPart:
for v := range 24 {
if !cfg.hour12 || v/12 == t.Hour/12 {
options = append(options, TimeOfDay{v, t.Minute, t.Second})
}
}
case timeMinutePart:
for v := 0; v < 60; v += cfg.minuteStep {
options = append(options, TimeOfDay{t.Hour, v, t.Second})
}
case timeSecondPart:
for v := 0; v < 60; v += cfg.secondStep {
options = append(options, TimeOfDay{t.Hour, t.Minute, v})
}
case timePeriodPart:
options = []TimeOfDay{{t.Hour % 12, t.Minute, t.Second}, {t.Hour%12 + 12, t.Minute, t.Second}}
}
name := msg(c, timePartNames[part])
ui.Scroll(c).Height(h * 6).Width(d.ControlHeight() * 1.5).Children(func() {
ui.Column(c).FocusGroup(ui.Vertical).Role(ui.RoleRadioGroup).Label(name).Children(func() {
for _, o := range options {
var lo, hi TimeOfDay
text := ""
switch part {
case timeHourPart:
lo, hi = TimeOfDay{o.Hour, 0, 0}, TimeOfDay{o.Hour, 59, 59}
text = fmt.Sprintf("%02d", o.Hour)
if cfg.hour12 {
text = fmt.Sprintf("%d", timeHour12(o.Hour))
}
case timeMinutePart:
lo, hi = TimeOfDay{o.Hour, o.Minute, 0}, TimeOfDay{o.Hour, o.Minute, 59}
text = fmt.Sprintf("%02d", o.Minute)
case timeSecondPart:
lo, hi = o, o
text = fmt.Sprintf("%02d", o.Second)
case timePeriodPart:
lo, hi = TimeOfDay{o.Hour / 12 * 12, 0, 0}, TimeOfDay{o.Hour/12*12 + 11, 59, 59}
text = timePeriod(c, o.Hour)
}
sel := t
item := ui.RadioBase(c, &sel, o).Height(h).FillWidth().Center().Radius(theme.ControlRadius).
Disabled(!within(lo, hi)).Label(text)
fg := k.Text
switch {
case o == t:
item.Background(k.Accent)
fg = k.OnAccent
if fresh {
item.ScrollIntoView()
}
case item.Pressed():
item.Background(k.Selection)
case item.Hovered():
item.Background(k.SurfaceHover)
}
internal.Ring(item, k.Focus, theme.ControlRadius)
item.Children(func() { ui.Text(c, text).TextColor(fg).FontFeatures("tnum") })
if item.Clicked() && o != *draft {
*draft, edited = o, true
}
}
})
})
}
})
return edited
}