~eliasnaur/gio-patches

widget/material: add ProgressBar v2 PROPOSED

metaclips: 1
 widget/material: add ProgressBar

 1 files changed, 68 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/10069/mbox | git am -3
Learn more about email & git

[PATCH v2] widget/material: add ProgressBar Export this patch

Add progress indicator support to material widget

Signed-off-by: metaclips <utimichael9@gmail.com>
---
 widget/material/progressbar.go | 68 ++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 widget/material/progressbar.go

diff --git a/widget/material/progressbar.go b/widget/material/progressbar.go
new file mode 100644
index 0000000..b4528a0
--- /dev/null
+++ b/widget/material/progressbar.go
@@ -0,0 +1,68 @@
// 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 ProgressBar struct {
	Color color.RGBA
}

func (t *Theme) ProgressBar() ProgressBar {
	return ProgressBar{
		Color: t.Color.Primary,
	}
}

func (b ProgressBar) Layout(gtx *layout.Context, progress int) {
	shader := func(width float32, color color.RGBA) {
		maxHeight := unit.Dp(10)
		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.Width.Max)

	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)
		}),
	)
}
-- 
2.21.1 (Apple Git-122.3)
View this thread in the archives