Added margins control.

This commit is contained in:
Sander Schobers 2019-12-29 09:53:03 +01:00
parent a08f961e80
commit 8d7ec272b6
3 changed files with 58 additions and 22 deletions

51
alui/margins.go Normal file
View File

@ -0,0 +1,51 @@
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 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) 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)
}

View File

@ -31,14 +31,14 @@ func (p *Proxy) Handle(e allg5.Event) {
}
}
func (p *Proxy) Render(ctx *Context, bounds geom.RectangleF32) {
if p.Target != nil {
p.Target.Render(ctx, bounds)
}
}
func (p *Proxy) Layout(ctx *Context, bounds geom.RectangleF32) {
if p.Target != nil {
p.Target.Layout(ctx, bounds)
}
}
func (p *Proxy) Render(ctx *Context, bounds geom.RectangleF32) {
if p.Target != nil {
p.Target.Render(ctx, bounds)
}
}

View File

@ -181,22 +181,7 @@ type settingsHeader struct {
func newSettingsHeader(label string) alui.Control {
header := &settingsHeader{}
header.Text = label
return header
}
func (h *settingsHeader) DesiredSize(ctx *alui.Context) geom.PointF32 {
size := h.Label.DesiredSize(ctx)
size.Y += 5 * margin
return size
}
func (h *settingsHeader) Layout(ctx *alui.Context, bounds geom.RectangleF32) {
var label = bounds
if label.Dy() > 5*margin {
label.Min.Y = bounds.Min.Y + 3*margin
label.Max.Y = bounds.Max.Y - 2*margin
}
h.Label.Layout(ctx, label)
return alui.NewMargins(header, 3*margin, 0, 2*margin, 0)
}
type settingsRow struct {