From 8d7ec272b66d28c1d4689d7cb4834f6d00eb111b Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sun, 29 Dec 2019 09:53:03 +0100 Subject: [PATCH] Added margins control. --- alui/margins.go | 51 +++++++++++++++++++++++++++++++++ alui/proxy.go | 12 ++++---- cmd/krampus19/changesettings.go | 17 +---------- 3 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 alui/margins.go diff --git a/alui/margins.go b/alui/margins.go new file mode 100644 index 0000000..8fd1ad7 --- /dev/null +++ b/alui/margins.go @@ -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) +} diff --git a/alui/proxy.go b/alui/proxy.go index dd7f338..aa5f98c 100644 --- a/alui/proxy.go +++ b/alui/proxy.go @@ -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) + } +} diff --git a/cmd/krampus19/changesettings.go b/cmd/krampus19/changesettings.go index 2944a18..7645c22 100644 --- a/cmd/krampus19/changesettings.go +++ b/cmd/krampus19/changesettings.go @@ -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 {