Files
8bitMyGoUI/feedback/progress.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

58 lines
1.3 KiB
Go

package feedback
import (
"fmt"
"math"
"strconv"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type ProgressOptions struct {
// Width is the bar in dots; zero means 120.
Width int
// Percent shows the number after the bar.
Percent bool
}
// Progress fills chunk by chunk; below zero a block runs.
func Progress(c *ui.Context, text string, value float64, o ProgressOptions) ui.Element {
k := pixel.K(c)
if value > 1 || math.IsNaN(value) {
panic(fmt.Sprintf("feedback: Progress value %v is past 1", value))
}
w := pixel.Or("Progress Width", o.Width, 120)
n := (w - 3) / 5
pct := strconv.Itoa(int(math.Round(value*100))) + "%"
e := ui.Box(c).Role(ui.RoleProgress).Label(text)
full := w
if value < 0 {
e.Description(k.Msg("loading"))
} else {
e.Description(pct)
if o.Percent {
full += 4 + pixel.MicroWidth("100%")
}
}
return e.Size(k.D(full), k.D(10)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Frame(0, 0, w, 10, pixel.Sunken, pixel.Well)
chunk := func(i int) { g.Rect(3+i*5, 3, 4, 4, pixel.Accent) }
if value < 0 {
at := pingpong(g.Busy(12), n-3)
for i := range 3 {
chunk(at + i)
}
return
}
for i := range int(value * float64(n)) {
chunk(i)
}
if o.Percent {
g.Micro(w+4, 2, pct, pixel.Ink, 1)
}
})
}