2019-04-10 00:35:44 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2020-03-10 00:13:33 +00:00
|
|
|
"errors"
|
2019-04-10 00:35:44 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-12-23 02:21:58 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2019-04-10 00:35:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Frame is a widget that contains other widgets.
|
|
|
|
type Frame struct {
|
|
|
|
Name string
|
|
|
|
BaseWidget
|
2020-03-09 05:07:46 +00:00
|
|
|
|
|
|
|
// Widget placement settings.
|
|
|
|
packs map[Side][]packedWidget // Packed widgets
|
|
|
|
placed []placedWidget // Placed widgets
|
2019-04-10 00:35:44 +00:00
|
|
|
widgets []Widget
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFrame creates a new Frame.
|
|
|
|
func NewFrame(name string) *Frame {
|
|
|
|
w := &Frame{
|
|
|
|
Name: name,
|
2019-12-29 05:47:46 +00:00
|
|
|
packs: map[Side][]packedWidget{},
|
2019-04-10 00:35:44 +00:00
|
|
|
widgets: []Widget{},
|
|
|
|
}
|
2019-06-25 21:57:11 +00:00
|
|
|
w.SetBackground(render.RGBA(1, 0, 0, 0)) // invisible default BG
|
2019-04-10 00:35:44 +00:00
|
|
|
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 {
|
2019-12-29 05:47:46 +00:00
|
|
|
w.packs = map[Side][]packedWidget{}
|
2019-04-10 00:35:44 +00:00
|
|
|
}
|
|
|
|
if w.widgets == nil {
|
|
|
|
w.widgets = []Widget{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 00:13:33 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-04-10 00:35:44 +00:00
|
|
|
// 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)
|
2020-03-09 05:07:46 +00:00
|
|
|
w.computePlaced(e)
|
2020-03-10 00:13:33 +00:00
|
|
|
|
|
|
|
// Call the BaseWidget Compute in case we have subscribers.
|
|
|
|
w.BaseWidget.Compute(e)
|
2019-04-10 00:35:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
// child.Compute(e)
|
|
|
|
p := child.Point()
|
|
|
|
moveTo := render.NewPoint(
|
|
|
|
P.X+p.X+w.BoxThickness(1),
|
|
|
|
P.Y+p.Y+w.BoxThickness(1),
|
|
|
|
)
|
|
|
|
child.Present(e, moveTo)
|
|
|
|
}
|
2020-03-10 00:13:33 +00:00
|
|
|
|
|
|
|
// Call the BaseWidget Present in case we have subscribers.
|
|
|
|
w.BaseWidget.Present(e, P)
|
2019-04-10 00:35:44 +00:00
|
|
|
}
|