mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 15:45:38 +00:00
widget/material: use float32 for progress
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
This commit is contained in:
@@ -17,10 +17,10 @@ import (
|
||||
type ProgressBarStyle struct {
|
||||
Color color.NRGBA
|
||||
TrackColor color.NRGBA
|
||||
Progress int
|
||||
Progress float32
|
||||
}
|
||||
|
||||
func ProgressBar(th *Theme, progress int) ProgressBarStyle {
|
||||
func ProgressBar(th *Theme, progress float32) ProgressBarStyle {
|
||||
return ProgressBarStyle{
|
||||
Progress: progress,
|
||||
Color: th.Palette.ContrastBg,
|
||||
@@ -46,21 +46,13 @@ func (p ProgressBarStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
return layout.Dimensions{Size: d}
|
||||
}
|
||||
|
||||
progress := p.Progress
|
||||
if progress > 100 {
|
||||
progress = 100
|
||||
} else if progress < 0 {
|
||||
progress = 0
|
||||
}
|
||||
|
||||
progressBarWidth := float32(gtx.Constraints.Max.X)
|
||||
|
||||
return layout.Stack{Alignment: layout.W}.Layout(gtx,
|
||||
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
|
||||
return shader(progressBarWidth, p.TrackColor)
|
||||
}),
|
||||
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
|
||||
fillWidth := (progressBarWidth / 100) * float32(progress)
|
||||
fillWidth := progressBarWidth * clamp1(p.Progress)
|
||||
fillColor := p.Color
|
||||
if gtx.Queue == nil {
|
||||
fillColor = f32color.MulAlpha(fillColor, 200)
|
||||
@@ -69,3 +61,14 @@ func (p ProgressBarStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// clamp1 limits v to range [0..1].
|
||||
func clamp1(v float32) float32 {
|
||||
if v >= 1 {
|
||||
return 1
|
||||
} else if v <= 0 {
|
||||
return 0
|
||||
} else {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user