diff --git a/newctrl/stack.go b/newctrl/stack.go index 7de2fbb..164d663 100644 --- a/newctrl/stack.go +++ b/newctrl/stack.go @@ -24,6 +24,11 @@ type Stack interface { // SetStretchy marks a control in a Stack as stretchy. // It panics if index is out of range. SetStretchy(index int) + + // Padded and SetPadded get and set whether the controls of the Stack have padding between them. + // The size of the padding is platform-dependent. + Padded() bool + SetPadded(padded bool) } type stack struct { @@ -32,6 +37,7 @@ type stack struct { stretchy []bool width, height []int // caches to avoid reallocating these each time container *container + padded bool } func newStack(o orientation, controls ...Control) Stack { @@ -67,6 +73,14 @@ func (s *stack) SetStretchy(index int) { s.stretchy[index] = true } +func (s *stack) Padded() bool { + return s.padded +} + +func (s *stack) SetPadded(padded bool) { + s.padded = padded +} + func (s *stack) setParent(parent *controlParent) { s.container.setParent(parent) } @@ -78,11 +92,18 @@ func (s *stack) resize(x int, y int, width int, height int, d *sizing) { if len(s.controls) == 0 { // do nothing if there's nothing to do return } + // -1) get this Stack's padding + xpadding := d.xpadding + ypadding := d.ypadding + if !s.padded { + xpadding = 0 + ypadding = 0 + } // 0) inset the available rect by the needed padding and reset the x/y coordinates for the children if s.orientation == horizontal { - width -= (len(s.controls) - 1) * d.xpadding + width -= (len(s.controls) - 1) * xpadding } else { - height -= (len(s.controls) - 1) * d.ypadding + height -= (len(s.controls) - 1) * ypadding } x = 0 y = 0 @@ -125,9 +146,9 @@ func (s *stack) resize(x int, y int, width int, height int, d *sizing) { for i, c := range s.controls { c.resize(x, y, s.width[i], s.height[i], d) if s.orientation == horizontal { - x += s.width[i] + d.xpadding + x += s.width[i] + xpadding } else { - y += s.height[i] + d.ypadding + y += s.height[i] + ypadding } } return @@ -148,10 +169,16 @@ func (s *stack) preferredSize(d *sizing) (width int, height int) { if len(s.controls) == 0 { // no controls, so return emptiness return 0, 0 } + xpadding := d.xpadding + ypadding := d.ypadding + if !s.padded { + xpadding = 0 + ypadding = 0 + } if s.orientation == horizontal { - width = (len(s.controls) - 1) * d.xpadding + width = (len(s.controls) - 1) * xpadding } else { - height = (len(s.controls) - 1) * d.ypadding + height = (len(s.controls) - 1) * ypadding } for i, c := range s.controls { w, h := c.preferredSize(d) diff --git a/newctrl/zz_test.go b/newctrl/zz_test.go index cb8bf60..0097745 100644 --- a/newctrl/zz_test.go +++ b/newctrl/zz_test.go @@ -20,6 +20,18 @@ var closeOnClick = flag.Bool("close", false, "close on click") var smallWindow = flag.Bool("small", false, "open a small window (test Mac OS X initial control sizing)") var spaced = flag.Bool("spaced", false, "enable spacing") +func newHorizontalStack(c ...Control) Stack { + s := NewHorizontalStack(c...) + s.SetPadded(*spaced) + return s +} + +func newVerticalStack(c ...Control) Stack { + s := NewVerticalStack(c...) + s.SetPadded(*spaced) + return s +} + type dtype struct { Name string Address string @@ -119,7 +131,7 @@ func (tw *testwin) addfe() { OpenFile(tw.w, tw.openFile) }) tw.fnlabel = NewLabel("") - tw.festack = NewVerticalStack(tw.festart, + tw.festack = newVerticalStack(tw.festart, tw.felabel, tw.festop, NewCheckbox("This is a checkbox test"), @@ -130,7 +142,7 @@ func (tw *testwin) addfe() { tw.openbtn, tw.fnlabel) tw.festack.SetStretchy(4) tw.festack.SetStretchy(6) - tw.festack = NewHorizontalStack(tw.festack, Space()) + tw.festack = newHorizontalStack(tw.festack, Space()) tw.festack.SetStretchy(0) tw.festack.SetStretchy(1) tw.t.Append("Foreign Events", tw.festack) @@ -174,7 +186,7 @@ func (tw *testwin) make(done chan struct{}) { tw.t.Append("Empty Group", NewGroup("Group", Space())) tw.t.Append("Filled Group", tw.group2) tw.group2.SetMargined(*spaced) - tw.group = NewGroup("Group", NewVerticalStack(NewCheckbox("Checkbox in Group"))) + tw.group = NewGroup("Group", newVerticalStack(NewCheckbox("Checkbox in Group"))) tw.group.SetMargined(*spaced) tw.t.Append("Group", tw.group) tw.simpleGrid = NewSimpleGrid(3, @@ -193,7 +205,7 @@ func (tw *testwin) make(done chan struct{}) { tw.t.Append("Space", Space()) tw.a = NewArea(200, 200, &areaHandler{false}) tw.t.Append("Area", tw.a) - tw.spw = NewHorizontalStack( + tw.spw = newHorizontalStack( NewButton("hello"), NewCheckbox("hello"), NewTextField(), @@ -201,7 +213,7 @@ func (tw *testwin) make(done chan struct{}) { NewTable(reflect.TypeOf(struct{ A, B, C int }{})), NewLabel("hello")) tw.t.Append("Pref Width", tw.spw) - tw.sph = NewVerticalStack( + tw.sph = newVerticalStack( NewButton("hello"), NewCheckbox("hello"), NewTextField(), @@ -209,14 +221,14 @@ func (tw *testwin) make(done chan struct{}) { NewTable(reflect.TypeOf(struct{ A, B, C int }{})), NewLabel("hello ÉÀÔ")) tw.t.Append("Pref Height", tw.sph) - stack1 := NewHorizontalStack(NewLabel("Test"), NewTextField()) + stack1 := newHorizontalStack(NewLabel("Test"), NewTextField()) stack1.SetStretchy(1) - stack2 := NewHorizontalStack(NewLabel("ÉÀÔ"), NewTextField()) + stack2 := newHorizontalStack(NewLabel("ÉÀÔ"), NewTextField()) stack2.SetStretchy(1) - stack3 := NewHorizontalStack(NewLabel("Test 2"), + stack3 := newHorizontalStack(NewLabel("Test 2"), NewTable(reflect.TypeOf(struct{ A, B, C int }{}))) stack3.SetStretchy(1) - tw.s = NewVerticalStack(stack1, stack2, stack3) + tw.s = newVerticalStack(stack1, stack2, stack3) tw.s.SetStretchy(2) tw.t.Append("Stack", tw.s) tw.l = NewLabel("hello") @@ -252,7 +264,7 @@ func (tw *testwin) make(done chan struct{}) { tw.w.Show() if *smallWindow { tw.wsmall = NewWindow("Small", 80, 80, - NewVerticalStack( + newVerticalStack( NewButton("Small"), NewButton("Small 2"), NewArea(200, 200, &areaHandler{true})))