zntg/ui/columns.go

113 lines
2.0 KiB
Go

package ui
import (
"opslag.de/schobers/geom"
)
type columns struct {
ContainerBase
cols []*column
col int
}
type column struct {
p DockPanel
}
func (c *column) Append(ctrl Control) {
c.p.Append(DockTop, ctrl)
}
func (c *column) AppendCreate(ctx Context, ctrl Control) {
c.p.AppendCreate(ctx, DockTop, ctrl)
}
type Columns interface {
Container
Columns() []Column
Append(Control)
AppendCreate(Context, Control)
}
type Column interface {
Append(Control)
AppendCreate(Context, Control)
}
func NewColumns(cols int, ctrls ...Control) Columns {
var c = &columns{}
for i := 0; i < cols; i++ {
c.addColumn()
}
for _, ctrl := range ctrls {
c.Append(ctrl)
}
return c
}
func (c *columns) addColumn() {
var p = NewDockPanel(c)
c.ContainerBase.Append(p)
c.cols = append(c.cols, &column{p})
}
func (c *columns) next() {
c.col = (c.col + 1) % len(c.cols)
}
func (c *columns) Columns() []Column {
var cols = make([]Column, len(c.cols))
for i, col := range c.cols {
cols[i] = col
}
return cols
}
func (c *columns) Append(ctrl Control) {
c.cols[c.col].Append(ctrl)
c.next()
}
func (c *columns) AppendCreate(ctx Context, ctrl Control) {
c.cols[c.col].AppendCreate(ctx, ctrl)
c.next()
}
func (c *columns) DesiredSize(ctx Context) geom.PointF32 {
var w float32
var h float32
for _, col := range c.cols {
var sz = col.p.DesiredSize(ctx)
if !geom.IsNaN32(w) {
if geom.IsNaN32(sz.X) {
w = sz.X
} else {
w += sz.X
}
}
if !geom.IsNaN32(h) {
if geom.IsNaN32(sz.Y) {
h = sz.Y
} else {
h = geom.Max32(h, sz.Y)
}
}
}
return geom.PtF32(w, h)
}
func (c *columns) Arrange(ctx Context, rect geom.RectangleF32) {
c.ContainerBase.SetRect(rect)
var w, h = rect.Dx(), rect.Dy()
var cols = float32(len(c.cols))
for i, col := range c.cols {
var ii = float32(i)
var colH = col.p.DesiredSize(ctx).Y
if colH > h {
colH = h
}
var colR = geom.RectF32(rect.Min.X+ii*w/cols, rect.Min.Y, rect.Min.X+(ii+1)*w/cols, rect.Min.Y+colH)
Arrange(ctx, col.p, colR)
}
}