gocui/size.go

131 lines
2.3 KiB
Go

package main
import (
"go.wit.com/widget"
)
func (tk *guiWidget) Size() (int, int) {
if tk == nil {
return 0, 0
}
if me.treeRoot == nil {
return 0, 0
}
// don't count hidden widgets in size calculations
if tk.hidden {
return 0, 0
}
switch tk.WidgetType {
case widget.Window:
var maxH int = 0
var maxW int = 0
for _, child := range tk.children {
if tk.hidden {
continue
}
sizeW, sizeH := child.Size()
maxW += sizeW
if sizeH > maxH {
maxH = sizeH
}
}
return maxW, maxH
case widget.Grid:
return tk.sizeGrid()
case widget.Box:
return tk.sizeBox()
case widget.Group:
// move the group to the parent's next location
maxW := tk.gocuiSize.Width()
maxH := tk.gocuiSize.Height()
for _, child := range tk.children {
if tk.hidden {
continue
}
sizeW, sizeH := child.Size()
// increment straight down
maxH += sizeH
if sizeW > maxW {
maxW = sizeW
}
}
return maxW + me.GroupPadW + 3, maxH
case widget.Checkbox:
return len(tk.String()) + 2, 3
}
if tk.isFake {
return 0, 0
}
return len(tk.String()), 3
}
func (w *guiWidget) sizeGrid() (int, int) {
if w.hidden {
return 0, 0
}
// first compute the max sizes of the rows and columns
for _, child := range w.children {
if w.hidden {
continue
}
sizeW, sizeH := child.Size()
sizeW += 2
// set the child's realWidth, and grid offset
if w.widths[child.node.State.AtW] < sizeW {
w.widths[child.node.State.AtW] = sizeW
}
if w.heights[child.node.State.AtH] < sizeH {
w.heights[child.node.State.AtH] = sizeH
}
}
// find the width and height offset of the grid for AtW,AtH
var totalW int = 0
var totalH int = 0
for _, width := range w.widths {
totalW += width
}
for _, h := range w.heights {
totalH += h
}
return totalW + me.GridPadW, totalH
}
func (w *guiWidget) sizeBox() (int, int) {
if w.WidgetType != widget.Box {
return 0, 0
}
if w.hidden {
return 0, 0
}
var maxW int = 0
var maxH int = 0
for _, child := range w.children {
if w.hidden {
continue
}
sizeW, sizeH := child.Size()
if child.direction == widget.Horizontal {
maxW += sizeW
if sizeH > maxH {
maxH = sizeH
}
} else {
maxH += sizeH
if sizeW > maxW {
maxW = sizeW
}
}
}
return maxW + me.BoxPadW, maxH
}