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

128 lines
3.9 KiB
Go

package chat
import (
"fmt"
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// MarkdownResult is a MarkdownView's element and the link the user opened this frame.
type MarkdownResult struct {
Element ui.Element
link string
}
// Link returns the URL of the link clicked or activated this frame.
func (r MarkdownResult) Link() (string, bool) { return r.link, r.link != "" }
// MarkdownView renders a Markdown subset; a growing src parses only its last block again.
func MarkdownView(c *ui.Context, src string) MarkdownResult {
u := core.Density(c).Unit()
var r MarkdownResult
r.Element = ui.Column(c).Gap(u * 3).MinWidth(0)
doc := ui.Local(r.Element, "doc", func() *internal.Markdown { return &internal.Markdown{} })
if from := (*doc).Update(src); from < len((*doc).Blocks) {
slog.Debug("mujica: markdown_view parse", "from", from, "blocks", len((*doc).Blocks))
}
r.Element.Children(func() {
for i := range (*doc).Blocks {
b := &(*doc).Blocks[i]
ui.Column(c.Key(i)).MinWidth(0).Children(func() {
if url := markdownBlock(c, b); url != "" {
r.link = url
}
})
}
})
if r.link != "" {
slog.Debug("mujica: markdown_view link")
}
return r
}
// markdownBlock builds one block and returns a link opened in it.
func markdownBlock(c *ui.Context, b *internal.MdBlock) string {
k := core.Tokens(c)
u := core.Density(c).Unit()
switch b.Kind {
case internal.BlockHeading:
size := []float32{theme.H1Size, theme.H2Size, theme.H3Size}[min(b.Level, 3)-1]
t := ui.RichText(c).Font(theme.SerifFont).FontSize(core.FontSize(c, size)).FontWeight(600).Role(ui.RoleText)
return markdownSpans(c, t, b.Spans)
case internal.BlockParagraph:
return markdownSpans(c, ui.RichText(c), b.Spans)
case internal.BlockQuote:
var link string
ui.Row(c).Padding(0, 0, 0, u*3).BorderWidth(0, 0, 0, 3).BorderColor(k.Border).Children(func() {
link = markdownSpans(c, ui.RichText(c).TextColor(k.TextMuted).Grow(1), b.Spans)
})
return link
case internal.BlockList:
var link string
ui.Column(c).Gap(u).Role(ui.RoleList).Children(func() {
for _, it := range b.Items {
ui.Row(c).Gap(u*2).AlignItems(ui.Start).Padding(0, 0, 0, float32(it.Depth)*u*5).Role(ui.RoleListItem).Children(func() {
mark := "•"
if it.Number > 0 {
mark = fmt.Sprintf("%d.", it.Number)
}
ui.Text(c, mark).TextColor(k.TextMuted).FontFeatures("tnum").MinWidth(u * 4).Shrink(0)
if l := markdownSpans(c, ui.RichText(c).Grow(1).MinWidth(0), it.Spans); l != "" {
link = l
}
})
}
})
return link
case internal.BlockCode:
CodeBlock(c, b.Code, CodeBlockOptions{Language: b.Lang})
case internal.BlockTable:
align := make([]ui.Align, len(b.Align))
for i, a := range b.Align {
align[i] = []ui.Align{ui.Start, ui.Center, ui.End}[a]
}
TableBlock(c, b.Header, b.Rows, TableBlockOptions{Align: align})
case internal.BlockRule:
ui.Box(c).Height(u * 4).Draw(func(p *ui.Painter, r ui.Rect) {
internal.RuleDiamond(p, r, u*2, 5, k.Ornament)
})
}
return ""
}
// markdownSpans fills paragraph t with styled spans; links are focusable and return their URL when used.
func markdownSpans(c *ui.Context, t ui.Element, spans []internal.MdSpan) string {
k := core.Tokens(c)
var link string
t.MinWidth(0).Children(func() {
for _, sp := range spans {
e := ui.Text(c, sp.Text)
if sp.Strong {
e.FontWeight(700)
}
if sp.Em {
e.Italic()
}
if sp.Strike {
e.Strikethrough()
}
if sp.Code {
e.Font(theme.MonoFont).TextBackground(k.SurfaceHover)
}
if sp.URL == "" {
continue
}
e.TextColor(k.AccentText).Underline().Focusable().Role(ui.RoleLink).Cursor(ui.CursorPointer).Tooltip(sp.URL)
if e.Clicked() && !e.IsDisabled() {
link = sp.URL
}
}
})
return link
}