border.go raw

   1  // SPDX-License-Identifier: Unlicense OR MIT
   2  
   3  package widget
   4  
   5  import (
   6  	"image/color"
   7  
   8  	"github.com/p9c/p9/pkg/gel/gio/f32"
   9  	"github.com/p9c/p9/pkg/gel/gio/layout"
  10  	"github.com/p9c/p9/pkg/gel/gio/op/clip"
  11  	"github.com/p9c/p9/pkg/gel/gio/op/paint"
  12  	"github.com/p9c/p9/pkg/gel/gio/unit"
  13  )
  14  
  15  // Border lays out a widget and draws a border inside it.
  16  type Border struct {
  17  	Color        color.NRGBA
  18  	CornerRadius unit.Value
  19  	Width        unit.Value
  20  }
  21  
  22  func (b Border) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
  23  	dims := w(gtx)
  24  	sz := layout.FPt(dims.Size)
  25  
  26  	rr := float32(gtx.Px(b.CornerRadius))
  27  	width := float32(gtx.Px(b.Width))
  28  	sz.X -= width
  29  	sz.Y -= width
  30  
  31  	r := f32.Rectangle{Max: sz}
  32  	r = r.Add(f32.Point{X: width * 0.5, Y: width * 0.5})
  33  
  34  	paint.FillShape(gtx.Ops,
  35  		b.Color,
  36  		clip.Stroke{
  37  			Path:  clip.UniformRRect(r, rr).Path(gtx.Ops),
  38  			Style: clip.StrokeStyle{Width: width},
  39  		}.Op(),
  40  	)
  41  
  42  	return dims
  43  }
  44