/
border.go
49 lines (40 loc) · 921 Bytes
/
border.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cryptomaterial
import (
"image"
"image/color"
"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 {
Color color.NRGBA
Radius CornerRadius
Width unit.Dp
}
func (b Border) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
dims := w(gtx)
sz := dims.Size
width := gtx.Dp(b.Width)
sz.X -= width
sz.Y -= width
r := image.Rectangle{Max: sz}
r = r.Add(image.Point{X: width / 2, Y: width / 2})
tr := gtx.Dp(unit.Dp(b.Radius.TopRight))
tl := gtx.Dp(unit.Dp(b.Radius.TopLeft))
br := gtx.Dp(unit.Dp(b.Radius.BottomRight))
bl := gtx.Dp(unit.Dp(b.Radius.BottomLeft))
radius := clip.RRect{
Rect: r,
NW: tl, NE: tr, SE: br, SW: bl,
}
paint.FillShape(gtx.Ops,
b.Color,
clip.Stroke{
Path: radius.Path(gtx.Ops),
Width: float32(width),
}.Op(),
)
return dims
}