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>
110 lines
2.9 KiB
Go
110 lines
2.9 KiB
Go
package display
|
|
|
|
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"
|
|
)
|
|
|
|
// TagOptions configure a Tag; zero is a plain label.
|
|
type TagOptions struct {
|
|
// Icon names an icon shown before the label.
|
|
Icon string
|
|
// Clickable makes the label a button reported by TagView.Clicked.
|
|
Clickable bool
|
|
// Closable adds a remove button reported by TagView.Closed.
|
|
Closable bool
|
|
Disabled bool
|
|
}
|
|
|
|
// TagView is a shown Tag and this frame's events.
|
|
type TagView struct {
|
|
Element ui.Element
|
|
// Clicked reports the label was activated, not the remove button.
|
|
Clicked bool
|
|
// Closed reports the remove button, Delete or Backspace.
|
|
Closed bool
|
|
}
|
|
|
|
// Tag shows a label; click and remove are separate stops.
|
|
func Tag(c *ui.Context, label string, opts TagOptions) TagView {
|
|
if label == "" {
|
|
panic("mujica: Tag needs a label")
|
|
}
|
|
k := core.Tokens(c)
|
|
u := core.Density(c).Unit()
|
|
var v TagView
|
|
box := ui.Row(c).AlignItems(ui.Center).MinHeight(theme.MinHitSize).Radius(theme.ControlRadius).
|
|
Background(k.Surface).Border(theme.BorderWidth, k.Border).Shrink(0).Disabled(opts.Disabled)
|
|
v.Element = box
|
|
box.Children(func() {
|
|
var main ui.Element
|
|
if opts.Clickable {
|
|
main = ui.ButtonBase(c).Label(label)
|
|
internal.Ring(main, k.Focus, theme.ControlRadius)
|
|
} else {
|
|
main = ui.Row(c).AlignItems(ui.Center)
|
|
}
|
|
main.Gap(u*1.5).Padding(0, u*2).FillHeight().Radius(theme.ControlRadius).TextColor(k.Text)
|
|
if opts.Clickable && !opts.Disabled {
|
|
if main.Pressed() {
|
|
main.Background(k.Selection)
|
|
} else if main.Hovered() {
|
|
main.Background(k.SurfaceHover)
|
|
}
|
|
main.Transition(core.ColorFade)
|
|
}
|
|
if opts.Closable {
|
|
main.Padding(0, u, 0, u*2)
|
|
}
|
|
main.Children(func() {
|
|
if opts.Icon != "" {
|
|
Icon(c, opts.Icon, IconOptions{Size: 14})
|
|
}
|
|
ui.Text(c, label).FontSize(core.FontSize(c, theme.BodySize-1)).SingleLine()
|
|
})
|
|
if opts.Clickable && main.Clicked() {
|
|
v.Clicked = true
|
|
}
|
|
if opts.Closable {
|
|
if opts.Clickable && !main.IsDisabled() && (main.Shortcut(0, ui.KeyDelete) || main.Shortcut(0, ui.KeyBackspace)) {
|
|
v.Closed = true
|
|
}
|
|
if tagClose(c, k, label).Clicked() {
|
|
v.Closed = true
|
|
}
|
|
}
|
|
})
|
|
if v.Clicked {
|
|
slog.Debug("mujica: tag click", "label", label)
|
|
}
|
|
if v.Closed {
|
|
slog.Debug("mujica: tag close", "label", label)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func tagClose(c *ui.Context, k theme.Tokens, label string) ui.Element {
|
|
b := ui.ButtonBase(c).Size(theme.MinHitSize, theme.MinHitSize).
|
|
Radius(theme.ControlRadius).Label(fmt.Sprintf(core.Msg(c, "tag.remove"), label))
|
|
fg := k.TextMuted
|
|
if b.Pressed() {
|
|
b.Background(k.Selection)
|
|
fg = k.Text
|
|
} else if b.Hovered() {
|
|
b.Background(k.SurfaceHover)
|
|
fg = k.Text
|
|
}
|
|
internal.Ring(b, k.Focus, theme.ControlRadius)
|
|
b.Transition(core.ColorFade)
|
|
b.TextColor(fg).Children(func() {
|
|
Icon(c, "x", IconOptions{Size: 12})
|
|
})
|
|
return b
|
|
}
|