Files
ZacharyZhang-NYandClaude Opus 5.5 bcce34cda1 Upgrade MyGo to v0.5.5
MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:

- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
  elements expire and Context methods are inert: read IsDisabled into a
  local during the build, keep a ui.Handle for Focus, use c.Services()
  for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
  content builds, so same-frame autofocus lost focus restore on close.
  It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
  c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
  split button tooltip no longer reorders its open menu; disabled
  menubar rows pass presses to the panel; Form defers blur-started
  async checks so the growing message no longer moves Submit away
  from the release; Combobox drops its disabled copy now that MyGo
  blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
  time.Sleep/time.Now and read element state inside the view. The web
  mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.

Snapshot goldens are darwin-only and were not re-verified here.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:37:54 -04:00

172 lines
5.5 KiB
Go

package datetime
import (
"fmt"
"log/slog"
"strings"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
)
// DatePickerOptions configure a DatePicker.
type DatePickerOptions struct {
// Layout is the Go time layout typed and shown; empty uses 2006-01-02.
Layout string
// Min and Max bound the dates accepted; zero leaves a side open.
Min, Max core.Date
DisableDate func(core.Date) bool
WeekStart WeekStart
// Today is marked in the calendar; zero uses the system clock.
Today core.Date
// Clearable shows a clear button and accepts empty text as no date.
Clearable bool
// Placeholder shows in the empty field; empty uses the built-in copy.
Placeholder string
Disabled bool
// ReadOnly keeps the text focusable and copyable but blocks edits and the popup.
ReadOnly bool
// Label names the field for assistive technology.
Label string
}
// DatePicker types or picks *value; invalid text is kept and reported.
func DatePicker(c *ui.Context, value *core.Date, opts DatePickerOptions) DateTimeResult {
core.DatetimeCheckDate("DatePicker value", *value)
core.DatetimeCheckBounds("DatePicker", opts.Min, opts.Max)
layout := opts.Layout
if layout == "" {
layout = "2006-01-02"
}
format := func(d core.Date) string {
if d.IsZero() {
return ""
}
return d.Format(layout)
}
d := core.Density(c)
today := core.DatetimeToday(c, opts.Today)
enabled := calendarEnabled(opts.Min, opts.Max, opts.DisableDate)
col := ui.Column(c).Width(d.ControlHeight() * 7).Gap(d.Unit()).AlignItems(ui.Stretch).Disabled(opts.Disabled)
text := ui.Local(col, "date-picker-text", func() string { return format(*value) })
seen := ui.Local(col, "date-picker-seen", func() core.Date { return *value })
problem := ui.Local(col, "date-picker-error", func() string { return "" })
open := ui.Local(col, "date-picker-open", func() bool { return false })
cursor := ui.Local(col, "date-picker-cursor", func() core.Date { return core.Date{} })
wasFocused := ui.Local(col, "date-picker-focused", func() bool { return false })
shownLayout := ui.Local(col, "date-picker-layout", func() string { return layout })
if *shownLayout != layout {
if *text == core.Date.Format(*value, *shownLayout) || value.IsZero() && *text == "" {
*text = format(*value)
}
*shownLayout = layout
}
if *seen != *value {
*seen, *text, *problem = *value, format(*value), ""
}
changed := false
commit := func(to core.Date, how string) {
if to != *value {
slog.Debug("mujica: date_picker change", "from", *value, "to", to, "by", how)
*value = to
changed = true
}
*seen, *text, *problem = to, format(to), ""
}
submit := func() {
s := strings.TrimSpace(*text)
if s == "" && opts.Clearable {
commit(core.Date{}, "text")
return
}
t, err := time.Parse(layout, s)
if err != nil {
*problem = fmt.Sprintf(core.Msg(c, "date_picker.invalid"), format(today))
slog.Debug("mujica: date_picker invalid", "text", s)
return
}
if p := datePickerProblem(c, core.DateOf(t), opts.Min, opts.Max, enabled, format); p != "" {
*problem = p
return
}
commit(core.DateOf(t), "text")
}
openAt := func() {
*open = true
*cursor = *value
if cursor.IsZero() {
*cursor = today
}
}
placeholder := opts.Placeholder
if placeholder == "" {
placeholder = core.Msg(c, "date_picker.placeholder")
}
var field, inputEl ui.Element
col.Children(func() {
field = core.DatetimeField(c, ui.Row(c), *problem != "", opts.ReadOnly)
field.Children(func() {
in := input.TextInputEditor(c, field, text, opts.ReadOnly, ui.TextInputBase).Grow(1).Shrink(1).Placeholder(placeholder).FontFeatures("tnum")
inputEl = in
label := opts.Label
if label == "" {
label = placeholder
}
in.Label(label).Error(*problem)
focused := in.Focused()
if !opts.ReadOnly && !in.IsDisabled() && (in.Submitted() || *wasFocused && !focused && *text != format(*value)) {
submit()
}
*wasFocused = focused
if opts.ReadOnly {
return
}
if opts.Clearable && !value.IsZero() && core.IconAction(c, icons.Must("x"), core.Msg(c, "calendar.clear")).Clicked() {
commit(core.Date{}, "clear")
}
if core.IconAction(c, icons.Must("calendar"), core.Msg(c, "date_picker.open")).Role(ui.RolePopUpButton).Clicked() {
if *open {
*open = false
} else {
openAt()
}
}
})
if !*open && !opts.ReadOnly && (field.Shortcut(0, ui.KeyDown) || field.Shortcut(ui.Alt, ui.KeyDown)) {
openAt()
}
core.DatetimePopup(c, field, open, opts.ReadOnly, func() {
picked, _ := calendarMonth(c, cursor, calendarConfig{
chosen: func(x core.Date) bool { return x == *value },
enabled: enabled,
today: today,
weekStart: opts.WeekStart,
autoFocus: true,
})
if !picked.IsZero() {
commit(picked, "calendar")
*open = false
}
})
core.DatetimeError(c, *problem)
})
return DateTimeResult{Element: col, Input: inputEl, changed: changed}
}
// datePickerProblem explains why an existing day d cannot be chosen, or returns "".
func datePickerProblem(c *ui.Context, d core.Date, lo, hi core.Date, enabled func(core.Date) bool, format func(core.Date) string) string {
switch {
case !lo.IsZero() && d.Compare(lo) < 0:
return fmt.Sprintf(core.Msg(c, "calendar.before_min"), format(lo))
case !hi.IsZero() && d.Compare(hi) > 0:
return fmt.Sprintf(core.Msg(c, "calendar.after_max"), format(hi))
case !enabled(d):
return fmt.Sprintf(core.Msg(c, "calendar.unavailable"), format(d))
}
return ""
}