From 2a0dbfc74a5def29d6902dcb6dc0de9e6884d032 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 26 Jun 2014 14:22:49 -0400 Subject: [PATCH] Added margins and padding to Grid and a test program to make sure they're the same... --- grid.go | 12 ++++++++--- test/main.go | 4 ++++ test/spacing.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 test/spacing.go diff --git a/grid.go b/grid.go index 8a42bcf..b1c2a52 100644 --- a/grid.go +++ b/grid.go @@ -134,16 +134,19 @@ func (g *Grid) allocate(x int, y int, width int, height int, d *sysSizeData) (al var current *allocation // for neighboring + // TODO return if nControls == 0? // before we do anything, steal the margin so nested Stacks/Grids don't double down xmargin := d.xmargin ymargin := d.ymargin d.xmargin = 0 d.ymargin = 0 - // 0) inset the available rect by the margins + // 0) inset the available rect by the margins and needed padding x += xmargin y += ymargin width -= xmargin * 2 height -= ymargin * 2 + width -= (len(g.colwidths) - 1) * d.xpadding + height -= (len(g.rowheights) - 1) * d.ypadding // 1) clear data structures for i := range g.rowheights { g.rowheights[i] = 0 @@ -197,15 +200,16 @@ func (g *Grid) allocate(x int, y int, width int, height int, d *sysSizeData) (al current = nil // spaces don't have allocation data } allocations = append(allocations, as...) - x += g.colwidths[col] + x += g.colwidths[col] + d.xpadding } x = startx - y += g.rowheights[row] + y += g.rowheights[row] + d.ypadding } return } // filling and stretchy are ignored for preferred size calculation +// We don't consider the margins here, but will need to if Window.SizeToFit() is ever made a thing. func (g *Grid) preferredSize(d *sysSizeData) (width int, height int) { max := func(a int, b int) int { if a > b { @@ -214,6 +218,8 @@ func (g *Grid) preferredSize(d *sysSizeData) (width int, height int) { return b } + width -= (len(g.colwidths) - 1) * d.xpadding + height -= (len(g.rowheights) - 1) * d.ypadding // 1) clear data structures for i := range g.rowheights { g.rowheights[i] = 0 diff --git a/test/main.go b/test/main.go index b65a799..68f3463 100644 --- a/test/main.go +++ b/test/main.go @@ -280,6 +280,10 @@ var labelAlignTest = flag.Bool("label", false, "show Label Alignment test window var spacingTest = flag.Bool("spacing", false, "margins and padding on Window") func myMain() { + if *spacetest != "" { + spaceTest() + return + } if *doArea { areaTest() return diff --git a/test/spacing.go b/test/spacing.go new file mode 100644 index 0000000..367b0be --- /dev/null +++ b/test/spacing.go @@ -0,0 +1,54 @@ +// 26 june 2014 +package main + +import ( + "flag" + "image" + "image/color" + "image/draw" + . "github.com/andlabs/ui" +) + +// spacing test + +type solidColor struct { + c color.Color +} +func (s solidColor) Paint(r image.Rectangle) *image.RGBA { + i := image.NewRGBA(r) + draw.Draw(i, r, &image.Uniform{s.c}, image.ZP, draw.Src) + return i +} +func (s solidColor) Mouse(m MouseEvent) bool { return false } +func (s solidColor) Key(e KeyEvent) bool { return false } + +var spacetest = flag.String("spacetest", "", "test space idempotency; arg is x or y; overrides -area") +func spaceTest() { + w := 100 + h := 50 + ng := 1 + gsx, gsy := 1, 0 + f := NewVerticalStack + if *spacetest == "x" { + w = 50 + h = 100 + ng = 2 + gsx, gsy = 0, 1 + f = NewHorizontalStack + } + ah := solidColor{color.NRGBA{0,0,255,255}} + a1 := NewArea(w, h, ah) + a2 := NewArea(w, h, ah) + a3 := NewArea(w, h, ah) + a4 := NewArea(w, h, ah) + win := NewWindow("Stack", 250, 250) + win.SetSpaced(true) + win.Open(f(a1, a2)) + win = NewWindow("Grid", 250, 250) + win.SetSpaced(true) + g := NewGrid(ng, a3, a4) + g.SetFilling(0, 0) + g.SetStretchy(gsx, gsy) + win.Open(g) + select {} +}