doodle/ui/frame.go

76 lines
1.3 KiB
Go
Raw Normal View History

2018-08-01 00:18:13 +00:00
package ui
import (
"fmt"
"git.kirsle.net/apps/doodle/render"
)
// Frame is a widget that contains other widgets.
type Frame struct {
Name string
2018-08-01 00:18:13 +00:00
BaseWidget
packs map[Anchor][]packedWidget
widgets []Widget
}
// NewFrame creates a new Frame.
func NewFrame(name string) *Frame {
w := &Frame{
Name: name,
2018-08-01 00:18:13 +00:00
packs: map[Anchor][]packedWidget{},
widgets: []Widget{},
}
w.IDFunc(func() string {
return fmt.Sprintf("Frame<%s; %d widgets>",
name,
len(w.widgets),
)
})
return w
2018-08-01 00:18:13 +00:00
}
// Setup ensures all the Frame's data is initialized and not null.
func (w *Frame) Setup() {
if w.packs == nil {
w.packs = map[Anchor][]packedWidget{}
2018-08-01 00:18:13 +00:00
}
if w.widgets == nil {
w.widgets = []Widget{}
2018-08-01 00:18:13 +00:00
}
}
2018-08-01 00:18:13 +00:00
// Compute the size of the Frame.
func (w *Frame) Compute(e render.Engine) {
w.computePacked(e)
2018-08-01 00:18:13 +00:00
}
// Present the Frame.
func (w *Frame) Present(e render.Engine) {
var (
P = w.Point()
S = w.Size()
)
// Draw the widget's border and everything.
w.DrawBox(e)
// 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 {
p := child.Point()
child.MoveTo(render.NewPoint(
P.X+p.X+w.BoxThickness(1),
P.Y+p.Y+w.BoxThickness(1),
))
child.Present(e)
}
}