Files
MujicaUI/chat/message_list.go
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

110 lines
3.4 KiB
Go

package chat
import (
"log/slog"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// MessageListState keeps a MessageList's place and what the user has seen.
type MessageListState struct {
list ui.ListState
seen int
started bool
}
// ScrollToEnd shows the newest message and follows new ones again.
func (s *MessageListState) ScrollToEnd() { s.list.ScrollToEnd() }
// AtEnd reports whether the newest message is in view.
func (s *MessageListState) AtEnd() bool { return s.list.AtEnd() }
// MessageListOptions configure a MessageList.
type MessageListOptions struct {
// ID returns message i's stable identity; required.
ID func(i int) string
// Date returns message i's time; set, a separator heads each new day.
Date func(i int) time.Time
// Label returns message i's text for assistive technology and typing to find.
Label func(i int) string
}
// MessageListResult is a MessageList's element and its unread count.
type MessageListResult struct {
Element ui.Element
unread int
}
// Unread returns how many messages arrived since the user scrolled away from the end.
func (r MessageListResult) Unread() int { return r.unread }
// MessageList shows n messages, building only those in view, and follows the end until the user scrolls up.
func MessageList(c *ui.Context, s *MessageListState, n int, opts MessageListOptions, row func(i int)) MessageListResult {
if opts.ID == nil || row == nil {
panic("mujica: MessageList needs ID and row")
}
if n < 0 {
panic("mujica: MessageList with negative message count")
}
u := core.Density(c).Unit()
s.list.Key = func(i int) any { return opts.ID(i) }
s.list.Label = opts.Label
s.list.FollowEnd = true
at := !s.started || s.list.AtEnd()
if at || n < s.seen {
s.seen = n
}
s.started = true
r := MessageListResult{unread: n - s.seen}
r.Element = ui.Box(c).Grow(1).MinHeight(0)
r.Element.Children(func() {
ui.List(c, &s.list, n, func(i int) {
ui.Column(c).Padding(u*2, u*4).Gap(u * 2).Children(func() {
if opts.Date != nil && (i == 0 || !messageListSameDay(opts.Date(i-1), opts.Date(i))) {
messageListDay(c, opts.Date(i))
}
row(i)
})
}).Fill().Justify(ui.End).Label(core.Msg(c, "message_list.label"))
jump := ScrollToBottomButton(c, !at, r.unread)
if jump.Clicked() && !r.Element.IsDisabled() {
s.list.ScrollToEnd()
s.seen = n
slog.Debug("mujica: message_list jump", "unread", r.unread)
}
})
return r
}
func messageListSameDay(a, b time.Time) bool {
ay, am, ad := a.Date()
by, bm, bd := b.Date()
return ay == by && am == bm && ad == bd
}
// messageListDay is a separator naming the day: today, yesterday or the date.
func messageListDay(c *ui.Context, t time.Time) {
k := core.Tokens(c)
u := core.Density(c).Unit()
label := t.Format(core.Msg(c, "message_list.date"))
now := c.Now().In(t.Location())
switch {
case messageListSameDay(t, now):
label = core.Msg(c, "message_list.today")
case messageListSameDay(t, now.AddDate(0, 0, -1)):
label = core.Msg(c, "message_list.yesterday")
}
ui.Row(c).Gap(u*3).AlignItems(ui.Center).Padding(u*2, 0).Role(ui.RoleText).Label(label).Children(func() {
rule := func() {
ui.Box(c).Grow(1).Height(theme.BorderWidth).Background(k.Border)
}
rule()
ui.Text(c, label).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted).SingleLine()
rule()
})
}