summaryrefslogtreecommitdiff
path: root/size.go
blob: ac12415267b27dd4b4ba2c0eba25ebac77d7d289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

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.node.Hidden() {
		return 0, 0
	}

	switch tk.node.WidgetType {
	case widget.Window:
		var maxH int = 0
		var maxW int = 0
		for _, child := range tk.children {
			if tk.node.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.node.Hidden() {
				continue
			}
			sizeW, sizeH := child.Size()

			// increment straight down
			maxH += sizeH
			if sizeW > maxW {
				maxW = sizeW
			}
		}
		return maxW + me.GroupPadW + 3, maxH
	case widget.Label:
		return len(tk.String()) + 2, 1
	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.node.Hidden() {
		return 0, 0
	}

	// first compute the max sizes of the rows and columns
	for _, child := range w.children {
		if w.node.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.node.WidgetType != widget.Box {
		return 0, 0
	}
	if w.node.Hidden() {
		return 0, 0
	}
	var maxW int = 0
	var maxH int = 0

	for _, child := range w.children {
		if w.node.Hidden() {
			continue
		}
		sizeW, sizeH := child.Size()
		if child.node.State.Direction == widget.Vertical {
			maxW += sizeW
			if sizeH > maxH {
				maxH = sizeH
			}
		} else {
			maxH += sizeH
			if sizeW > maxW {
				maxW = sizeW
			}
		}
	}
	return maxW + me.BoxPadW, maxH
}