91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package mujica
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"mujicaui/internal"
|
|
"mujicaui/theme"
|
|
)
|
|
|
|
// 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
|
|
}
|