gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/widget/material/progresscircle.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package material 4 5 import ( 6 "image" 7 "image/color" 8 "math" 9 10 "gioui.org/layout" 11 "gioui.org/op" 12 "gioui.org/op/paint" 13 ) 14 15 type ProgressCircleStyle struct { 16 Color color.NRGBA 17 Progress float32 18 } 19 20 func ProgressCircle(th *Theme, progress float32) ProgressCircleStyle { 21 return ProgressCircleStyle{ 22 Color: th.Palette.ContrastBg, 23 Progress: progress, 24 } 25 } 26 27 func (p ProgressCircleStyle) Layout(gtx layout.Context) layout.Dimensions { 28 diam := gtx.Constraints.Min.X 29 if minY := gtx.Constraints.Min.Y; minY > diam { 30 diam = minY 31 } 32 if diam == 0 { 33 diam = gtx.Dp(24) 34 } 35 sz := gtx.Constraints.Constrain(image.Pt(diam, diam)) 36 radius := sz.X / 2 37 defer op.Offset(image.Pt(radius, radius)).Push(gtx.Ops).Pop() 38 39 defer clipLoader(gtx.Ops, -math.Pi/2, -math.Pi/2+math.Pi*2*p.Progress, float32(radius)).Push(gtx.Ops).Pop() 40 paint.ColorOp{ 41 Color: p.Color, 42 }.Add(gtx.Ops) 43 paint.PaintOp{}.Add(gtx.Ops) 44 return layout.Dimensions{ 45 Size: sz, 46 } 47 }