Files
gio/widget/material/progresscircle.go
T
Elias Naur 3d37491342 all: [API] replace unit.Value with separate unit.Dp, unit.Sp types
The unit.Value is a struct and thus more inconvenient to use than its
underlying float32 type. In addition, most uses don't need a general
value, but rather a specific unit given by the context. This change
replaces unit.Value with two float32 units, Dp and Sp. It also changes
variables and parameters of unit.Value to a specific unit type matching
the context. That is, unit.Dp everywhere except for text sizes which are
in Sp.

Switching to typed float32s has multiple advantages

- They can be constants:

const touchSlop = unit.Dp(16)

- Casting untyped constants is no longer necessary:

insets := layout.UniformInset(16)

- Calculation with values is natural:

func (s ScrollbarStyle) Width() unit.Dp {
	return s.Indicator.MinorWidth + s.Track.MinorPadding + s.Track.MinorPadding
}

The main API change is that calls to gtx.Px must be replaced with either
gtx.Dp or gtx.Sp depending on the unit.

Idea by Christophe Meessen.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00

48 lines
991 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"
)
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.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,
}
}