Files
8bitMyGoUI/chat/mailbox.go
T
ZacharyZhang-NYandClaude Opus 5.5 03e1f91706 Upgrade MyGo to v0.5.5
Elements are now pass-scoped value handles: tests that read bounds
after a frame bind a ui.Handle. pixel tells frames apart by the build
pass's root, since the Tester clock no longer moves on its own.

Fix the regressions the upgrade brought:
- Clipboard reads in input handlers go through c.Services().
- MyGo notes an overlay's opener after building it, so AutoFocus
  inside lost it; pixel.AutoFocus focuses through a Handle, and
  archtest bans the raw call. Dialogs hand the focus back again.
- Elements around a press stay hovered: Tooltip tells a press-given
  focus by focus arriving under the pointer.
- The web host moves the Tester clock with real time, at most 500 ms
  a call, and gives components the real clock.

The gallery's source index also accepts CRLF checkouts.

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

67 lines
1.9 KiB
Go

package chat
import (
"log/slog"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// Mail is a letter; Read opens its envelope.
type Mail struct {
From, Subject, Body string
At time.Time
Read bool
}
type MailboxOptions struct {
// Width and Height are dots; zeros mean 220 by 120.
Width, Height int
Disabled bool
}
type MailboxResult struct {
Element ui.Element
Changed bool
// Opened is the mail opened this frame, else -1.
Opened int
}
// Mailbox lists envelopes; Enter or a double click opens.
func Mailbox(c *ui.Context, text string, mails []Mail, selected *int, o MailboxOptions) MailboxResult {
k := pixel.K(c)
w := pixel.Or("Mailbox Width", o.Width, 220)
room := w - 14 - 16 - 30
name := func(i int) string {
m := mails[i]
if !m.Read {
return m.From + ": " + m.Subject + ", " + k.Msg("new_messages")
}
return m.From + ": " + m.Subject
}
r := list(c, k, text, len(mails), selected, w, pixel.Or("Mailbox Height", o.Height, 120), o.Disabled, name, func(i int) {
m := mails[i]
env, ink, from := icon.MailOpen, pixel.Faint, pixel.Faint
if !m.Read {
env, ink, from = icon.Mail, pixel.Accent, pixel.Ink
}
ui.Box(c).FillWidth().Height(k.D(2 * pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Sprite(0, 4, env, 0, ink, 1)
g.Text(16, 0, line(m.From, room), from, 1)
g.Text(16, pixel.LineHeight, line(m.Subject, room+28), pixel.Faint, 1)
t := clock(m.At)
g.Micro(g.W-pixel.MicroWidth(t)-2, 3, t, pixel.Faint, 1)
})
})
res := MailboxResult{Element: r.Element, Changed: r.Changed, Opened: -1}
if r.Submitted && *selected >= 0 {
mails[*selected].Read, res.Opened = true, *selected
slog.Debug("8bit: mailbox opened", "label", text, "mail", *selected)
}
return res
}