op/clip: expose Circle.Path and RRect.Path.

These can be nicely used together with clip.Stroke.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
This commit is contained in:
Egon Elbre
2021-03-14 12:09:21 +02:00
committed by Elias Naur
parent a50a0db3a2
commit fecfbbb050
2 changed files with 31 additions and 31 deletions
+17 -12
View File
@@ -51,11 +51,16 @@ type RRect struct {
// Op returns the op for the rounded rectangle.
func (rr RRect) Op(ops *op.Ops) Op {
return Outline{Path: rr.path(ops)}.Op()
return Outline{Path: rr.Path(ops)}.Op()
}
// path returns the path spec for the rounded rectangle.
func (rr RRect) path(ops *op.Ops) PathSpec {
// Add the rectangle clip.
func (rr RRect) Add(ops *op.Ops) {
rr.Op(ops).Add(ops)
}
// Path returns the PathSpec for the rounded rectangle.
func (rr RRect) Path(ops *op.Ops) PathSpec {
var p Path
p.Begin(ops)
@@ -91,11 +96,6 @@ func (rr RRect) path(ops *op.Ops) PathSpec {
return p.End()
}
// Add the rectangle clip.
func (rr RRect) Add(ops *op.Ops) {
rr.Op(ops).Add(ops)
}
// Border represents a rectangular border.
type Border struct {
// Rect is the bounds of the border.
@@ -115,7 +115,7 @@ func (b Border) Op(ops *op.Ops) Op {
Path: RRect{
Rect: b.Rect,
SE: b.SE, SW: b.SW, NW: b.NW, NE: b.NE,
}.path(ops),
}.Path(ops),
Style: StrokeStyle{
Width: b.Width,
},
@@ -136,11 +136,16 @@ type Circle struct {
// Op returns the op for the circle.
func (c Circle) Op(ops *op.Ops) Op {
return Outline{Path: c.path(ops)}.Op()
return Outline{Path: c.Path(ops)}.Op()
}
// path returns the path spec for the circle.
func (c Circle) path(ops *op.Ops) PathSpec {
// Add the circle clip.
func (c Circle) Add(ops *op.Ops) {
c.Op(ops).Add(ops)
}
// Path returns the PathSpec for the circle.
func (c Circle) Path(ops *op.Ops) PathSpec {
var p Path
p.Begin(ops)
+14 -19
View File
@@ -7,7 +7,6 @@ import (
"gioui.org/f32"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
@@ -23,24 +22,20 @@ type Border struct {
func (b Border) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
dims := w(gtx)
sz := dims.Size
rr := float32(gtx.Px(b.CornerRadius))
st := op.Save(gtx.Ops)
width := gtx.Px(b.Width)
sz.X -= width
sz.Y -= width
op.Offset(f32.Point{
X: float32(width) * 0.5,
Y: float32(width) * 0.5,
}).Add(gtx.Ops)
clip.Border{
Rect: f32.Rectangle{
Max: layout.FPt(sz),
},
NE: rr, NW: rr, SE: rr, SW: rr,
Width: float32(width),
}.Add(gtx.Ops)
paint.ColorOp{Color: b.Color}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
st.Load()
width := float32(gtx.Px(b.Width))
r := f32.Rectangle{Max: layout.FPt(sz)}
r = r.Add(f32.Point{X: width * 0.5, Y: width * 0.5})
paint.FillShape(gtx.Ops,
b.Color,
clip.Stroke{
Path: clip.UniformRRect(r, rr).Path(gtx.Ops),
Style: clip.StrokeStyle{Width: width},
}.Op(),
)
return dims
}