border.go raw
1 package gel
2
3 import (
4 "image/color"
5
6 "github.com/p9c/gio/f32"
7 l "github.com/p9c/gio/layout"
8 "github.com/p9c/gio/op/clip"
9 "github.com/p9c/gio/op/paint"
10 "github.com/p9c/gio/unit"
11 )
12
13 // Border lays out a widget and draws a border inside it.
14 type Border struct {
15 *Window
16 color color.NRGBA
17 cornerRadius unit.Value
18 width unit.Value
19 w l.Widget
20 }
21
22 // Border creates a border with configurable color, width and corner radius.
23 func (w *Window) Border() *Border {
24 b := &Border{Window: w}
25 b.CornerRadius(0.25).Color("Primary").Width(0.125)
26 return b
27 }
28
29 // Color sets the color to render the border in
30 func (b *Border) Color(color string) *Border {
31 b.color = b.Theme.Colors.GetNRGBAFromName(color)
32 return b
33 }
34
35 // CornerRadius sets the radius of the curve on the corners
36 func (b *Border) CornerRadius(rad float32) *Border {
37 b.cornerRadius = b.Theme.TextSize.Scale(rad)
38 return b
39 }
40
41 // Width sets the width of the border line
42 func (b *Border) Width(width float32) *Border {
43 b.width = b.Theme.TextSize.Scale(width)
44 return b
45 }
46
47 func (b *Border) Embed(w l.Widget) *Border {
48 b.w = w
49 return b
50 }
51
52 // Fn renders the border
53 func (b *Border) Fn(gtx l.Context) l.Dimensions {
54 dims := b.w(gtx)
55 sz := l.FPt(dims.Size)
56
57 rr := float32(gtx.Px(b.cornerRadius))
58 width := float32(gtx.Px(b.width))
59 sz.X -= width
60 sz.Y -= width
61
62 r := f32.Rectangle{Max: sz}
63 r = r.Add(f32.Point{X: width * 0.5, Y: width * 0.5})
64
65 paint.FillShape(gtx.Ops,
66 b.color,
67 clip.Stroke{
68 Path: clip.UniformRRect(r, rr).Path(gtx.Ops),
69 Style: clip.StrokeStyle{Width: width},
70 }.Op(),
71 )
72
73 return dims
74 }
75