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>
117 lines
3.1 KiB
Go
117 lines
3.1 KiB
Go
package chat
|
|
|
|
import (
|
|
"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/theme"
|
|
)
|
|
|
|
// MessageRole is who a chat message comes from.
|
|
type MessageRole int
|
|
|
|
const (
|
|
MessageUser MessageRole = iota
|
|
MessageAssistant
|
|
MessageSystem
|
|
MessageError
|
|
)
|
|
|
|
func (r MessageRole) key() string {
|
|
switch r {
|
|
case MessageUser:
|
|
return "message_bubble.user"
|
|
case MessageAssistant:
|
|
return "message_bubble.assistant"
|
|
case MessageSystem:
|
|
return "message_bubble.system"
|
|
case MessageError:
|
|
return "message_bubble.error"
|
|
}
|
|
panic("mujica: unknown MessageRole")
|
|
}
|
|
|
|
// MessageBubbleOptions configure a MessageBubble.
|
|
type MessageBubbleOptions struct {
|
|
// Name shows above the bubble; empty shows none.
|
|
Name string
|
|
// Avatar builds the picture beside the bubble.
|
|
Avatar func()
|
|
// MaxWidth caps the bubble in DIPs; zero means 560.
|
|
MaxWidth float32
|
|
}
|
|
|
|
// MessageBubble frames one chat message by role: users right, others left, system centered.
|
|
func MessageBubble(c *ui.Context, role MessageRole, opts MessageBubbleOptions, content func()) ui.Element {
|
|
if opts.MaxWidth < 0 {
|
|
panic("mujica: MessageBubble MaxWidth must not be negative")
|
|
}
|
|
if content == nil {
|
|
panic("mujica: MessageBubble needs content")
|
|
}
|
|
maxW := opts.MaxWidth
|
|
if maxW == 0 {
|
|
maxW = 560
|
|
}
|
|
k := core.Tokens(c)
|
|
u := core.Density(c).Unit()
|
|
label := core.Msg(c, role.key())
|
|
if opts.Name != "" {
|
|
label += ", " + opts.Name
|
|
}
|
|
row := ui.Row(c).Gap(u * 2).AlignItems(ui.Start).Role(ui.RoleGroup).Label(label)
|
|
switch role {
|
|
case MessageUser:
|
|
row.Justify(ui.End)
|
|
case MessageSystem:
|
|
row.Justify(ui.Center)
|
|
}
|
|
row.Children(func() {
|
|
if role == MessageSystem {
|
|
ui.Row(c).MaxWidth(maxW).Padding(u, u*3).Gap(u * 2).Radius(theme.ControlRadius).
|
|
Background(k.SurfaceHover).TextColor(k.TextMuted).FontSize(core.FontSize(c, theme.CaptionSize)).
|
|
Children(content)
|
|
return
|
|
}
|
|
if opts.Avatar != nil && role != MessageUser {
|
|
opts.Avatar()
|
|
}
|
|
ui.Column(c).Gap(u).MaxWidth(maxW).Shrink(1).MinWidth(0).AlignItems(messageBubbleAlign(role)).Children(func() {
|
|
if opts.Name != "" {
|
|
ui.Text(c, opts.Name).FontSize(core.FontSize(c, theme.CaptionSize)).TextColor(k.TextMuted).SingleLine()
|
|
}
|
|
b := ui.Column(c).Padding(u*2, u*3).Gap(u * 2).Radius(theme.CardRadius).MinWidth(0)
|
|
switch role {
|
|
case MessageUser:
|
|
b.Background(k.Selection)
|
|
case MessageError:
|
|
b.Background(k.Surface).Border(theme.BorderWidth, k.Danger)
|
|
default:
|
|
b.Background(k.Surface).Border(theme.BorderWidth, k.Border)
|
|
}
|
|
b.Children(func() {
|
|
if role != MessageError {
|
|
content()
|
|
return
|
|
}
|
|
ui.Row(c).Gap(u * 2).AlignItems(ui.Start).Children(func() {
|
|
ui.Icon(c, icons.Must("circle-alert")).Size(16, 16).Shrink(0).TextColor(k.Danger)
|
|
ui.Column(c).Gap(u * 2).Grow(1).MinWidth(0).Children(content)
|
|
})
|
|
})
|
|
})
|
|
if opts.Avatar != nil && role == MessageUser {
|
|
opts.Avatar()
|
|
}
|
|
})
|
|
return row
|
|
}
|
|
|
|
func messageBubbleAlign(r MessageRole) ui.Align {
|
|
if r == MessageUser {
|
|
return ui.End
|
|
}
|
|
return ui.Start
|
|
}
|