...
1 package etk
2
3 import "image"
4
5
6
7 type Frame struct {
8 *Box
9 padding int
10 positionChildren bool
11 maxWidth int
12 maxHeight int
13 }
14
15
16 func NewFrame(w ...Widget) *Frame {
17 f := &Frame{
18 Box: NewBox(),
19 }
20 f.AddChild(w...)
21 return f
22 }
23
24
25 func (f *Frame) SetPadding(padding int) {
26 f.Lock()
27 defer f.Unlock()
28
29 f.padding = padding
30 f.repositionAll()
31 }
32
33
34 func (f *Frame) SetRect(r image.Rectangle) {
35 f.Lock()
36 defer f.Unlock()
37
38 f.rect = r
39 f.repositionAll()
40 }
41
42
43
44 func (f *Frame) SetPositionChildren(position bool) {
45 f.Lock()
46 defer f.Unlock()
47
48 f.positionChildren = position
49 f.repositionAll()
50 }
51
52
53
54 func (f *Frame) SetMaxWidth(w int) {
55 f.Lock()
56 defer f.Unlock()
57
58 f.maxWidth = w
59 f.repositionAll()
60 }
61
62
63
64 func (f *Frame) SetMaxHeight(h int) {
65 f.Lock()
66 defer f.Unlock()
67
68 f.maxHeight = h
69 f.repositionAll()
70 }
71
72
73 func (f *Frame) AddChild(w ...Widget) {
74 f.Lock()
75 defer f.Unlock()
76
77 f.children = append(f.children, w...)
78
79 if !f.positionChildren {
80 return
81 }
82 for _, child := range w {
83 f.repositionChild(child)
84 }
85 }
86
87 func (f *Frame) repositionChild(w Widget) {
88 r := f.rect.Inset(f.padding)
89 if f.maxWidth > 0 && r.Dx() > f.maxWidth {
90 r.Max.X = r.Min.X + f.maxWidth
91 }
92 if f.maxHeight > 0 && r.Dy() > f.maxHeight {
93 r.Max.Y = r.Min.Y + f.maxHeight
94 }
95 w.SetRect(r)
96 }
97
98 func (f *Frame) repositionAll() {
99 if !f.positionChildren {
100 return
101 }
102 for _, w := range f.children {
103 f.repositionChild(w)
104 }
105 }
106
View as plain text