Files
8bitMyGoUI/account/sessionlist.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

75 lines
2.2 KiB
Go

package account
import (
"fmt"
"log/slog"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/datetime"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// Session is a signed-in device and when it was seen.
type Session struct {
Device string
Icon *pixel.Sprite
Seen time.Time
Current bool
}
type SessionListOptions struct {
// Width is in dots; zero means 240.
Width int
Disabled bool
}
type SessionListResult struct {
Element ui.Element
// SignedOut is the session signed out this frame, else -1.
SignedOut int
}
// SessionList lists signed-in devices, each with SIGN OUT.
func SessionList(c *ui.Context, text string, sessions []Session, o SessionListOptions) SessionListResult {
k := pixel.K(c)
// Rows are keyed by device, so focus follows its row.
seen := map[string]bool{}
for _, s := range sessions {
if seen[s.Device] {
panic(fmt.Sprintf("account: SessionList has %q twice", s.Device))
}
seen[s.Device] = true
}
res := SessionListResult{SignedOut: -1}
res.Element = ui.Column(c).Width(k.D(pixel.Or("SessionList Width", o.Width, 240))).Gap(k.D(4)).Role(ui.RoleList).Label(text).Children(func() {
for i, s := range sessions {
ui.Row(c).Key(s.Device).FillWidth().Gap(k.D(4)).AlignItems(ui.Center).Role(ui.RoleListItem).Children(func() {
if s.Icon != nil {
ui.Box(c).Size(k.D(s.Icon.W), k.D(s.Icon.H)).Draw(func(p *ui.Painter, r ui.Rect) {
k.Canvas(p, r).Sprite(0, 0, s.Icon, 0, pixel.Ink, 1)
})
}
ui.Column(c).Grow(1).Children(func() {
pixel.Text(c, s.Device, pixel.TextOptions{MaxLines: 1})
// This device is here now; others say when.
if s.Current {
pixel.Text(c, k.Msg("this_device"), pixel.TextOptions{Slot: pixel.Good})
} else {
datetime.RelativeTime(c, s.Seen, datetime.RelativeTimeOptions{})
}
})
if input.Button(c, k.Msg("sign_out"), input.ButtonOptions{Size: input.Small, Variant: input.Danger, Disabled: o.Disabled, Label: k.Msg("sign_out") + " " + s.Device}).Clicked {
res.SignedOut = i
}
})
}
})
if res.SignedOut >= 0 {
slog.Debug("8bit: session list sign out", "label", text, "session", res.SignedOut)
}
return res
}