Files
prevara/progressbar.go
2025-11-30 16:45:22 +00:00

84 lines
2.0 KiB
Go

package gel
import (
"image"
"image/color"
l "github.com/p9c/p9/pkg/gel/gio/layout"
"github.com/p9c/p9/pkg/gel/gio/op/clip"
"github.com/p9c/p9/pkg/gel/gio/op/paint"
"github.com/p9c/p9/pkg/gel/gio/unit"
"github.com/p9c/p9/pkg/gel/f32color"
)
type ProgressBar struct {
*Window
color color.NRGBA
progress int
}
// ProgressBar renders a horizontal bar with an indication of completion of a process
func (w *Window) ProgressBar() *ProgressBar {
return &ProgressBar{
Window: w,
progress: 0,
color: w.Colors.GetNRGBAFromName("Primary"),
}
}
// SetProgress sets the progress of the progress bar
func (p *ProgressBar) SetProgress(progress int) *ProgressBar {
p.progress = progress
return p
}
// Color sets the color to render the bar in
func (p *ProgressBar) Color(c string) *ProgressBar {
p.color = p.Theme.Colors.GetNRGBAFromName(c)
return p
}
// Fn renders the progress bar as it is currently configured
func (p *ProgressBar) Fn(gtx l.Context) l.Dimensions {
shader := func(width float32, col color.NRGBA) l.Dimensions {
maxHeight := unit.Dp(4)
rr := gtx.Dp(unit.Dp(2))
d := image.Point{X: int(width), Y: gtx.Dp(maxHeight)}
defer clip.UniformRRect(image.Rectangle{Max: image.Pt(int(width), gtx.Dp(maxHeight))}, rr).Push(gtx.Ops).Pop()
paint.ColorOp{Color: col}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
return l.Dimensions{Size: d}
}
progress := p.progress
if progress > 100 {
progress = 100
} else if progress < 0 {
progress = 0
}
progressBarWidth := float32(gtx.Constraints.Max.X)
return l.Stack{Alignment: l.W}.Layout(gtx,
l.Stacked(func(gtx l.Context) l.Dimensions {
// Use a transparent equivalent of progress color.
bgCol := f32color.MulAlpha(p.color, 150)
return shader(progressBarWidth, bgCol)
}),
l.Stacked(func(gtx l.Context) l.Dimensions {
fillWidth := (progressBarWidth / 100) * float32(progress)
fillColor := p.color
if !gtx.Source.Enabled() {
fillColor = f32color.MulAlpha(fillColor, 200)
}
return shader(fillWidth, fillColor)
}),
)
}