2018-09-09 19:07:53 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import "opslag.de/schobers/geom"
|
|
|
|
|
|
|
|
type margin struct {
|
|
|
|
Wrapper
|
2018-09-23 07:42:23 +00:00
|
|
|
Left, Top, Right, Bottom float32
|
2018-09-09 19:07:53 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 07:42:23 +00:00
|
|
|
func Margin(c Control, m float32) Control {
|
2018-09-09 19:07:53 +00:00
|
|
|
return &margin{Wrap(c), m, m, m, m}
|
|
|
|
}
|
|
|
|
|
2018-09-23 07:42:23 +00:00
|
|
|
func LeftMargin(c Control, m float32) Control {
|
2018-09-09 19:07:53 +00:00
|
|
|
return &margin{Wrap(c), m, 0, 0, 0}
|
|
|
|
}
|
|
|
|
|
2018-09-23 07:42:23 +00:00
|
|
|
func TopMargin(c Control, m float32) Control {
|
2018-09-09 19:07:53 +00:00
|
|
|
return &margin{Wrap(c), 0, m, 0, 0}
|
|
|
|
}
|
|
|
|
|
2018-09-23 07:42:23 +00:00
|
|
|
func HorizontalMargin(c Control, m float32) Control {
|
2018-09-09 19:07:53 +00:00
|
|
|
return &margin{Wrap(c), m, 0, m, 0}
|
|
|
|
}
|
|
|
|
|
2018-09-23 07:42:23 +00:00
|
|
|
func VerticalMargin(c Control, m float32) Control {
|
2018-09-09 19:07:53 +00:00
|
|
|
return &margin{Wrap(c), 0, m, 0, m}
|
|
|
|
}
|
|
|
|
|
2018-09-23 07:42:23 +00:00
|
|
|
func (m *margin) DesiredSize(ctx Context) geom.PointF32 {
|
2018-09-09 19:07:53 +00:00
|
|
|
var sz = m.Wrapper.DesiredSize(ctx)
|
2018-09-23 07:42:23 +00:00
|
|
|
return geom.PtF32(sz.X+m.Left+m.Right, sz.Y+m.Top+m.Bottom)
|
2018-09-09 19:07:53 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 07:42:23 +00:00
|
|
|
func (m *margin) Arrange(ctx Context, rect geom.RectangleF32) {
|
2018-09-09 19:07:53 +00:00
|
|
|
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)
|
|
|
|
}
|