76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package gel
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
|
|
l "gioui.org/layout"
|
|
"gioui.org/op/clip"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/unit"
|
|
)
|
|
|
|
// Border lays out a widget and draws a border inside it.
|
|
type Border struct {
|
|
*Window
|
|
color color.NRGBA
|
|
cornerRadius unit.Dp
|
|
width unit.Dp
|
|
w l.Widget
|
|
}
|
|
|
|
// Border creates a border with configurable color, width and corner radius.
|
|
func (w *Window) Border() *Border {
|
|
b := &Border{Window: w}
|
|
b.CornerRadius(0.25).Color("Primary").Width(0.125)
|
|
return b
|
|
}
|
|
|
|
// Color sets the color to render the border in
|
|
func (b *Border) Color(color string) *Border {
|
|
b.color = b.Theme.Colors.GetNRGBAFromName(color)
|
|
return b
|
|
}
|
|
|
|
// CornerRadius sets the radius of the curve on the corners
|
|
func (b *Border) CornerRadius(rad float32) *Border {
|
|
b.cornerRadius = unit.Dp(float32(b.Theme.TextSize) * rad)
|
|
return b
|
|
}
|
|
|
|
// Width sets the width of the border line
|
|
func (b *Border) Width(width float32) *Border {
|
|
b.width = unit.Dp(float32(b.Theme.TextSize) * width)
|
|
return b
|
|
}
|
|
|
|
func (b *Border) Embed(w l.Widget) *Border {
|
|
b.w = w
|
|
return b
|
|
}
|
|
|
|
// Fn renders the border
|
|
func (b *Border) Fn(gtx l.Context) l.Dimensions {
|
|
dims := b.w(gtx)
|
|
|
|
rr := gtx.Dp(b.cornerRadius)
|
|
width := float32(gtx.Dp(b.width))
|
|
widthInt := int(width)
|
|
|
|
// Create rectangle inset by half the stroke width
|
|
r := image.Rectangle{
|
|
Min: image.Pt(widthInt/2, widthInt/2),
|
|
Max: image.Pt(dims.Size.X-widthInt/2, dims.Size.Y-widthInt/2),
|
|
}
|
|
|
|
paint.FillShape(gtx.Ops,
|
|
b.color,
|
|
clip.Stroke{
|
|
Path: clip.UniformRRect(r, rr).Path(gtx.Ops),
|
|
Width: width,
|
|
}.Op(),
|
|
)
|
|
|
|
return dims
|
|
}
|