Files
gio/widget/material/progressbar.go
T
Elias Naur 7bf3265ccd layout,widget: transpose Constraints to use image.Points for limits
Instead of

    type Contraints struct {
	    Width, Height Constraint
    }

use

    type Constraints struct {
	    Min, Max image.Point
    }

which leads to simpler use. For example, the Min method is trivally replaced by
the field, and the RigidConstraints constructor is no longer a net win.

API Change. Rewrites:

    gofmt -r 'gtx.Constraints.Min() -> gtx.Constraints.Min'
    gofmt -r 'gtx.Constraints.Width.Min -> gtx.Constraints.Min.X'
    gofmt -r 'gtx.Constraints.Height.Min -> gtx.Constraints.Min.Y'
    gofmt -r 'gtx.Constraints.Height.Max -> gtx.Constraints.Max.Y'
    gofmt -r 'gtx.Constraints.Width.Max -> gtx.Constraints.Max.X'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-19 09:58:07 +02:00

69 lines
1.4 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package material
import (
"image"
"image/color"
"gioui.org/f32"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
)
type ProgressBarStyle struct {
Color color.RGBA
}
func ProgressBar(th *Theme) ProgressBarStyle {
return ProgressBarStyle{
Color: th.Color.Primary,
}
}
func (b ProgressBarStyle) Layout(gtx *layout.Context, progress int) {
shader := func(width float32, color color.RGBA) {
maxHeight := unit.Dp(4)
rr := float32(gtx.Px(unit.Dp(2)))
d := image.Point{X: int(width), Y: gtx.Px(maxHeight)}
dr := f32.Rectangle{
Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
}
clip.Rect{
Rect: f32.Rectangle{Max: f32.Point{X: width, Y: float32(gtx.Px(maxHeight))}},
NE: rr, NW: rr, SE: rr, SW: rr,
}.Op(gtx.Ops).Add(gtx.Ops)
paint.ColorOp{Color: color}.Add(gtx.Ops)
paint.PaintOp{Rect: dr}.Add(gtx.Ops)
gtx.Dimensions = layout.Dimensions{Size: d}
}
if progress > 100 {
progress = 100
} else if progress < 0 {
progress = 0
}
progressBarWidth := float32(gtx.Constraints.Max.X)
layout.Stack{Alignment: layout.W}.Layout(gtx,
layout.Stacked(func() {
// Use a transparent equivalent of progress color.
backgroundColor := b.Color
backgroundColor.A = 100
shader(progressBarWidth, backgroundColor)
}),
layout.Stacked(func() {
fillWidth := (progressBarWidth / 100) * float32(progress)
shader(fillWidth, b.Color)
}),
)
}