core/progressbar/progressbar.go
Functions
func Children
func (w *Widget) Children() []widget.Widget {
return nil
}
func Draw
Draw renders the progress bar to the canvas.
func (w *Widget) Draw(_ widget.Context, canvas widget.Canvas) {
value := clampValue(w.cfg.ResolvedValue())
w.lastDrawnValue = value
w.lastDrawnValueSet = true
barH := w.cfg.height
if barH <= 0 {
barH = defaultBarHeight
}
radius := w.cfg.radius
if radius < 0 {
radius = defaultBarRadius
}
// Use default radius when not explicitly set (zero value).
if !w.cfg.radiusSet {
radius = defaultBarRadius
}
label := w.resolveLabel(value)
w.painter.PaintProgressBar(canvas, PaintState{
Value: value,
Bounds: w.Bounds(),
BarHeight: barH,
Radius: radius,
ShowLabel: w.cfg.showLabel,
Label: label,
Disabled: w.cfg.ResolvedDisabled(),
ProgressBarColorScheme: w.cfg.colorScheme,
})
}
func Event
Event handles an input event. Progress bars are display-only and always
return false (events are never consumed).
func (w *Widget) Event(_ widget.Context, _ event.Event) bool {
return false
}
func Layout
Layout calculates the progress bar's preferred size within the given constraints.
func (w *Widget) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size {
barH := w.cfg.height
if barH <= 0 {
barH = defaultBarHeight
}
preferred := geometry.Sz(
preferredBarWidth+w.padding*2,
barH+w.padding*2,
)
return constraints.Constrain(preferred)
}
func Mount
Mount creates signal bindings for push-based invalidation.
Implements [widget.Lifecycle].
The value signal binding deduplicates notifications: if the signal fires
with the same value that was last drawn, the widget is not marked dirty.
This prevents flicker from redundant redraws (#112).
func (w *Widget) Mount(ctx widget.Context) {
sched := ctx.Scheduler()
if sched == nil {
return
}
// Value signal: deduplicate by comparing against lastDrawnValue.
if w.cfg.readonlyValueSignal != nil {
b := w.bindValueSignal(w.cfg.readonlyValueSignal, sched)
w.AddBinding(b)
} else if w.cfg.valueSignal != nil {
b := w.bindValueSignal(w.cfg.valueSignal, sched)
w.AddBinding(b)
}
// Disabled signal: no deduplication needed (bool changes are infrequent).
if w.cfg.readonlyDisabledSignal != nil {
b := state.BindToScheduler(w.cfg.readonlyDisabledSignal, w, sched)
w.AddBinding(b)
} else if w.cfg.disabledSignal != nil {
b := state.BindToScheduler(w.cfg.disabledSignal, w, sched)
w.AddBinding(b)
}
}
func New
New creates a new progress bar Widget with the given options.
The returned widget is visible and enabled by default. It is not focusable
because progress bars are display-only widgets.
func New(opts ...Option) *Widget {
w := &Widget{
padding: defaultPadding,
painter: DefaultPainter{},
}
w.SetVisible(true)
w.SetEnabled(true)
for _, opt := range opts {
opt(&w.cfg)
}
// Apply painter from config if set.
if w.cfg.painter != nil {
w.painter = w.cfg.painter
}
return w
}
func Padding
Padding sets the padding around the progress bar.
Returns the widget for method chaining.
func (w *Widget) Padding(v float32) *Widget {
w.padding = v
return w
}
func SetValue
SetValue updates the progress bar's static value.
The value is clamped to [0, 1].
func (w *Widget) SetValue(v float64) {
v = clampValue(v)
if w.cfg.value != v {
w.cfg.value = v
w.SetNeedsRedraw(true)
}
}
func Unmount
Unmount is called when the progress bar is removed from the widget tree.
Implements [widget.Lifecycle].
func (w *Widget) Unmount() {
// Bindings are cleaned up automatically by WidgetBase.CleanupBindings().
}
func Value
Value returns the current resolved value (0 to 1).
func (w *Widget) Value() float64 {
return clampValue(w.cfg.ResolvedValue())
}
Structs
type Widget struct
Widget implements a linear progress bar showing a value between 0 and 1.
A progress bar is created with [New] using functional options:
bar := progressbar.New(
progressbar.Value(0.65),
progressbar.ShowLabel(true),
progressbar.Height(20),
)
Fluent styling methods may be chained after construction:
bar.Padding(8)
type Widget struct {
widget.WidgetBase
cfg config
painter Painter
// Styling overrides set via fluent methods.
padding float32
// lastDrawnValue caches the value from the most recent Draw call.
// Signal bindings compare against this to suppress redundant redraws
// when the signal fires with an unchanged value (#112).
lastDrawnValue float64
// lastDrawnValueSet tracks whether lastDrawnValue has been populated
// by at least one Draw call. Zero is a valid progress value, so we
// need a separate flag.
lastDrawnValueSet bool
}
Children returns nil because a progress bar is a leaf widget.