summaryrefslogtreecommitdiff
path: root/size.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-01-28 20:15:59 -0600
committerJeff Carr <[email protected]>2024-01-28 20:15:59 -0600
commit3029f04bd2b82eadfe7d5569616c322175a94c64 (patch)
tree199ae0b7f98171838439cc8299990e5c91217f18 /size.go
parent63b76b29127aae98f5b709611cfcd036484ebefe (diff)
trying to compute sizes
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'size.go')
-rw-r--r--size.go119
1 files changed, 119 insertions, 0 deletions
diff --git a/size.go b/size.go
new file mode 100644
index 0000000..ed73cb9
--- /dev/null
+++ b/size.go
@@ -0,0 +1,119 @@
+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
+ }
+
+ switch tk.WidgetType {
+ case widget.Window:
+ var maxH int = 0
+ var maxW int = 0
+ for _, child := range tk.children {
+ 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 {
+ sizeW, sizeH := child.Size()
+
+ // increment straight down
+ maxH += sizeH
+ if sizeW > maxW {
+ maxW = sizeW
+ }
+ }
+ return maxW, maxH
+ }
+ if tk.isFake {
+ return 0, 0
+ }
+ return tk.gocuiSize.Width(), tk.gocuiSize.Height()
+}
+
+func (w *guiWidget) sizeGrid() (int, int) {
+
+ // first compute the max sizes of the rows and columns
+ for _, child := range w.children {
+ sizeW, sizeH := child.Size()
+
+ // set the child's realWidth, and grid offset
+ if w.widths[child.AtW] < sizeW {
+ w.widths[child.AtW] = sizeW
+ }
+ if w.heights[child.AtH] < sizeH {
+ w.heights[child.AtH] = sizeH
+ }
+ }
+
+ var maxW int = 0
+ var maxH int = 0
+
+ // find the width and height offset of the grid for AtW,AtH
+ for _, child := range w.children {
+ var totalW, totalH int
+ for i, w := range w.widths {
+ if i < child.AtW {
+ totalW += w
+ }
+ }
+ for i, h := range w.heights {
+ if i < child.AtH {
+ totalH += h
+ }
+ }
+
+ if totalW > maxW {
+ maxW = totalW
+ }
+ if totalH > maxH {
+ maxH = totalH
+ }
+ }
+ return maxW, maxH
+}
+
+func (tk *guiWidget) sizeBox() (int, int) {
+ if tk.WidgetType != widget.Box {
+ return 0, 0
+ }
+ var maxW int = 0
+ var maxH int = 0
+
+ for _, child := range tk.children {
+ 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, maxH
+}
+