allg5/alui/margins.go

54 lines
1.4 KiB
Go
Raw Normal View History

2020-03-08 09:10:41 +00:00
package alui
import (
"opslag.de/schobers/geom"
)
var _ Control = &Margins{}
type Margins struct {
Proxy
Top, Left, Bottom, Right float32
bounds geom.RectangleF32
}
func NewMargins(target Control, margins ...float32) *Margins {
m := &Margins{Proxy: Proxy{Target: target}}
switch len(margins) {
case 1:
m.Top, m.Left, m.Bottom, m.Right = margins[0], margins[0], margins[0], margins[0]
case 2:
m.Top, m.Left, m.Bottom, m.Right = margins[0], margins[1], margins[0], margins[1]
case 3:
m.Top, m.Left, m.Bottom, m.Right = margins[0], margins[1], margins[2], margins[1]
case 4:
m.Top, m.Left, m.Bottom, m.Right = margins[0], margins[1], margins[2], margins[3]
default:
panic("expected 1 (all same), 2 (vertical, horizontal), 3 (top, horizontal, bottom) or 4 margins (all separately specified)")
}
return m
}
func (m *Margins) Bounds() geom.RectangleF32 { return m.bounds }
func (m *Margins) DesiredSize(ctx *Context) geom.PointF32 {
return m.Proxy.DesiredSize(ctx).Add2D(m.Left+m.Right, m.Top+m.Bottom)
}
func (m *Margins) inset(bounds geom.RectangleF32) geom.RectangleF32 {
return geom.RectF32(bounds.Min.X+m.Left, bounds.Min.Y+m.Top, bounds.Max.X-m.Right, bounds.Max.Y-m.Bottom)
}
func (m *Margins) Layout(ctx *Context, bounds geom.RectangleF32) {
m.bounds = bounds
target := m.inset(bounds)
m.Proxy.Layout(ctx, target)
}
func (m *Margins) Render(ctx *Context, bounds geom.RectangleF32) {
target := m.inset(bounds)
m.Proxy.Render(ctx, target)
}