Integrated the new sizing system into Grid and Stack.

This commit is contained in:
Pietro Gagliardi 2014-06-25 22:43:35 -04:00
parent 08922103a7
commit 92afc9b944
3 changed files with 53 additions and 26 deletions

View File

@ -38,17 +38,3 @@ type controlSizing interface {
commitResize(c *allocation, d *sysSizeData)
getAuxResizeInfo(d *sysSizeData)
}
// vertical stack: no concept of neighbor, but not too hard to add a vertical neighbor
// horizontal stack:
var current *allocation
// ...
as := s.controls[i].allocate(...)
if current != nil {
current.neighbor = as[0].self
}
current = as[0]
// append all of as
// grid:
// same as above, except current is set to nil on each new row
// adding a vertical neighbor would require storing an extra list

30
grid.go
View File

@ -124,7 +124,7 @@ func (g *Grid) make(window *sysData) error {
return nil
}
func (g *Grid) setRect(x int, y int, width int, height int, rr *[]resizerequest) {
func (g *Grid) setRect(x int, y int, width int, height int, d *sysSizeData) (allocations []*allocation) {
max := func(a int, b int) int {
if a > b {
return a
@ -132,6 +132,12 @@ func (g *Grid) setRect(x int, y int, width int, height int, rr *[]resizerequest)
return b
}
var current *allocation // for neighboring
// before we do anything, steal the margin so nested Stacks/Grids don't double down
margin := d.margin
d.margin = 0
_=margin
// 1) clear data structures
for i := range g.rowheights {
g.rowheights[i] = 0
@ -142,7 +148,7 @@ func (g *Grid) setRect(x int, y int, width int, height int, rr *[]resizerequest)
// 2) get preferred sizes; compute row/column sizes
for row, xcol := range g.controls {
for col, c := range xcol {
w, h := c.preferredSize()
w, h := c.preferredSize(d)
g.widths[row][col] = w
g.heights[row][col] = h
g.rowheights[row] = max(g.rowheights[row], h)
@ -167,6 +173,7 @@ func (g *Grid) setRect(x int, y int, width int, height int, rr *[]resizerequest)
// 4) draw
startx := x
for row, xcol := range g.controls {
current = nil // reset on new columns
for col, c := range xcol {
w := g.widths[row][col]
h := g.heights[row][col]
@ -174,7 +181,12 @@ func (g *Grid) setRect(x int, y int, width int, height int, rr *[]resizerequest)
w = g.colwidths[col]
h = g.rowheights[row]
}
c.setRect(x, y, w, h, rr)
as := c.allocation(x, y, w, h, d)
if current != nil { // connect first left to first right
current.neighbor = c
}
current = as[0] // next left is first subwidget
allocations = append(allocations, as...)
x += g.colwidths[col]
}
x = startx
@ -184,7 +196,7 @@ func (g *Grid) setRect(x int, y int, width int, height int, rr *[]resizerequest)
}
// filling and stretchy are ignored for preferred size calculation
func (g *Grid) preferredSize() (width int, height int) {
func (g *Grid) preferredSize(d *sysSizeData) (width int, height int) {
max := func(a int, b int) int {
if a > b {
return a
@ -202,7 +214,7 @@ func (g *Grid) preferredSize() (width int, height int) {
// 2) get preferred sizes; compute row/column sizes
for row, xcol := range g.controls {
for col, c := range xcol {
w, h := c.preferredSize()
w, h := c.preferredSize(d)
g.widths[row][col] = w
g.heights[row][col] = h
g.rowheights[row] = max(g.rowheights[row], h)
@ -218,3 +230,11 @@ func (g *Grid) preferredSize() (width int, height int) {
}
return width, height
}
func (g *Grid) commitResize(c *allocation, d *sysSizeData) {
// this is to satisfy Control; nothing to do here
}
func (g *Grid) getAuxResizeInfo(d *sysSizeData) {
// this is to satisfy Control; nothing to do here
}

View File

@ -77,12 +77,17 @@ func (s *Stack) make(window *sysData) error {
return nil
}
func (s *Stack) setRect(x int, y int, width int, height int, rr *[]resizerequest) {
func (s *Stack) allocate(x int, y int, width int, height int, d *sysSizeData) (allocations []*allocation) {
var stretchywid, stretchyht int
var current *allocation // for neighboring
if len(s.controls) == 0 { // do nothing if there's nothing to do
return
return nil
}
// before we do anything, steal the margin so nested Stacks/Grids don't double down
margin := d.margin
d.margin = 0
_=margin
// 1) get height and width of non-stretchy controls; figure out how much space is alloted to stretchy controls
stretchywid = width
stretchyht = height
@ -92,7 +97,7 @@ func (s *Stack) setRect(x int, y int, width int, height int, rr *[]resizerequest
nStretchy++
continue
}
w, h := c.preferredSize()
w, h := c.preferredSize(d)
if s.orientation == horizontal { // all controls have same height
s.width[i] = w
s.height[i] = height
@ -120,18 +125,25 @@ func (s *Stack) setRect(x int, y int, width int, height int, rr *[]resizerequest
}
// 3) now actually place controls
for i, c := range s.controls {
c.setRect(x, y, s.width[i], s.height[i], rr)
as := c.allocate(x, y, s.width[i], s.height[i], d)
if s.orientation == horizontal { // no vertical neighbors
if current != nil { // connect first left to first right
current.neighbor = c
}
current = as[0] // next left is first subwidget
}
allocations = append(allocations, as...)
if s.orientation == horizontal {
x += s.width[i]
} else {
y += s.height[i]
}
}
return
return allocations
}
// The preferred size of a Stack is the sum of the preferred sizes of non-stretchy controls + (the number of stretchy controls * the largest preferred size among all stretchy controls).
func (s *Stack) preferredSize() (width int, height int) {
func (s *Stack) preferredSize(d *sysSizeData) (width int, height int) {
max := func(a int, b int) int {
if a > b {
return a
@ -146,7 +158,7 @@ func (s *Stack) preferredSize() (width int, height int) {
return 0, 0
}
for i, c := range s.controls {
w, h := c.preferredSize()
w, h := c.preferredSize(d)
if s.stretchy[i] {
nStretchy++
maxswid = max(maxswid, w)
@ -172,6 +184,15 @@ func (s *Stack) preferredSize() (width int, height int) {
return
}
func (s *Stack) commitResize(c *allocation, d *sysSizeData) {
// this is to satisfy Control; nothing to do here
}
func (s *Stack) getAuxResizeInfo(d *sysSizeData) {
// this is to satisfy Control; nothing to do here
}
// Space returns a null Control intended for padding layouts with blank space.
// It appears to its owner as a Control of 0x0 size.
//