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>
85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
package overlay
|
|
|
|
import (
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
|
|
)
|
|
|
|
// TooltipOptions configure a Tooltip.
|
|
type TooltipOptions struct {
|
|
// Delay is how long hover or focus rests before the tip shows: 500 ms when zero.
|
|
Delay time.Duration
|
|
}
|
|
|
|
// TooltipState tracks when the anchor became hot and whether Escape hid the tip.
|
|
type TooltipState struct {
|
|
Since time.Time
|
|
Dismissed bool
|
|
}
|
|
|
|
const tooltipMaxWidth = 240
|
|
|
|
// Tooltip shows a non-interactive tip by anchor after hover or keyboard focus rests on it for Delay.
|
|
func Tooltip(c *ui.Context, anchor ui.Element, text string, opts TooltipOptions) ui.Element {
|
|
if !anchor.Valid() {
|
|
panic("mujica: Tooltip needs an anchor")
|
|
}
|
|
if opts.Delay < 0 {
|
|
panic("mujica: Tooltip Delay is negative")
|
|
}
|
|
if text == "" {
|
|
return anchor
|
|
}
|
|
anchor.Description(text)
|
|
st := ui.Local(anchor, "mujica.tooltip", func() TooltipState { return TooltipState{} })
|
|
hot := anchor.Hovered() || anchor.FocusVisible()
|
|
if !hot {
|
|
st.Since, st.Dismissed = time.Time{}, false
|
|
return anchor
|
|
}
|
|
if st.Dismissed {
|
|
return anchor
|
|
}
|
|
if st.Since.IsZero() {
|
|
st.Since = c.Now()
|
|
}
|
|
delay := opts.Delay
|
|
if delay == 0 {
|
|
delay = 500 * time.Millisecond
|
|
}
|
|
if wait := delay - c.Now().Sub(st.Since); wait > 0 {
|
|
c.After(wait)
|
|
return anchor
|
|
}
|
|
if core.OverlayEscape(c, st) {
|
|
slog.Debug("mujica: tooltip dismiss")
|
|
st.Dismissed = true
|
|
return anchor
|
|
}
|
|
tooltipShow(c, anchor.Bounds(), text)
|
|
return anchor
|
|
}
|
|
|
|
// tooltipShow draws the tip below a, or above it when there is no room.
|
|
func tooltipShow(c *ui.Context, a ui.Rect, text string) {
|
|
k := core.Tokens(c)
|
|
u := core.Density(c).Unit()
|
|
size := core.FontSize(c, theme.CaptionSize+1)
|
|
tw, th := c.MeasureText(tooltipMaxWidth, ui.Span{Text: text, Size: size, Font: c.Theme().Font})
|
|
padX, padY := u*2.5, u*1.5
|
|
w, h := tw+2*(padX+theme.BorderWidth)+1, th+2*(padY+theme.BorderWidth)
|
|
x, y := core.PlaceOverlay(c, ui.Rect{X: a.X + a.W/2 - w/2, Y: a.Y, W: w, H: a.H}, w, h, 6)
|
|
ui.Overlay(c, func() {
|
|
ui.Box(c).Absolute().Left(x).Top(y).Width(w).Padding(padY, padX).Radius(theme.ControlRadius).
|
|
Background(k.Text).TextColor(k.Background).Border(theme.BorderWidth, k.Text).
|
|
PassThrough().Role(ui.RoleTooltip).Children(func() {
|
|
ui.Text(c, text).FontSize(size)
|
|
})
|
|
})
|
|
}
|