doodle/ui/frame.go
Noah Petherbridge 8624a28ea9 Add StatusBar to Editor Mode, Iterate on UI Toolkit
* Added `BoxSize()` to Widget that reports the full box size including
  borders and margin.
* The Frame uses the `BoxSize()` of widgets to position them.
  Reintroduces some padding issues (boxes on the GUI Test stick out of
  bounds a bit) but is on the right track.
* Renamed `Padding` to `Margin` on the Widget object, since the Margin
  is taken into consideration along with Outline and Border in computing
  the widget's BoxSize.
* Restructured the Label widget to take a Text or TextVariable property
  and the font settings (render.Text) are in a new `Font` property.
2018-08-05 12:54:57 -07:00

77 lines
1.4 KiB
Go

package ui
import (
"fmt"
"git.kirsle.net/apps/doodle/render"
)
// Frame is a widget that contains other widgets.
type Frame struct {
Name string
BaseWidget
packs map[Anchor][]packedWidget
widgets []Widget
}
// NewFrame creates a new Frame.
func NewFrame(name string) *Frame {
w := &Frame{
Name: name,
packs: map[Anchor][]packedWidget{},
widgets: []Widget{},
}
w.IDFunc(func() string {
return fmt.Sprintf("Frame<%s; %d widgets>",
name,
len(w.widgets),
)
})
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[Anchor][]packedWidget{}
}
if w.widgets == nil {
w.widgets = []Widget{}
}
}
// Compute the size of the Frame.
func (w *Frame) Compute(e render.Engine) {
w.computePacked(e)
}
// Present the Frame.
func (w *Frame) Present(e render.Engine, P render.Point) {
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.MoveTo(moveTo)
child.Present(e, moveTo)
}
}