Files
MujicaUI/git/review_comment.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

131 lines
4.8 KiB
Go

package git
import (
"fmt"
"log/slog"
"strings"
"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/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// ReviewMessage is one comment of a review thread.
type ReviewMessage struct {
Author string
When time.Time
Body string
}
// ReviewThread is a line-level review discussion.
type ReviewThread struct {
File string
Line int
Resolved bool
Messages []ReviewMessage
}
// ReviewCommentOptions configure a ReviewComment.
type ReviewCommentOptions struct {
// TimeFormat lays out message times; empty means "2006-01-02 15:04".
TimeFormat string
Disabled bool
}
// ReviewCommentResult is a built ReviewComment and its events.
type ReviewCommentResult struct {
Element ui.Element
reply string
resolved, reopened bool
}
// Replied returns the reply sent this frame.
func (r ReviewCommentResult) Replied() (string, bool) { return r.reply, r.reply != "" }
// Resolved reports the Resolve button this frame.
func (r ReviewCommentResult) Resolved() bool { return r.resolved }
// Reopened reports the Reopen button this frame.
func (r ReviewCommentResult) Reopened() bool { return r.reopened }
// ReviewComment shows a line's review thread; *draft is the reply.
func ReviewComment(c *ui.Context, thread ReviewThread, draft *string, opts ReviewCommentOptions) ReviewCommentResult {
if thread.File == "" || thread.Line <= 0 || len(thread.Messages) == 0 {
panic("mujica: ReviewThread needs a File, a positive Line and a message")
}
k := core.Tokens(c)
u := core.Density(c).Unit()
small := core.FontSize(c, theme.CaptionSize)
layout := gitTimeLayout(opts.TimeFormat)
card := ui.Column(c).AlignItems(ui.Stretch).Background(k.Surface).Border(theme.BorderWidth, k.Border).
Radius(theme.CardRadius).Disabled(opts.Disabled).Clip()
res := ReviewCommentResult{Element: card}
card.Children(func() {
ui.Row(c).AlignItems(ui.Center).Gap(u*2).Padding(u*2, u*3).Background(k.Background).FontSize(small).Children(func() {
ui.Icon(c, icons.Must("message-square")).Size(14, 14).TextColor(k.TextMuted)
ui.Text(c, fmt.Sprintf("%s:%d", thread.File, thread.Line)).Font(theme.MonoFont).SingleLine().Grow(1).MinWidth(0)
if thread.Resolved {
ui.Row(c).AlignItems(ui.Center).Gap(u).TextColor(k.Success).Children(func() {
ui.Icon(c, icons.Must("circle-check")).Size(14, 14)
ui.Text(c, core.Msg(c, "review_comment.resolved")).FontWeight(600)
})
}
})
for i, m := range thread.Messages {
ui.Column(c).Gap(u).Padding(u*3, u*4).Children(func() {
if i > 0 {
ui.Box(c).Height(1).Background(k.Border).Margin(0, 0, u*2, 0).Role(ui.RoleNone)
}
ui.Row(c).AlignItems(ui.Center).Gap(u * 2).Children(func() {
display.Avatar(c, m.Author, display.AvatarOptions{Size: 20})
ui.Text(c, m.Author).FontWeight(600).SingleLine()
ui.Text(c, m.When.Format(layout)).FontSize(small).TextColor(k.TextMuted).FontFeatures("tnum")
})
ui.Text(c, m.Body)
})
}
ui.Box(c).Height(1).Background(k.Border).Role(ui.RoleNone)
ui.Column(c).Gap(u * 2).Padding(u * 3).AlignItems(ui.Stretch).Children(func() {
if !thread.Resolved {
in := input.TextArea(c, draft, input.TextAreaOptions{Placeholder: core.Msg(c, "review_comment.placeholder"), Label: core.Msg(c, "review_comment.field"),
MinHeight: 56, MaxHeight: 160, Disabled: opts.Disabled})
send := in.Submitted()
ui.Row(c).Gap(u * 2).Justify(ui.End).Children(func() {
if input.Button(c, core.Msg(c, "review_comment.resolve"), input.ButtonOptions{Variant: input.Secondary, Icon: icons.Must("check"), Disabled: opts.Disabled}).Clicked() {
res.resolved = true
}
if input.Button(c, core.Msg(c, "review_comment.reply"), input.ButtonOptions{Icon: icons.Must("reply"), Disabled: opts.Disabled || strings.TrimSpace(*draft) == ""}).Clicked() {
send = true
}
})
if send && !card.IsDisabled() && strings.TrimSpace(*draft) != "" {
res.reply, *draft = strings.TrimSpace(*draft), ""
}
return
}
ui.Row(c).Justify(ui.End).Children(func() {
if input.Button(c, core.Msg(c, "review_comment.reopen"), input.ButtonOptions{Variant: input.Secondary, Icon: icons.Must("rotate-ccw"), Disabled: opts.Disabled}).Clicked() {
res.reopened = true
}
})
})
})
if card.IsDisabled() {
res.resolved, res.reopened = false, false
}
switch {
case res.reply != "":
slog.Debug("mujica: review comment reply", "file", thread.File, "line", thread.Line)
case res.resolved:
slog.Debug("mujica: review comment resolve", "file", thread.File, "line", thread.Line)
case res.reopened:
slog.Debug("mujica: review comment reopen", "file", thread.File, "line", thread.Line)
}
return res
}