Added margins control.
This commit is contained in:
parent
a08f961e80
commit
8d7ec272b6
51
alui/margins.go
Normal file
51
alui/margins.go
Normal 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)
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user