Files
MujicaUI/overlay/overlay.go
T
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

153 lines
3.7 KiB
Go

package overlay
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// OverlayPlace is where an Overlay sits in the window. The zero value is OverlayCenter.
type OverlayPlace int
const (
OverlayCenter OverlayPlace = iota
OverlayTop
OverlayBottom
OverlayLeft
OverlayRight
OverlayTopLeft
OverlayTopRight
OverlayBottomLeft
OverlayBottomRight
)
// OverlayOptions configure an Overlay.
type OverlayOptions struct {
Place OverlayPlace
// Inset keeps the panel from the window edges; 16 DIPs when zero.
Inset float32
// Modal traps the focus, makes the window inert and closes on a backdrop click.
Modal bool
// Dim darkens the window behind a modal overlay. It requires Modal.
Dim bool
// Label names the layer for assistive technology.
Label string
}
// Overlay shows content in a layer above the window while *open is true; Escape closes it.
func Overlay(c *ui.Context, open *bool, opts OverlayOptions, content func()) ui.Element {
if opts.Dim && !opts.Modal {
panic("mujica: Overlay Dim requires Modal")
}
if opts.Place < OverlayCenter || opts.Place > OverlayBottomRight {
panic("mujica: Overlay Place out of range")
}
site, focus := core.OverlaySite(c)
if !*open {
focus.Closed(c, site)
return ui.Element{}
}
k := core.Tokens(c)
inset := opts.Inset
if inset == 0 {
inset = 16
}
fx, fy := overlayFractions(opts.Place)
if opts.Modal {
core.OverlayLayer(c, open)
var backClicked bool
shown := true
panel := ui.DialogBase(c, &shown, func(back, panel ui.Element) {
backClicked = back.Clicked()
p := core.OverlayEnter(c, panel)
back.Padding(inset).Justify(overlayAlign(fy)).AlignItems(overlayAlign(fx))
if opts.Dim {
back.Background(OverlayScrim(k).Alpha(p))
}
core.OverlayStyle(c, panel, p)
w, h := c.Size()
panel.Label(opts.Label).Padding(core.Density(c).Unit() * 4).MaxWidth(w - 2*inset).MaxHeight(h - 2*inset)
ui.Scroll(c).Shrink(1).Children(content)
})
focus.Shown(panel)
if !shown {
slog.Debug("mujica: overlay close", "backdrop", backClicked)
*open = false
}
return panel
}
var panel ui.Element
ui.Overlay(c, func() {
w, h := c.Size()
panel = ui.Column(c).Absolute().Role(ui.RoleDialog).Label(opts.Label).Padding(core.Density(c).Unit() * 4).
MaxWidth(w - 2*inset).MaxHeight(h - 2*inset)
b := panel.Bounds()
panel.Left(inset + (w-2*inset-b.W)*fx).Top(inset + (h-2*inset-b.H)*fy)
p := core.OverlayEnter(c, panel)
if b.W == 0 {
p = 0 // not measured yet
c.AnimationFrame()
}
core.OverlayStyle(c, panel, p)
panel.Children(func() { ui.Scroll(c).Shrink(1).Children(content) })
})
focus.Shown(panel)
if core.OverlayEscape(c, open) {
slog.Debug("mujica: overlay close", "escape", true)
*open = false
}
return panel
}
func overlayFractions(p OverlayPlace) (fx, fy float32) {
switch p {
case OverlayTop:
return 0.5, 0
case OverlayBottom:
return 0.5, 1
case OverlayLeft:
return 0, 0.5
case OverlayRight:
return 1, 0.5
case OverlayTopLeft:
return 0, 0
case OverlayTopRight:
return 1, 0
case OverlayBottomLeft:
return 0, 1
case OverlayBottomRight:
return 1, 1
}
return 0.5, 0.5
}
func overlayAlign(f float32) ui.Align {
switch f {
case 0:
return ui.Start
case 1:
return ui.End
}
return ui.Center
}
// OverlayScrim is the color dimming the window behind a modal layer.
func OverlayScrim(k theme.Tokens) ui.Color {
if k.Dark {
return ui.RGBA(0, 0, 0, 0.6)
}
return k.Text.Alpha(0.42)
}
// OverlayAnchored is overlayEnter, held at 0 until the anchor b has been laid out.
func OverlayAnchored(c *ui.Context, panel ui.Element, b ui.Rect) float32 {
if b.W == 0 && b.H == 0 {
c.AnimationFrame()
return 0
}
return core.OverlayEnter(c, panel)
}