From fecfbbb050da50dc111cffafcf8dc16a7028b745 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Sun, 14 Mar 2021 12:09:21 +0200 Subject: [PATCH] op/clip: expose Circle.Path and RRect.Path. These can be nicely used together with clip.Stroke. Signed-off-by: Egon Elbre --- op/clip/shapes.go | 29 +++++++++++++++++------------ widget/border.go | 33 ++++++++++++++------------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/op/clip/shapes.go b/op/clip/shapes.go index 0157f36d..53b8fd55 100644 --- a/op/clip/shapes.go +++ b/op/clip/shapes.go @@ -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) diff --git a/widget/border.go b/widget/border.go index c3f6ede7..fac2aa7a 100644 --- a/widget/border.go +++ b/widget/border.go @@ -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 }