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. // Op returns the op for the rounded rectangle.
func (rr RRect) Op(ops *op.Ops) Op { 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. // Add the rectangle clip.
func (rr RRect) path(ops *op.Ops) PathSpec { 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 var p Path
p.Begin(ops) p.Begin(ops)
@@ -91,11 +96,6 @@ func (rr RRect) path(ops *op.Ops) PathSpec {
return p.End() return p.End()
} }
// Add the rectangle clip.
func (rr RRect) Add(ops *op.Ops) {
rr.Op(ops).Add(ops)
}
// Border represents a rectangular border. // Border represents a rectangular border.
type Border struct { type Border struct {
// Rect is the bounds of the border. // Rect is the bounds of the border.
@@ -115,7 +115,7 @@ func (b Border) Op(ops *op.Ops) Op {
Path: RRect{ Path: RRect{
Rect: b.Rect, Rect: b.Rect,
SE: b.SE, SW: b.SW, NW: b.NW, NE: b.NE, SE: b.SE, SW: b.SW, NW: b.NW, NE: b.NE,
}.path(ops), }.Path(ops),
Style: StrokeStyle{ Style: StrokeStyle{
Width: b.Width, Width: b.Width,
}, },
@@ -136,11 +136,16 @@ type Circle struct {
// Op returns the op for the circle. // Op returns the op for the circle.
func (c Circle) Op(ops *op.Ops) Op { 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. // Add the circle clip.
func (c Circle) path(ops *op.Ops) PathSpec { 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 var p Path
p.Begin(ops) p.Begin(ops)
+14 -19
View File
@@ -7,7 +7,6 @@ import (
"gioui.org/f32" "gioui.org/f32"
"gioui.org/layout" "gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip" "gioui.org/op/clip"
"gioui.org/op/paint" "gioui.org/op/paint"
"gioui.org/unit" "gioui.org/unit"
@@ -23,24 +22,20 @@ type Border struct {
func (b Border) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions { func (b Border) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
dims := w(gtx) dims := w(gtx)
sz := dims.Size sz := dims.Size
rr := float32(gtx.Px(b.CornerRadius)) rr := float32(gtx.Px(b.CornerRadius))
st := op.Save(gtx.Ops) width := float32(gtx.Px(b.Width))
width := gtx.Px(b.Width)
sz.X -= width r := f32.Rectangle{Max: layout.FPt(sz)}
sz.Y -= width r = r.Add(f32.Point{X: width * 0.5, Y: width * 0.5})
op.Offset(f32.Point{
X: float32(width) * 0.5, paint.FillShape(gtx.Ops,
Y: float32(width) * 0.5, b.Color,
}).Add(gtx.Ops) clip.Stroke{
clip.Border{ Path: clip.UniformRRect(r, rr).Path(gtx.Ops),
Rect: f32.Rectangle{ Style: clip.StrokeStyle{Width: width},
Max: layout.FPt(sz), }.Op(),
}, )
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()
return dims return dims
} }