Files
8bitMyGoUI/layout/titlebar.go
T
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

90 lines
2.1 KiB
Go

package layout
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
var (
closeGlyph = pixel.MustSprite(`
#...#
.#.#.
..#..
.#.#.
#...#
`)
minimizeGlyph = pixel.MustSprite(`
.....
.....
.....
.....
#####
`)
)
type TitleBarOptions struct {
// NoMinimize leaves out the minimize box.
NoMinimize bool
Disabled bool
}
type TitleBarResult struct {
Element ui.Element
Close bool
Minimize bool
}
// TitleBar is a striped bar: title, close box, minimize box.
func TitleBar(c *ui.Context, title string, o TitleBarOptions) TitleBarResult {
k := pixel.K(c)
tw := pixel.Measure(title)
res := TitleBarResult{}
res.Element = ui.Row(c).FillWidth().Height(k.D(pixel.LineHeight+2)).AlignItems(ui.Center).Padding(0, k.D(4)).Role(ui.RoleGroup).Label(title).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Rect(0, 0, g.W, g.H, pixel.Face)
g.HLine(0, g.H-1, g.W, pixel.Edge)
for y := 4; y < g.H-4; y += 2 {
g.HLine(2, y, g.W-4, pixel.Shade)
}
x := (g.W - tw) / 2
g.Rect(x-6, 0, tw+12, g.H-1, pixel.Face)
g.Text(x, 1, title, pixel.Ink, 1)
}).Children(func() {
res.Close = windowBox(c, k, closeGlyph, k.Msg("close"), o.Disabled)
ui.Spacer(c)
if !o.NoMinimize {
res.Minimize = windowBox(c, k, minimizeGlyph, k.Msg("minimize"), o.Disabled)
}
})
if res.Close || res.Minimize {
slog.Debug("8bit: titlebar", "title", title, "close", res.Close, "minimize", res.Minimize)
}
return res
}
// windowBox is a framed button showing its glyph on hover.
func windowBox(c *ui.Context, k pixel.Kit, glyph *pixel.Sprite, name string, disabled bool) bool {
shell, base, st := pixel.Shell(c, func() ui.Element { return ui.ButtonBase(c).Label(name).Disabled(disabled) })
shell.Size(k.D(11), k.D(11)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
fill := pixel.Face
if st.Press {
fill = pixel.Shade
}
box(g, pixel.Flat, fill)
if st.Hover || st.Press || st.Focus {
g.Sprite(3, 3, glyph, 0, pixel.Ink, 1)
}
if st.Disabled {
g.Checker(0, 0, g.W, g.H, pixel.Face)
}
if st.Focus {
g.FocusMark(0, 0, g.W, g.H)
}
})
return base.Clicked()
}