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>
113 lines
3.3 KiB
Go
113 lines
3.3 KiB
Go
package chat
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
|
|
)
|
|
|
|
// MessageAction is one action on a chat message.
|
|
type MessageAction int
|
|
|
|
const (
|
|
ActionCopy MessageAction = iota + 1
|
|
ActionRegenerate
|
|
ActionEdit
|
|
ActionLike
|
|
ActionDislike
|
|
ActionSpeak
|
|
ActionShare
|
|
)
|
|
|
|
// MessageFeedback is the user's rating of an assistant message.
|
|
type MessageFeedback int
|
|
|
|
const (
|
|
FeedbackNone MessageFeedback = iota
|
|
FeedbackLiked
|
|
FeedbackDisliked
|
|
)
|
|
|
|
// MessageActionsOptions configure MessageActions.
|
|
type MessageActionsOptions struct {
|
|
// Feedback holds the rating; nil hides like and dislike.
|
|
Feedback *MessageFeedback
|
|
}
|
|
|
|
// MessageActionsResult is the toolbar's element and the action chosen this frame.
|
|
type MessageActionsResult struct {
|
|
Element ui.Element
|
|
action MessageAction
|
|
}
|
|
|
|
// Action returns the action the user chose this frame.
|
|
func (r MessageActionsResult) Action() (MessageAction, bool) { return r.action, r.action != 0 }
|
|
|
|
// messageActionIcons names each action's icon and copy key.
|
|
var messageActionIcons = map[MessageAction][2]string{
|
|
ActionCopy: {"copy", "message_actions.copy"},
|
|
ActionRegenerate: {"refresh-cw", "message_actions.regenerate"},
|
|
ActionEdit: {"pencil", "message_actions.edit"},
|
|
ActionLike: {"thumbs-up", "message_actions.like"},
|
|
ActionDislike: {"thumbs-down", "message_actions.dislike"},
|
|
ActionSpeak: {"volume-2", "message_actions.speak"},
|
|
ActionShare: {"share-2", "message_actions.share"},
|
|
}
|
|
|
|
// messageActionsFor lists the actions a role's messages offer.
|
|
func messageActionsFor(role MessageRole, rated bool) []MessageAction {
|
|
switch role {
|
|
case MessageUser:
|
|
return []MessageAction{ActionCopy, ActionEdit}
|
|
case MessageAssistant:
|
|
if rated {
|
|
return []MessageAction{ActionCopy, ActionRegenerate, ActionLike, ActionDislike, ActionSpeak, ActionShare}
|
|
}
|
|
return []MessageAction{ActionCopy, ActionRegenerate, ActionSpeak, ActionShare}
|
|
case MessageSystem:
|
|
return []MessageAction{ActionCopy}
|
|
case MessageError:
|
|
return []MessageAction{ActionCopy, ActionRegenerate}
|
|
}
|
|
panic("mujica: unknown MessageRole")
|
|
}
|
|
|
|
// MessageActions is a toolbar of the actions a role's message offers; arrows move between them.
|
|
func MessageActions(c *ui.Context, role MessageRole, opts MessageActionsOptions) MessageActionsResult {
|
|
var r MessageActionsResult
|
|
r.Element = ui.Row(c).Gap(core.Density(c).Unit()).AlignItems(ui.Center).FocusGroup(ui.Horizontal).
|
|
Role(ui.RoleToolbar).Label(core.Msg(c, "message_actions.label"))
|
|
disabled := r.Element.IsDisabled()
|
|
r.Element.Children(func() {
|
|
for _, a := range messageActionsFor(role, opts.Feedback != nil) {
|
|
ic := messageActionIcons[a]
|
|
hit := false
|
|
if a == ActionLike || a == ActionDislike {
|
|
want := FeedbackLiked
|
|
if a == ActionDislike {
|
|
want = FeedbackDisliked
|
|
}
|
|
on := *opts.Feedback == want
|
|
if codeBlockToggle(c, ic[0], core.Msg(c, ic[1]), &on) && !disabled {
|
|
hit = true
|
|
if on {
|
|
*opts.Feedback = want
|
|
} else {
|
|
*opts.Feedback = FeedbackNone
|
|
}
|
|
}
|
|
} else {
|
|
hit = core.IconAction(c, icons.Must(ic[0]), core.Msg(c, ic[1])).Tooltip(core.Msg(c, ic[1])).Clicked() && !disabled
|
|
}
|
|
if hit {
|
|
r.action = a
|
|
slog.Debug("mujica: message_actions action", "action", a)
|
|
}
|
|
}
|
|
})
|
|
return r
|
|
}
|