mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
a63e0cb44a
op.Offset is a convenience function most often used by layouts. Layouts usually operate in integer coordinates, and the float32 version of op.Offset needlessly force conversions from int to float32. This change makes op.Offset take integer coordinates, to better match its intended use. Signed-off-by: Elias Naur <mail@eliasnaur.com>
49 lines
1018 B
Go
49 lines
1018 B
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"math"
|
|
|
|
"gioui.org/layout"
|
|
"gioui.org/op"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/unit"
|
|
)
|
|
|
|
type ProgressCircleStyle struct {
|
|
Color color.NRGBA
|
|
Progress float32
|
|
}
|
|
|
|
func ProgressCircle(th *Theme, progress float32) ProgressCircleStyle {
|
|
return ProgressCircleStyle{
|
|
Color: th.Palette.ContrastBg,
|
|
Progress: progress,
|
|
}
|
|
}
|
|
|
|
func (p ProgressCircleStyle) Layout(gtx layout.Context) layout.Dimensions {
|
|
diam := gtx.Constraints.Min.X
|
|
if minY := gtx.Constraints.Min.Y; minY > diam {
|
|
diam = minY
|
|
}
|
|
if diam == 0 {
|
|
diam = gtx.Px(unit.Dp(24))
|
|
}
|
|
sz := gtx.Constraints.Constrain(image.Pt(diam, diam))
|
|
radius := sz.X / 2
|
|
defer op.Offset(image.Pt(radius, radius)).Push(gtx.Ops).Pop()
|
|
|
|
defer clipLoader(gtx.Ops, -math.Pi/2, -math.Pi/2+math.Pi*2*p.Progress, float32(radius)).Push(gtx.Ops).Pop()
|
|
paint.ColorOp{
|
|
Color: p.Color,
|
|
}.Add(gtx.Ops)
|
|
paint.PaintOp{}.Add(gtx.Ops)
|
|
return layout.Dimensions{
|
|
Size: sz,
|
|
}
|
|
}
|