Files
MujicaUI/files/presence_avatars.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

152 lines
4.8 KiB
Go

package files
import (
"fmt"
"log/slog"
"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/internal"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/overlay"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// Collaborator is someone working on the same document.
type Collaborator struct {
ID, Name string
Image *ui.Bitmap
// Away marks someone present but idle.
Away bool
}
// PresenceAvatarsOptions configure PresenceAvatars.
type PresenceAvatarsOptions struct {
// Max avatars show before the "+N" button; zero is 4.
Max int
// Following identifies the followed collaborator, empty for none.
Following string
// Size is the avatar diameter; zero is 28.
Size float32
Disabled bool
}
// PresenceAvatarsResult is the avatars and the follow request this frame.
type PresenceAvatarsResult struct {
Element ui.Element
// Follow identifies a collaborator; Unfollow requests stopping.
Follow string
Unfollow bool
}
// PresenceAvatars displays collaborators, overflow count and follow actions.
func PresenceAvatars(c *ui.Context, people []Collaborator, opts PresenceAvatarsOptions) PresenceAvatarsResult {
if opts.Max < 0 || opts.Size < 0 {
panic("mujica: PresenceAvatars Max and Size must not be negative")
}
limit, size := opts.Max, opts.Size
if limit == 0 {
limit = 4
}
if size == 0 {
size = 28
}
u := core.Density(c).Unit()
var res PresenceAvatarsResult
res.Element = ui.Row(c).AlignItems(ui.Center).Gap(u).Role(ui.RoleGroup).Label(fmt.Sprintf(core.Msg(c, "presence_avatars.label"), len(people))).
FocusGroup(ui.Horizontal).Disabled(opts.Disabled)
disabled := res.Element.IsDisabled()
open := ui.Local(res.Element, "presence_avatars.open", func() bool { return false })
rows := ui.Local(res.Element, "presence_avatars.rows", func() ui.ListState { return ui.ListState{} })
*open = *open && len(people) > limit && !disabled
pick := func(p Collaborator) {
if disabled {
return
}
if p.ID == opts.Following {
res.Unfollow = true
} else {
res.Follow = p.ID
}
*open = false
slog.Debug("mujica: presence_avatars follow", "id", p.ID, "unfollow", res.Unfollow)
}
res.Element.Children(func() {
for i, p := range people {
if i >= limit {
break
}
if presenceAvatar(c, p, size, p.ID == opts.Following).Clicked() {
pick(p)
}
}
if extra := len(people) - limit; extra > 0 {
more := ui.ButtonBase(c).Size(max(size+4, theme.MinHitSize), max(size+4, theme.MinHitSize)).Radius((size + 4) / 2).Label(fmt.Sprintf(core.Msg(c, "presence_avatars.more"), extra))
presenceStyle(c, more, size, false)
more.Children(func() {
ui.Text(c, fmt.Sprintf("+%d", extra)).FontSize(core.FontSize(c, theme.CaptionSize)).FontWeight(600)
})
if more.Clicked() && !disabled {
*open = !*open
}
overlay.Popover(c, more, open, overlay.PopoverOptions{Label: core.Msg(c, "presence_avatars.others")}, func() {
rows.Key = func(i int) any { return people[limit+i].ID }
ui.List(c, rows, extra, func(i int) {
p := people[limit+i]
ui.Row(c.Key(p.ID)).Gap(u * 2).AlignItems(ui.Center).Children(func() {
if presenceAvatar(c, p, 24, p.ID == opts.Following).Clicked() {
pick(p)
}
ui.Text(c, p.Name).SingleLine()
})
}).Width(260).Height(min(240, float32(extra)*core.Density(c).ControlHeight()))
})
}
})
return res
}
// presenceAvatar builds a collaborator button with presence status.
func presenceAvatar(c *ui.Context, p Collaborator, size float32, following bool) ui.Element {
if p.ID == "" {
panic("mujica: Collaborator needs an ID")
}
k := core.Tokens(c)
key := "presence_avatars.follow"
if following {
key = "presence_avatars.unfollow"
}
b := ui.ButtonBase(c.Key(p.ID)).Size(max(size+4, theme.MinHitSize), max(size+4, theme.MinHitSize)).Radius((size + 4) / 2).Label(fmt.Sprintf(core.Msg(c, key), p.Name)).Tooltip(p.Name)
if p.Away {
b.Description(core.Msg(c, "presence_avatars.away"))
}
presenceStyle(c, b, size, following)
return b.Children(func() {
display.Avatar(c, p.Name, display.AvatarOptions{Image: p.Image, Size: size})
dot := ui.Box(c).Size(9, 9).Radius(4.5).Absolute().Right(0).Bottom(0).Border(1.5, k.Surface).Role(ui.RoleNone)
if p.Away {
dot.Background(k.Surface).Border(1.5, k.TextMuted)
} else {
dot.Background(k.Success)
}
})
}
// presenceStyle styles hover, focus and following rings.
func presenceStyle(c *ui.Context, b ui.Element, size float32, following bool) {
k := core.Tokens(c)
b.Shrink(0).Background(k.Surface).FocusRing(false)
if b.Hovered() {
b.Background(k.SurfaceHover)
}
b.DrawOver(func(p *ui.Painter, r ui.Rect) {
if following {
p.Stroke(r, k.AccentText, r.W/2, 2)
}
if b.FocusVisible() {
internal.FocusRing(p, r, r.W/2, k.Focus)
}
})
}