Files
MujicaUI/account/session_list.go
T
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

129 lines
4.3 KiB
Go

package account
import (
"fmt"
"log/slog"
"slices"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/display"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/overlay"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// Session describes a device without carrying its authentication token.
type Session struct {
ID, Device, Location string
LastActive time.Time
// Current marks the session this window runs in.
Current bool
}
// SessionListOptions configure a SessionList.
type SessionListOptions struct {
Disabled bool
}
// SessionListResult reports individual and bulk sign-out requests.
type SessionListResult struct {
Element ui.Element
revoke string
others bool
}
// Revoked returns the ID of a session to sign out.
func (r SessionListResult) Revoked() (string, bool) { return r.revoke, r.revoke != "" }
// OthersRevoked reports confirmed bulk sign-out.
func (r SessionListResult) OthersRevoked() bool { return r.others }
type sessionListState struct {
confirm bool
source []Session
others int
ready bool
}
// SessionList displays devices and requests sign-out through events.
func SessionList(c *ui.Context, sessions []Session, opts SessionListOptions) SessionListResult {
k := core.Tokens(c)
d := core.Density(c)
var r SessionListResult
r.Element = ui.Column(c).Gap(d.Unit() * 3).MinWidth(420).Disabled(opts.Disabled)
st := ui.Local(r.Element, "sessions", func() sessionListState { return sessionListState{} })
disabled := r.Element.IsDisabled()
if !st.ready || !slices.Equal(st.source, sessions) {
st.source, st.others, st.ready = slices.Clone(sessions), 0, true
ids := map[string]bool{}
for _, s := range sessions {
if s.ID == "" || s.Device == "" || ids[s.ID] {
panic("mujica: Session needs a unique ID and a Device")
}
ids[s.ID] = true
if !s.Current {
st.others++
}
}
}
others := st.others
r.Element.Children(func() {
ui.Column(c).Role(ui.RoleList).Label(core.Msg(c, "session_list.label")).Children(func() {
accountRows(c, len(sessions), func(i int) any { return sessions[i].ID }, func(i int) {
s := sessions[i]
ui.Row(c.Key(s.ID)).MinHeight(d.ControlHeight()+d.Unit()*4).Gap(d.Unit()*3).AlignItems(ui.Center).
BorderWidth(0, 0, theme.BorderWidth, 0).BorderColor(k.Border).Role(ui.RoleListItem).Children(func() {
ui.Column(c).Grow(1).MinWidth(0).Gap(2).Children(func() {
ui.Row(c).Gap(d.Unit() * 2).AlignItems(ui.Center).Children(func() {
ui.Text(c, s.Device).FontWeight(600).SingleLine()
if s.Current {
display.Badge(c, display.BadgeOptions{Text: core.Msg(c, "session_list.current")}, nil)
}
})
ui.Text(c, sessionListDetail(c, s)).TextColor(k.TextMuted).FontSize(core.FontSize(c, theme.CaptionSize)).SingleLine()
})
if !s.Current {
b := input.Button(c, core.Msg(c, "session_list.sign_out"), input.ButtonOptions{Variant: input.Secondary, Disabled: disabled}).
Label(fmt.Sprintf(core.Msg(c, "session_list.sign_out_of"), s.Device))
if b.Clicked() && !disabled {
r.revoke = s.ID
}
}
})
})
})
ui.Row(c).Children(func() {
if input.Button(c, core.Msg(c, "session_list.sign_out_others"), input.ButtonOptions{Variant: input.Danger, Disabled: disabled || others == 0}).Clicked() && !disabled {
st.confirm = true
}
})
})
if disabled || others == 0 {
st.confirm = false
}
res := overlay.AlertDialog(c, &st.confirm, overlay.AlertDialogOptions{Title: core.Msg(c, "session_list.confirm_title"),
Description: fmt.Sprintf(core.Msg(c, "session_list.confirm_text"), others), ConfirmLabel: core.Msg(c, "session_list.sign_out_others")})
r.others = res.Confirmed && !disabled
if r.revoke != "" || r.others {
slog.Debug("mujica: session_list sign out", "others", r.others)
}
return r
}
// sessionListDetail formats the location and last active time.
func sessionListDetail(c *ui.Context, s Session) string {
when := core.Msg(c, "session_list.now")
if ago := c.Now().Sub(s.LastActive); s.LastActive.IsZero() {
when = core.Msg(c, "session_list.unknown")
} else if ago >= time.Minute {
when = s.LastActive.Format("2006-01-02 15:04")
}
if s.Location == "" {
return when
}
return s.Location + " · " + when
}