mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
94d242d18c
PaintOp.Rect is the wrong abstraction; it implies a clip operation better handled by package clip, and not all paints need it (colors). Furthermore, it's awkward to specify a PaintOp that fills up the current clip area, regardless of its size. Redefine PathOp to mean "fill current clip area". API change. Replace uses of PaintOp.Rect with a TransformOp applied before the PaintOp. Leave a TODO for the PathOp infinity area. Fixes gio#167 Signed-off-by: Elias Naur <mail@eliasnaur.com>
73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
|
|
"gioui.org/f32"
|
|
"gioui.org/internal/f32color"
|
|
"gioui.org/layout"
|
|
"gioui.org/op/clip"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/unit"
|
|
)
|
|
|
|
type ProgressBarStyle struct {
|
|
Color color.RGBA
|
|
Progress int
|
|
}
|
|
|
|
func ProgressBar(th *Theme, progress int) ProgressBarStyle {
|
|
return ProgressBarStyle{
|
|
Progress: progress,
|
|
Color: th.Color.Primary,
|
|
}
|
|
}
|
|
|
|
func (p ProgressBarStyle) Layout(gtx layout.Context) layout.Dimensions {
|
|
shader := func(width float32, color color.RGBA) layout.Dimensions {
|
|
maxHeight := unit.Dp(4)
|
|
rr := float32(gtx.Px(unit.Dp(2)))
|
|
|
|
d := image.Point{X: int(width), Y: gtx.Px(maxHeight)}
|
|
|
|
clip.RRect{
|
|
Rect: f32.Rectangle{Max: f32.Point{X: width, Y: float32(gtx.Px(maxHeight))}},
|
|
NE: rr, NW: rr, SE: rr, SW: rr,
|
|
}.Add(gtx.Ops)
|
|
|
|
paint.ColorOp{Color: color}.Add(gtx.Ops)
|
|
paint.PaintOp{}.Add(gtx.Ops)
|
|
|
|
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 {
|
|
// Use a transparent equivalent of progress color.
|
|
bgCol := f32color.MulAlpha(p.Color, 150)
|
|
|
|
return shader(progressBarWidth, bgCol)
|
|
}),
|
|
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
|
|
fillWidth := (progressBarWidth / 100) * float32(progress)
|
|
fillColor := p.Color
|
|
if gtx.Queue == nil {
|
|
fillColor = f32color.MulAlpha(fillColor, 200)
|
|
}
|
|
return shader(fillWidth, fillColor)
|
|
}),
|
|
)
|
|
}
|