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>
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package code
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
// Span is timed work, starting inside its parent's time.
|
|
type Span struct {
|
|
Name string
|
|
Start, Length time.Duration
|
|
Children []Span
|
|
}
|
|
|
|
type ProfilerBarsOptions struct {
|
|
// Width is in dots; zero means 240.
|
|
Width int
|
|
}
|
|
|
|
var depths = []pixel.Slot{pixel.Accent, pixel.Info, pixel.Good, pixel.Warn, pixel.Magic}
|
|
|
|
// ProfilerBars stacks spans under their parents, each bar its time.
|
|
func ProfilerBars(c *ui.Context, text string, root Span, o ProfilerBarsOptions) ui.Element {
|
|
k := pixel.K(c)
|
|
w := pixel.Or("ProfilerBars Width", o.Width, 240)
|
|
if root.Length <= 0 {
|
|
panic(fmt.Sprintf("code: ProfilerBars root %q lasts %v", root.Name, root.Length))
|
|
}
|
|
var said []string
|
|
rows := check(root, &said)
|
|
x := func(d time.Duration) int { return int(int64(w) * int64(d-root.Start) / int64(root.Length)) }
|
|
return ui.Box(c).Size(k.D(w), k.D(rows*pixel.LineHeight)).Role(ui.RoleImage).Label(text).Description(strings.Join(said, "; ")).Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
var bar func(s Span, depth int)
|
|
bar = func(s Span, depth int) {
|
|
x0, y := x(s.Start), depth*pixel.LineHeight
|
|
bw := max(1, x(s.Start+s.Length)-x0)
|
|
g.Rect(x0, y+1, bw, pixel.LineHeight-2, depths[depth%len(depths)])
|
|
// Names show where they fit, times when they also fit.
|
|
for _, s := range []string{fmt.Sprintf("%s %.1fms", s.Name, ms(s.Length)), s.Name} {
|
|
if pixel.Measure(s)+4 <= bw {
|
|
g.Text(x0+2, y, s, pixel.AccentInk, 1)
|
|
break
|
|
}
|
|
}
|
|
for _, ch := range s.Children {
|
|
bar(ch, depth+1)
|
|
}
|
|
}
|
|
bar(root, 0)
|
|
})
|
|
}
|
|
|
|
// check counts rows; a child outside its parent panics.
|
|
func check(s Span, said *[]string) int {
|
|
*said = append(*said, fmt.Sprintf("%s %.1f ms", s.Name, ms(s.Length)))
|
|
rows := 1
|
|
for _, ch := range s.Children {
|
|
if ch.Length < 0 || ch.Start < s.Start || ch.Start+ch.Length > s.Start+s.Length {
|
|
panic(fmt.Sprintf("code: ProfilerBars span %q is outside %q", ch.Name, s.Name))
|
|
}
|
|
rows = max(rows, 1+check(ch, said))
|
|
}
|
|
return rows
|
|
}
|