zntg/ui/margin.go

51 lines
1.1 KiB
Go

package ui
import "opslag.de/schobers/geom"
type margin struct {
Wrapper
Left, Top, Right, Bottom float32
}
func Margin(c Control, m float32) Control {
return &margin{Wrap(c), m, m, m, m}
}
func LeftMargin(c Control, m float32) Control {
return &margin{Wrap(c), m, 0, 0, 0}
}
func TopMargin(c Control, m float32) Control {
return &margin{Wrap(c), 0, m, 0, 0}
}
func HorizontalMargin(c Control, m float32) Control {
return &margin{Wrap(c), m, 0, m, 0}
}
func VerticalMargin(c Control, m float32) Control {
return &margin{Wrap(c), 0, m, 0, m}
}
func (m *margin) DesiredSize(ctx Context) geom.PointF32 {
var sz = m.Wrapper.DesiredSize(ctx)
return geom.PtF32(sz.X+m.Left+m.Right, sz.Y+m.Top+m.Bottom)
}
func (m *margin) Arrange(ctx Context, rect geom.RectangleF32) {
m.Wrapper.SetRect(rect)
rect.Min.X += m.Left
rect.Min.Y += m.Top
rect.Max.X -= m.Right
rect.Max.Y -= m.Bottom
if rect.Min.X > rect.Max.X {
var x = .5 * (rect.Min.X + rect.Max.X)
rect.Min.X, rect.Max.X = x, x
}
if rect.Min.Y > rect.Max.Y {
var y = .5 * (rect.Min.Y + rect.Max.Y)
rect.Min.Y, rect.Max.Y = y, y
}
Arrange(ctx, m.Wrapped, rect)
}