~eliasnaur/gio-patches

widget/material: add progressbar v1 PROPOSED

metaclips: 1
 widget/material: add progressbar

 1 files changed, 67 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/10058/mbox | git am -3
Learn more about email & git

[PATCH] widget/material: add progressbar Export this patch

Add progressbar support to material widget
---
 widget/material/progressbar.go | 67 ++++++++++++++++++++++++++++++++++
 1 file changed, 67 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..2ac9ea3
--- /dev/null
+++ b/widget/material/progressbar.go
@@ -0,0 +1,67 @@
// 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 {
	BackgroundColor color.RGBA
	ProgressColor   color.RGBA
	CornerRadius    unit.Value
	Height          unit.Value
	Width           unit.Value
}

func (t *Theme) ProgressBar() ProgressBar {
	return ProgressBar{
		BackgroundColor: rgb(0xe6eaed),
		ProgressColor:   rgb(0x41be53),
		CornerRadius:    unit.Dp(6),
		Height:          unit.Dp(10),
		Width:           unit.Dp(500),
	}
}

func (b ProgressBar) Layout(gtx *layout.Context, progress float64) {
	shader := func(width int, color color.RGBA) {
		rr := float32(gtx.Px(b.CornerRadius))

		d := image.Point{X: width, Y: gtx.Px(b.Height)}
		dr := f32.Rectangle{
			Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
		}

		clip.Rect{
			Rect: f32.Rectangle{Max: f32.Point{X: float32(width), Y: float32(gtx.Px(b.Height))}},
			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
	}
	fillWidth := float64(gtx.Px(b.Width)) * (progress / 100)

	layout.Stack{Alignment: layout.W}.Layout(gtx,
		layout.Stacked(func() {
			shader(gtx.Px(b.Width), b.BackgroundColor)
		}),
		layout.Stacked(func() {
			shader(int(fillWidth), b.ProgressColor)
		}),
	)
}
-- 
2.21.1 (Apple Git-122.3)
Nice, thank you.

If you haven't already thought of it, please do add a ProgressBar example
(in a separate commit).

Nit: use the type name in the subject:

	widget/material: add ProgressBar



On Wed Mar 25, 2020 at 08:20, metaclips wrote:
View this thread in the archives