Files
gio-patched/widget/material/loader.go
T
Sebastien Binet be89f8b945 all: introduce Outline and Stroke builders
This CL introduces 2 new path builders:
- Outline which takes a PathSpec to be outlined
- Stroke which takes a PathSpec and a stroke style, to stroke a path.

typically, code like this:

  var p clip.Path
  ...
  p.Outline().Add(o)

should be replaced with:

  var p clip.Path
  ...
  clip.Outline{Path: p.End()}.Op().Add(o)

similarly, stroking should be modified from:

  var p clip.Path
  ...
  p.Stroke(width, clip.StrokeStyle{...}).Add(o)

to:

  var p clip.Path
  ...
  clip.Stroke{Path: p.End(), Style: clip.StrokeStyle{Width:...}}.Op().Add(o)

here are tentative 'rf' scripts (see rsc.io/rf for more details):

  ```
  ex {
  	import "gioui.org/op";
  	import "gioui.org/op/clip";

  	var p clip.Path;
  	var o *op.Ops;

  	p.Outline().Add(o) -> clip.Outline{Path:p.End()}.Op().Add(o);
  }

  ex {
  	import "gioui.org/op";
  	import "gioui.org/op/clip";

  	var o *op.Ops;
  	var p clip.Path;
  	var sty clip.StrokeStyle;
  	var width float32;

  	p.Stroke(width, sty).Add(o) ->   \
	    clip.Stroke{                 \
		Path:p.End(),            \
		Style: clip.StrokeStyle{ \
		    Width: width,        \
	    }}.Op().Add(o);
  }
  ```

Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-12-09 09:44:15 +01:00

84 lines
1.7 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package material
import (
"image"
"image/color"
"math"
"time"
"gioui.org/f32"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
)
type LoaderStyle struct {
Color color.NRGBA
}
func Loader(th *Theme) LoaderStyle {
return LoaderStyle{
Color: th.Palette.ContrastBg,
}
}
func (l LoaderStyle) 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 := float64(sz.X) * .5
defer op.Push(gtx.Ops).Pop()
op.Offset(f32.Pt(float32(radius), float32(radius))).Add(gtx.Ops)
dt := (time.Duration(gtx.Now.UnixNano()) % (time.Second)).Seconds()
startAngle := dt * math.Pi * 2
endAngle := startAngle + math.Pi*1.5
clipLoader(gtx.Ops, startAngle, endAngle, radius)
paint.ColorOp{
Color: l.Color,
}.Add(gtx.Ops)
op.Offset(f32.Pt(-float32(radius), -float32(radius))).Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
op.InvalidateOp{}.Add(gtx.Ops)
return layout.Dimensions{
Size: sz,
}
}
func clipLoader(ops *op.Ops, startAngle, endAngle, radius float64) {
const thickness = .25
var (
width = float32(radius * thickness)
delta = float32(endAngle - startAngle)
vy, vx = math.Sincos(startAngle)
pen = f32.Pt(float32(vx), float32(vy)).Mul(float32(radius))
center = f32.Pt(0, 0).Sub(pen)
p clip.Path
)
p.Begin(ops)
p.Move(pen)
p.Arc(center, center, delta)
clip.Stroke{
Path: p.End(),
Style: clip.StrokeStyle{
Width: width,
Cap: clip.FlatCap,
},
}.Op().Add(ops)
}