Files
MujicaUI/chat/feedback_form.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

111 lines
3.8 KiB
Go

package chat
import (
"log/slog"
"slices"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// FeedbackRating is the thumb given; zero is FeedbackUp.
type FeedbackRating int
const (
FeedbackUp FeedbackRating = iota
FeedbackDown
)
// FeedbackValue is the chosen reasons and a comment.
type FeedbackValue struct {
Reasons []string
Comment string
}
// FeedbackFormOptions configure a FeedbackForm.
type FeedbackFormOptions struct {
Rating FeedbackRating
// Reasons replaces the built-in reasons for the rating.
Reasons []string
// Submitting shows the caller sending; it blocks the form.
Submitting bool
Disabled bool
}
// FeedbackFormView is a built FeedbackForm and this frame's outcome.
type FeedbackFormView struct {
Element ui.Element
Submitted, Cancelled bool
}
var feedbackReasons = [2][]string{
{"feedback_form.up.accurate", "feedback_form.up.helpful", "feedback_form.up.clear"},
{"feedback_form.down.wrong", "feedback_form.down.unhelpful", "feedback_form.down.unsafe", "feedback_form.down.style"},
}
// FeedbackForm asks for reasons and a comment after a rating.
func FeedbackForm(c *ui.Context, value *FeedbackValue, opts FeedbackFormOptions) FeedbackFormView {
if value == nil {
panic("mujica: FeedbackForm needs a value")
}
if opts.Rating != FeedbackUp && opts.Rating != FeedbackDown {
panic("mujica: FeedbackForm Rating out of range")
}
reasons := opts.Reasons
if reasons == nil {
for _, key := range feedbackReasons[opts.Rating] {
reasons = append(reasons, core.Msg(c, key))
}
}
k := core.Tokens(c)
d := core.Density(c)
blocked := opts.Disabled || opts.Submitting
col := ui.Column(c).Gap(d.Unit()*3).Padding(d.Unit()*4).Background(k.Surface).Border(theme.BorderWidth, k.Border).
Radius(theme.CardRadius).Role(ui.RoleGroup).Disabled(blocked)
v := FeedbackFormView{Element: col}
title := map[FeedbackRating]string{FeedbackUp: "feedback_form.up.title", FeedbackDown: "feedback_form.down.title"}[opts.Rating]
col.Label(core.Msg(c, title))
col.Children(func() {
ui.Row(c).Gap(d.Unit() * 2).AlignItems(ui.Center).Children(func() {
ui.Text(c, core.Msg(c, title)).Font(theme.SerifFont).FontSize(core.FontSize(c, theme.H3Size)).TextColor(k.Text)
})
ui.Row(c).Wrap().Gap(d.Unit() * 2).FocusGroup(ui.Horizontal).Children(func() {
for _, r := range reasons {
on := slices.Contains(value.Reasons, r)
was := on
input.Toggle(c, &on, r, input.ToggleOptions{Disabled: blocked})
if on != was && !col.IsDisabled() {
if on {
value.Reasons = append(slices.Clone(value.Reasons), r)
} else {
value.Reasons = slices.DeleteFunc(slices.Clone(value.Reasons), func(x string) bool { return x == r })
}
}
}
})
input.TextArea(c, &value.Comment, input.TextAreaOptions{Placeholder: core.Msg(c, "feedback_form.comment"), Label: core.Msg(c, "feedback_form.comment"),
MinHeight: 72, Disabled: blocked})
ui.Row(c).Gap(d.Unit() * 2).Justify(ui.End).Children(func() {
v.Cancelled = input.Button(c, core.Msg(c, "feedback_form.cancel"), input.ButtonOptions{Variant: input.Ghost, Disabled: blocked}).Clicked()
v.Submitted = input.Button(c, core.Msg(c, "feedback_form.submit"), input.ButtonOptions{Icon: icons.Must("check"), Loading: opts.Submitting, Disabled: opts.Disabled}).Clicked()
})
})
if !col.IsDisabled() && col.Shortcut(0, ui.KeyEscape) {
v.Cancelled = true
}
if col.IsDisabled() {
v.Submitted, v.Cancelled = false, false
}
switch {
case v.Submitted:
slog.Debug("mujica: feedback_form submit", "rating", opts.Rating, "reasons", len(value.Reasons))
case v.Cancelled:
slog.Debug("mujica: feedback_form cancel")
}
return v
}