Files
8bitMyGoUI/layout/divider.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

96 lines
1.7 KiB
Go

package layout
import (
"fmt"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type DividerStyle uint8
const (
Solid DividerStyle = iota
Dotted
Dashed
Double
Ornament
)
type DividerOptions struct {
Vertical bool
Style DividerStyle
// Slot is the ink; zero means Edge.
Slot pixel.Slot
}
var diamond = pixel.MustSprite(`
..#..
.###.
#####
.###.
..#..
`)
// Divider is a rule across its parent.
func Divider(c *ui.Context, o DividerOptions) ui.Element {
k := pixel.K(c)
ink := o.Slot
if ink == pixel.Ink {
ink = pixel.Edge
}
thick := [...]int{Solid: 1, Dotted: 1, Dashed: 1, Double: 3, Ornament: 5}
if int(o.Style) >= len(thick) {
panic(fmt.Sprintf("layout: unknown divider style %d", o.Style))
}
t := thick[o.Style]
e := ui.Box(c).Role(ui.RoleNone)
if o.Vertical {
e.FillHeight().Width(k.D(t))
} else {
e.FillWidth().Height(k.D(t))
}
return e.Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
n := g.W
if o.Vertical {
n = g.H
}
line := func(at, from, n int) {
x, y := from, at
if o.Vertical {
x, y = at, from
}
switch o.Style {
case Dotted:
w, h := n, 1
if o.Vertical {
w, h = 1, n
}
g.Checker(x, y, w, h, ink)
case Dashed:
rule(g, x, y, n, o.Vertical, 4, 2, ink)
default:
rule(g, x, y, n, o.Vertical, max(n, 1), 0, ink)
}
}
switch o.Style {
case Double:
line(0, 0, n)
line(2, 0, n)
case Ornament:
mid := n / 2
line(2, 0, mid-4)
line(2, mid+5, n-mid-5)
if o.Vertical {
g.Sprite(0, mid-2, diamond, 0, ink, 1)
} else {
g.Sprite(mid-2, 0, diamond, 0, ink, 1)
}
default:
line(0, 0, n)
}
})
}