diff --git a/frame.go b/frame.go index 3e8f9ba..c76c5fb 100644 --- a/frame.go +++ b/frame.go @@ -1,6 +1,8 @@ package etk -import "image" +import ( + "image" +) // Frame is a widget container. All children are displayed at once. Children are // not repositioned by default. Repositioning may be enabled via SetPositionChildren. @@ -10,6 +12,8 @@ type Frame struct { positionChildren bool maxWidth int maxHeight int + horizontal Alignment + vertical Alignment } // NewFrame returns a new Frame widget. @@ -69,6 +73,24 @@ func (f *Frame) SetMaxHeight(h int) { f.repositionAll() } +// SetHorizontal sets the horizontal alignment of widgets within the Frame. This +// will only have an effect when a max width is set and there is extra space. +func (f *Frame) SetHorizontal(h Alignment) { + f.Lock() + defer f.Unlock() + + f.horizontal = h +} + +// SetVertical sets the vertical alignment ofwidgets within the Frame. This +// will only have an effect when a max height is set and there is extra space. +func (f *Frame) SetVertical(v Alignment) { + f.Lock() + defer f.Unlock() + + f.vertical = v +} + // AddChild adds a child to the widget. func (f *Frame) AddChild(w ...Widget) { f.Lock() @@ -85,13 +107,32 @@ func (f *Frame) AddChild(w ...Widget) { } func (f *Frame) repositionChild(w Widget) { - r := f.rect.Inset(f.padding) - if f.maxWidth > 0 && r.Dx() > f.maxWidth { - r.Max.X = r.Min.X + f.maxWidth + r := f.rect + if f.maxWidth > 0 { + if r.Dx() > f.maxWidth { + r.Max.X = r.Min.X + f.maxWidth + } + if f.horizontal == AlignCenter { + extraSpace := f.rect.Dx() - r.Dx() + if extraSpace > 0 { + r = image.Rectangle{image.Point{f.rect.Min.X + extraSpace/2, r.Min.Y}, image.Point{f.rect.Max.X - extraSpace/2, r.Max.Y}} + } + } + // TODO Handle AlignEnd } - if f.maxHeight > 0 && r.Dy() > f.maxHeight { - r.Max.Y = r.Min.Y + f.maxHeight + if f.maxHeight > 0 { + if r.Dy() > f.maxHeight { + r.Max.Y = r.Min.Y + f.maxHeight + } + if f.vertical == AlignCenter { + extraSpace := f.rect.Dy() - r.Dy() + if extraSpace > 0 { + r = image.Rectangle{image.Point{r.Min.X, f.rect.Min.Y + extraSpace/2}, image.Point{r.Max.X, f.rect.Max.Y - extraSpace/2}} + } + } + // TODO Handle AlignEnd } + r = r.Inset(f.padding) w.SetRect(r) }