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>
88 lines
3.0 KiB
Go
88 lines
3.0 KiB
Go
package files
|
|
|
|
import (
|
|
"fmt"
|
|
"hash/fnv"
|
|
"strings"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/code"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
|
|
)
|
|
|
|
// RemoteCaret describes a collaborator's caret and selection offsets.
|
|
type RemoteCaret struct {
|
|
ID, Name string
|
|
// Offset and Anchor bound the selection, equal for a caret.
|
|
Offset, Anchor int
|
|
}
|
|
|
|
// RemoteCursorOptions configure RemoteCursor.
|
|
type RemoteCursorOptions struct {
|
|
// Caret maps text offsets into overlay-relative rectangles; required.
|
|
Caret func(offset int) ui.Rect
|
|
// Selection maps offset ranges into overlay-relative rectangles; optional.
|
|
Selection func(from, to int) []ui.Rect
|
|
}
|
|
|
|
// RemoteCursor overlays collaborator carets, selections and names; input passes through.
|
|
// Place it filling the editor's box, after the editor.
|
|
func RemoteCursor(c *ui.Context, carets []RemoteCaret, opts RemoteCursorOptions) ui.Element {
|
|
if opts.Caret == nil {
|
|
panic("mujica: RemoteCursor needs Caret")
|
|
}
|
|
k := core.Tokens(c)
|
|
names := make([]string, len(carets))
|
|
for i, rc := range carets {
|
|
if rc.ID == "" || rc.Offset < 0 || rc.Anchor < 0 {
|
|
panic("mujica: RemoteCaret needs an ID and offsets not negative")
|
|
}
|
|
names[i] = rc.Name
|
|
}
|
|
size := core.FontSize(c, theme.CaptionSize)
|
|
e := ui.Box(c).Absolute().Top(0).Left(0).Right(0).Bottom(0).PassThrough().Clip().Role(ui.RoleNone)
|
|
if len(carets) > 0 {
|
|
e.Role(ui.RoleStatus).Label(fmt.Sprintf(core.Msg(c, "remote_cursor.label"), strings.Join(names, ", ")))
|
|
}
|
|
return e.Draw(func(p *ui.Painter, r ui.Rect) {
|
|
for _, rc := range carets {
|
|
col := remoteColor(k, rc.ID)
|
|
if opts.Selection != nil && rc.Anchor != rc.Offset {
|
|
for _, b := range opts.Selection(min(rc.Anchor, rc.Offset), max(rc.Anchor, rc.Offset)) {
|
|
p.Fill(remoteRect(p, ui.Rect{X: r.X + b.X, Y: r.Y + b.Y, W: b.W, H: b.H}), col.Alpha(0.22), 2)
|
|
}
|
|
}
|
|
cb := opts.Caret(rc.Offset)
|
|
x, y := internal.Snap(p, r.X+cb.X), internal.Snap(p, r.Y+cb.Y)
|
|
p.Fill(ui.Rect{X: x - 1, Y: y, W: 2, H: cb.H}, col, 0)
|
|
// Keep the name flag inside the overlay.
|
|
tw, th := p.MeasureText(0, ui.Span{Text: rc.Name, Size: size})
|
|
flag := ui.Rect{X: x - 1, Y: y - th - 2, W: min(tw+8, r.W), H: th + 2}
|
|
if flag.Y < r.Y {
|
|
flag.Y = y + cb.H
|
|
}
|
|
flag.X = max(r.X, min(flag.X, r.X+r.W-flag.W))
|
|
flag.Y = max(r.Y, min(flag.Y, r.Y+r.H-flag.H))
|
|
flag = remoteRect(p, flag)
|
|
p.Fill(flag, col, 2)
|
|
p.Clip(flag, 2, func() { p.Text(flag.X+4, flag.Y+1, rc.Name, size, k.Surface) })
|
|
}
|
|
})
|
|
}
|
|
|
|
func remoteRect(p *ui.Painter, r ui.Rect) ui.Rect {
|
|
x, y := internal.Snap(p, r.X), internal.Snap(p, r.Y)
|
|
return ui.Rect{X: x, Y: y, W: internal.Snap(p, r.X+r.W) - x, H: internal.Snap(p, r.Y+r.H) - y}
|
|
}
|
|
|
|
// remoteColor assigns a palette color by collaborator ID.
|
|
func remoteColor(k theme.Tokens, id string) ui.Color {
|
|
h := fnv.New32a()
|
|
h.Write([]byte(id))
|
|
pal := code.FlamegraphPalette(k)
|
|
return pal[h.Sum32()%uint32(len(pal))]
|
|
}
|