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>
75 lines
2.5 KiB
Go
75 lines
2.5 KiB
Go
package chat
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"math"
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
// ErrorMessageOptions configure an ErrorMessage.
|
|
type ErrorMessageOptions struct {
|
|
// Title sums the failure up; empty uses the built-in "Something went wrong".
|
|
Title string
|
|
// RetryAt is when a rate limit lifts; until then retry waits with a countdown.
|
|
RetryAt time.Time
|
|
}
|
|
|
|
// ErrorMessageResult is the error's element and a retry the user asked for.
|
|
type ErrorMessageResult struct {
|
|
Element ui.Element
|
|
retried bool
|
|
}
|
|
|
|
// Retried reports a press of retry.
|
|
func (r ErrorMessageResult) Retried() bool { return r.retried }
|
|
|
|
// ErrorMessage shows a failed reply with the original error text, selectable, and retry.
|
|
func ErrorMessage(c *ui.Context, text string, opts ErrorMessageOptions) ErrorMessageResult {
|
|
if text == "" {
|
|
panic("mujica: ErrorMessage needs the error text")
|
|
}
|
|
k := core.Tokens(c)
|
|
u := core.Density(c).Unit()
|
|
title := opts.Title
|
|
if title == "" {
|
|
title = core.Msg(c, "error_message.title")
|
|
}
|
|
wait := opts.RetryAt.Sub(c.Now())
|
|
var r ErrorMessageResult
|
|
r.Element = ui.Column(c).Gap(u*2).Padding(u*3, u*4).MaxWidth(560).Radius(theme.CardRadius).
|
|
Background(k.Surface).Border(theme.BorderWidth, k.Danger).Role(ui.RoleGroup).Label(title)
|
|
disabled := r.Element.IsDisabled()
|
|
r.Element.Children(func() {
|
|
ui.Row(c).Gap(u * 2).AlignItems(ui.Center).Children(func() {
|
|
ui.Icon(c, icons.Must("circle-alert")).Size(16, 16).Shrink(0).TextColor(k.Danger)
|
|
ui.Text(c, title).FontWeight(600).TextColor(k.Danger)
|
|
})
|
|
ui.Text(c, text).Selectable().Font(theme.MonoFont).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.Text)
|
|
ui.Row(c).Gap(u * 3).AlignItems(ui.Center).Children(func() {
|
|
label := core.Msg(c, "error_message.retry")
|
|
if wait > 0 {
|
|
secs := int(math.Ceil(wait.Seconds()))
|
|
label = fmt.Sprintf(core.Msg(c, "error_message.wait"), secs)
|
|
// Wake when the shown second changes.
|
|
c.After(wait - time.Duration(secs-1)*time.Second)
|
|
}
|
|
if input.Button(c, label, input.ButtonOptions{Variant: input.Secondary, Icon: icons.Must("refresh-cw"), Disabled: wait > 0}).Clicked() && !disabled {
|
|
r.retried = true
|
|
slog.Debug("mujica: error_message retry")
|
|
}
|
|
if wait > 0 {
|
|
ui.Text(c, core.Msg(c, "error_message.limited")).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted)
|
|
}
|
|
})
|
|
})
|
|
return r
|
|
}
|