Implemented Padded in Space.

This commit is contained in:
Pietro Gagliardi 2014-10-17 12:57:39 -04:00
parent d6ae3afeb4
commit bdadfe232d
2 changed files with 55 additions and 16 deletions

View File

@ -24,6 +24,11 @@ type Stack interface {
// SetStretchy marks a control in a Stack as stretchy. // SetStretchy marks a control in a Stack as stretchy.
// It panics if index is out of range. // It panics if index is out of range.
SetStretchy(index int) 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 { type stack struct {
@ -32,6 +37,7 @@ type stack struct {
stretchy []bool stretchy []bool
width, height []int // caches to avoid reallocating these each time width, height []int // caches to avoid reallocating these each time
container *container container *container
padded bool
} }
func newStack(o orientation, controls ...Control) Stack { func newStack(o orientation, controls ...Control) Stack {
@ -67,6 +73,14 @@ func (s *stack) SetStretchy(index int) {
s.stretchy[index] = true 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) { func (s *stack) setParent(parent *controlParent) {
s.container.setParent(parent) 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 if len(s.controls) == 0 { // do nothing if there's nothing to do
return 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 // 0) inset the available rect by the needed padding and reset the x/y coordinates for the children
if s.orientation == horizontal { if s.orientation == horizontal {
width -= (len(s.controls) - 1) * d.xpadding width -= (len(s.controls) - 1) * xpadding
} else { } else {
height -= (len(s.controls) - 1) * d.ypadding height -= (len(s.controls) - 1) * ypadding
} }
x = 0 x = 0
y = 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 { for i, c := range s.controls {
c.resize(x, y, s.width[i], s.height[i], d) c.resize(x, y, s.width[i], s.height[i], d)
if s.orientation == horizontal { if s.orientation == horizontal {
x += s.width[i] + d.xpadding x += s.width[i] + xpadding
} else { } else {
y += s.height[i] + d.ypadding y += s.height[i] + ypadding
} }
} }
return 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 if len(s.controls) == 0 { // no controls, so return emptiness
return 0, 0 return 0, 0
} }
xpadding := d.xpadding
ypadding := d.ypadding
if !s.padded {
xpadding = 0
ypadding = 0
}
if s.orientation == horizontal { if s.orientation == horizontal {
width = (len(s.controls) - 1) * d.xpadding width = (len(s.controls) - 1) * xpadding
} else { } else {
height = (len(s.controls) - 1) * d.ypadding height = (len(s.controls) - 1) * ypadding
} }
for i, c := range s.controls { for i, c := range s.controls {
w, h := c.preferredSize(d) w, h := c.preferredSize(d)

View File

@ -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 smallWindow = flag.Bool("small", false, "open a small window (test Mac OS X initial control sizing)")
var spaced = flag.Bool("spaced", false, "enable spacing") 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 { type dtype struct {
Name string Name string
Address string Address string
@ -119,7 +131,7 @@ func (tw *testwin) addfe() {
OpenFile(tw.w, tw.openFile) OpenFile(tw.w, tw.openFile)
}) })
tw.fnlabel = NewLabel("<no file selected>") tw.fnlabel = NewLabel("<no file selected>")
tw.festack = NewVerticalStack(tw.festart, tw.festack = newVerticalStack(tw.festart,
tw.felabel, tw.felabel,
tw.festop, tw.festop,
NewCheckbox("This is a checkbox test"), NewCheckbox("This is a checkbox test"),
@ -130,7 +142,7 @@ func (tw *testwin) addfe() {
tw.openbtn, tw.fnlabel) tw.openbtn, tw.fnlabel)
tw.festack.SetStretchy(4) tw.festack.SetStretchy(4)
tw.festack.SetStretchy(6) tw.festack.SetStretchy(6)
tw.festack = NewHorizontalStack(tw.festack, Space()) tw.festack = newHorizontalStack(tw.festack, Space())
tw.festack.SetStretchy(0) tw.festack.SetStretchy(0)
tw.festack.SetStretchy(1) tw.festack.SetStretchy(1)
tw.t.Append("Foreign Events", tw.festack) 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("Empty Group", NewGroup("Group", Space()))
tw.t.Append("Filled Group", tw.group2) tw.t.Append("Filled Group", tw.group2)
tw.group2.SetMargined(*spaced) 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.group.SetMargined(*spaced)
tw.t.Append("Group", tw.group) tw.t.Append("Group", tw.group)
tw.simpleGrid = NewSimpleGrid(3, tw.simpleGrid = NewSimpleGrid(3,
@ -193,7 +205,7 @@ func (tw *testwin) make(done chan struct{}) {
tw.t.Append("Space", Space()) tw.t.Append("Space", Space())
tw.a = NewArea(200, 200, &areaHandler{false}) tw.a = NewArea(200, 200, &areaHandler{false})
tw.t.Append("Area", tw.a) tw.t.Append("Area", tw.a)
tw.spw = NewHorizontalStack( tw.spw = newHorizontalStack(
NewButton("hello"), NewButton("hello"),
NewCheckbox("hello"), NewCheckbox("hello"),
NewTextField(), NewTextField(),
@ -201,7 +213,7 @@ func (tw *testwin) make(done chan struct{}) {
NewTable(reflect.TypeOf(struct{ A, B, C int }{})), NewTable(reflect.TypeOf(struct{ A, B, C int }{})),
NewLabel("hello")) NewLabel("hello"))
tw.t.Append("Pref Width", tw.spw) tw.t.Append("Pref Width", tw.spw)
tw.sph = NewVerticalStack( tw.sph = newVerticalStack(
NewButton("hello"), NewButton("hello"),
NewCheckbox("hello"), NewCheckbox("hello"),
NewTextField(), NewTextField(),
@ -209,14 +221,14 @@ func (tw *testwin) make(done chan struct{}) {
NewTable(reflect.TypeOf(struct{ A, B, C int }{})), NewTable(reflect.TypeOf(struct{ A, B, C int }{})),
NewLabel("hello ÉÀÔ")) NewLabel("hello ÉÀÔ"))
tw.t.Append("Pref Height", tw.sph) tw.t.Append("Pref Height", tw.sph)
stack1 := NewHorizontalStack(NewLabel("Test"), NewTextField()) stack1 := newHorizontalStack(NewLabel("Test"), NewTextField())
stack1.SetStretchy(1) stack1.SetStretchy(1)
stack2 := NewHorizontalStack(NewLabel("ÉÀÔ"), NewTextField()) stack2 := newHorizontalStack(NewLabel("ÉÀÔ"), NewTextField())
stack2.SetStretchy(1) stack2.SetStretchy(1)
stack3 := NewHorizontalStack(NewLabel("Test 2"), stack3 := newHorizontalStack(NewLabel("Test 2"),
NewTable(reflect.TypeOf(struct{ A, B, C int }{}))) NewTable(reflect.TypeOf(struct{ A, B, C int }{})))
stack3.SetStretchy(1) stack3.SetStretchy(1)
tw.s = NewVerticalStack(stack1, stack2, stack3) tw.s = newVerticalStack(stack1, stack2, stack3)
tw.s.SetStretchy(2) tw.s.SetStretchy(2)
tw.t.Append("Stack", tw.s) tw.t.Append("Stack", tw.s)
tw.l = NewLabel("hello") tw.l = NewLabel("hello")
@ -252,7 +264,7 @@ func (tw *testwin) make(done chan struct{}) {
tw.w.Show() tw.w.Show()
if *smallWindow { if *smallWindow {
tw.wsmall = NewWindow("Small", 80, 80, tw.wsmall = NewWindow("Small", 80, 80,
NewVerticalStack( newVerticalStack(
NewButton("Small"), NewButton("Small"),
NewButton("Small 2"), NewButton("Small 2"),
NewArea(200, 200, &areaHandler{true}))) NewArea(200, 200, &areaHandler{true})))