Wrote the Grid test and found/fixed one error in Grid's position calculations. (If you compare the previous Windows build with this one, you may not notice a difference; this is because of the default preferred size of Labels being too wide).
This commit is contained in:
parent
7ba3c5ac17
commit
c6cafdd402
25
grid.go
25
grid.go
|
@ -2,6 +2,7 @@
|
|||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
@ -13,7 +14,7 @@ type Grid struct {
|
|||
lock sync.Mutex
|
||||
created bool
|
||||
controls [][]Control
|
||||
cwidths, cheights [][]int // caches to avoid reallocating each time
|
||||
widths, heights [][]int // caches to avoid reallocating each time
|
||||
rowheights, colwidths []int
|
||||
}
|
||||
|
||||
|
@ -29,10 +30,10 @@ func NewGrid(nPerRow int, controls ...Control) *Grid {
|
|||
if len(controls) % nPerRow != 0 {
|
||||
panic(fmt.Errorf("incomplete grid given to NewGrid() (not enough controls to evenly divide %d controls into rows of %d controls each)", len(controls), nPerRow))
|
||||
}
|
||||
nRows = len(controls) / nPerRow
|
||||
nRows := len(controls) / nPerRow
|
||||
cc := make([][]Control, nRows)
|
||||
cw := make([][]int, nRows)
|
||||
ch := make([][]int, nRow)
|
||||
ch := make([][]int, nRows)
|
||||
i := 0
|
||||
for row := 0; row < nRows; row++ {
|
||||
cc[row] = make([]Control, nPerRow)
|
||||
|
@ -57,7 +58,7 @@ func (g *Grid) make(window *sysData) error {
|
|||
defer g.lock.Unlock()
|
||||
|
||||
for row, xcol := range g.controls {
|
||||
for col, c := range xcol
|
||||
for col, c := range xcol {
|
||||
err := c.make(window)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error adding control (%d,%d) to Grid: %v", row, col, err)
|
||||
|
@ -93,24 +94,24 @@ func (g *Grid) setRect(x int, y int, width int, height int) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("error getting preferred size of control (%d,%d) in Grid.setRect(): %v", row, col, err)
|
||||
}
|
||||
widths[row][col] = w
|
||||
g.widths[row][col] = w
|
||||
g.heights[row][col] = h
|
||||
g.rowheights[row] = max(g.rowheights[row], h)
|
||||
g.colwidths[col] = max(g.colwidths[row], w)
|
||||
g.colwidths[col] = max(g.colwidths[col], w)
|
||||
}
|
||||
}
|
||||
// 3) draw
|
||||
startx := x
|
||||
for row, xcol := range g.controls {
|
||||
for col, c := range xcol {
|
||||
err = c.setRect(x, y, g.widths[row][col], g.heights[row][col])
|
||||
err := c.setRect(x, y, g.widths[row][col], g.heights[row][col])
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting size of control (%d,%d) in Grid.setRect(): %v", row, col, err)
|
||||
}
|
||||
x += colwidths[col]
|
||||
x += g.colwidths[col]
|
||||
}
|
||||
x = startx
|
||||
y += rowheights[row]
|
||||
y += g.rowheights[row]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -138,12 +139,12 @@ func (g *Grid) preferredSize() (width int, height int, err error) {
|
|||
for col, c := range xcol {
|
||||
w, h, err := c.preferredSize()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting preferred size of control (%d,%d) in Grid.setRect(): %v", row, col, err)
|
||||
return 0, 0, fmt.Errorf("error getting preferred size of control (%d,%d) in Grid.setRect(): %v", row, col, err)
|
||||
}
|
||||
widths[row][col] = w
|
||||
g.widths[row][col] = w
|
||||
g.heights[row][col] = h
|
||||
g.rowheights[row] = max(g.rowheights[row], h)
|
||||
g.colwidths[col] = max(g.colwidths[row], w)
|
||||
g.colwidths[col] = max(g.colwidths[col], w)
|
||||
}
|
||||
}
|
||||
// 3) now compute
|
||||
|
|
22
main_test.go
22
main_test.go
|
@ -6,6 +6,23 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func gridWindow() (*Window, error) {
|
||||
w := NewWindow("Grid Test", 400, 400)
|
||||
b00 := NewButton("0,0")
|
||||
b01 := NewButton("0,1")
|
||||
b02 := NewButton("0,2")
|
||||
l11 := NewListbox(true, "1,1")
|
||||
b12 := NewButton("1,2")
|
||||
l20 := NewLabel("2,0")
|
||||
c21 := NewCheckbox("2,1")
|
||||
l22 := NewLabel("2,2")
|
||||
g := NewGrid(3,
|
||||
b00, b01, b02,
|
||||
Space(), l11, b12,
|
||||
l20, c21, l22)
|
||||
return w, w.Open(g)
|
||||
}
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
w := NewWindow("Main Window", 320, 240)
|
||||
w.Closing = Event()
|
||||
|
@ -50,6 +67,10 @@ func TestMain(t *testing.T) {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
gw, err := gridWindow()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
mainloop:
|
||||
for {
|
||||
|
@ -93,5 +114,6 @@ mainloop:
|
|||
pbar.SetProgress(prog)
|
||||
}
|
||||
}
|
||||
gw.Hide()
|
||||
w.Hide()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue