github.com/utopiagio/gio@v0.0.8/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  	"github.com/utopiagio/gio/layout"
    11  	"github.com/utopiagio/gio/op"
    12  	"github.com/utopiagio/gio/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  }