-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame.go
116 lines (98 loc) · 2.38 KB
/
frame.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ui
import (
"errors"
"fmt"
"git.kirsle.net/go/render"
)
// Frame is a widget that contains other widgets.
type Frame struct {
Name string
BaseWidget
// Widget placement settings.
packs map[Side][]*packedWidget // Packed widgets
placed []*placedWidget // Placed widgets
widgets []Widget
}
// NewFrame creates a new Frame.
func NewFrame(name string) *Frame {
w := &Frame{
Name: name,
packs: map[Side][]*packedWidget{},
widgets: []Widget{},
}
w.SetBackground(render.RGBA(1, 0, 0, 0)) // invisible default BG
w.IDFunc(func() string {
return fmt.Sprintf("Frame<%s>",
name,
)
})
return w
}
// Setup ensures all the Frame's data is initialized and not null.
func (w *Frame) Setup() {
if w.packs == nil {
w.packs = map[Side][]*packedWidget{}
}
if w.widgets == nil {
w.widgets = []Widget{}
}
}
// Add a child widget to the frame. When the frame Presents itself, it also
// presents child widgets. This method is safe to call multiple times: it ensures
// the widget is not already a child of the Frame before adding it.
func (w *Frame) Add(child Widget) error {
if child == w {
return errors.New("can't add self to frame")
}
// Ensure child is new to the frame.
for _, widget := range w.widgets {
if widget == child {
return errors.New("widget already added to frame")
}
}
w.widgets = append(w.widgets, child)
return nil
}
// Children returns all of the child widgets.
func (w *Frame) Children() []Widget {
return w.widgets
}
// Compute the size of the Frame.
func (w *Frame) Compute(e render.Engine) {
w.computePacked(e)
w.computePlaced(e)
// Call the BaseWidget Compute in case we have subscribers.
w.BaseWidget.Compute(e)
}
// Present the Frame.
func (w *Frame) Present(e render.Engine, P render.Point) {
if w.Hidden() {
return
}
var (
S = w.Size()
)
// Draw the widget's border and everything.
w.DrawBox(e, P)
// Draw the background color.
e.DrawBox(w.Background(), render.Rect{
X: P.X + w.BoxThickness(1),
Y: P.Y + w.BoxThickness(1),
W: S.W - w.BoxThickness(2),
H: S.H - w.BoxThickness(2),
})
// Draw the widgets.
for _, child := range w.widgets {
if child.Hidden() {
continue
}
p := child.Point()
moveTo := render.NewPoint(
P.X+p.X+w.BoxThickness(1),
P.Y+p.Y+w.BoxThickness(1),
)
child.Present(e, moveTo)
}
// Call the BaseWidget Present in case we have subscribers.
w.BaseWidget.Present(e, P)
}