Files
ZacharyZhang-NYandClaude Opus 5.5 03e1f91706 Upgrade MyGo to v0.5.5
Elements are now pass-scoped value handles: tests that read bounds
after a frame bind a ui.Handle. pixel tells frames apart by the build
pass's root, since the Tester clock no longer moves on its own.

Fix the regressions the upgrade brought:
- Clipboard reads in input handlers go through c.Services().
- MyGo notes an overlay's opener after building it, so AutoFocus
  inside lost it; pixel.AutoFocus focuses through a Handle, and
  archtest bans the raw call. Dialogs hand the focus back again.
- Elements around a press stay hovered: Tooltip tells a press-given
  focus by focus arriving under the pointer.
- The web host moves the Tester clock with real time, at most 500 ms
  a call, and gives components the real clock.

The gallery's source index also accepts CRLF checkouts.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:31:08 -04:00

48 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package layout
import (
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type ViewportOptions struct {
// W×H is the virtual screen in dots; zero means 160×144.
W, H int
}
// Viewport fills its parent and draws children at W×H dots.
func Viewport(c *ui.Context, o ViewportOptions, children func()) ui.Element {
k := pixel.K(c)
vw, vh := pixel.Or("Viewport W", o.W, 160), pixel.Or("Viewport H", o.H, 144)
base := int(k.Dot())
outer := ui.Box(c).Fill()
was := outer.Bounds()
d := fit(vw, vh, was.W, was.H, base)
// Offsets stay whole outer dots, so both grids line up.
x := (int(was.W)/base - vw*d/base) / 2 * base
y := (int(was.H)/base - vh*d/base) / 2 * base
return outer.Draw(func(p *ui.Painter, r ui.Rect) {
if r.W != was.W || r.H != was.H {
c.Invalidate()
}
g := k.Canvas(p, r)
g.Rect(0, 0, g.W, g.H, pixel.Back)
}).Children(func() {
s := pixel.Current(c)
s.Dot = d
pixel.Scope(c, s, func() {
ui.Box(c).Absolute().Left(float32(max(0, x))).Top(float32(max(0, y))).Size(float32(vw*d), float32(vh*d)).Clip().Children(children)
})
})
}
// fit picks the largest fitting multiple of base, ≤ 4.
func fit(w, h int, roomW, roomH float32, base int) int {
d := base
for d+base <= 4 && float32(w*(d+base)) <= roomW && float32(h*(d+base)) <= roomH {
d += base
}
return d
}