forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxlayout.go
222 lines (187 loc) · 5.74 KB
/
boxlayout.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package layout
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
)
// NewVBoxLayout returns a vertical box layout for stacking a number of child
// canvas objects or widgets top to bottom. The objects are always displayed
// at their vertical MinSize. Use a different layout if the objects are intended
// to be larger than their vertical MinSize.
func NewVBoxLayout() fyne.Layout {
return vBoxLayout{
paddingFunc: theme.Padding,
}
}
// NewHBoxLayout returns a horizontal box layout for stacking a number of child
// canvas objects or widgets left to right. The objects are always displayed
// at their horizontal MinSize. Use a different layout if the objects are intended
// to be larger than their horizontal MinSize.
func NewHBoxLayout() fyne.Layout {
return hBoxLayout{
paddingFunc: theme.Padding,
}
}
// NewCustomPaddedHBoxLayout returns a layout similar to HBoxLayout that uses a custom
// amount of padding in between objects instead of the theme.Padding value.
//
// Since: 2.5
func NewCustomPaddedHBoxLayout(padding float32) fyne.Layout {
return hBoxLayout{
paddingFunc: func() float32 { return padding },
}
}
// NewCustomPaddedVBoxLayout returns a layout similar to VBoxLayout that uses a custom
// amount of padding in between objects instead of the theme.Padding value.
//
// Since: 2.5
func NewCustomPaddedVBoxLayout(padding float32) fyne.Layout {
return vBoxLayout{
paddingFunc: func() float32 { return padding },
}
}
// Declare conformity with Layout interface
var _ fyne.Layout = (*vBoxLayout)(nil)
type vBoxLayout struct {
paddingFunc func() float32
}
// Layout is called to pack all child objects into a specified size.
// This will pack objects into a single column where each item
// is full width but the height is the minimum required.
// Any spacers added will pad the view, sharing the space if there are two or more.
func (v vBoxLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
spacers := 0
visibleObjects := 0
// Size taken up by visible objects
total := float32(0)
for _, child := range objects {
if !child.Visible() {
continue
}
if isVerticalSpacer(child) {
spacers++
continue
}
visibleObjects++
total += child.MinSize().Height
}
padding := v.paddingFunc()
// Amount of space not taken up by visible objects and inter-object padding
extra := size.Height - total - (padding * float32(visibleObjects-1))
// Spacers split extra space equally
spacerSize := float32(0)
if spacers > 0 {
spacerSize = extra / float32(spacers)
}
x, y := float32(0), float32(0)
for _, child := range objects {
if !child.Visible() {
continue
}
if isVerticalSpacer(child) {
y += spacerSize
continue
}
child.Move(fyne.NewPos(x, y))
height := child.MinSize().Height
y += padding + height
child.Resize(fyne.NewSize(size.Width, height))
}
}
// MinSize finds the smallest size that satisfies all the child objects.
// For a BoxLayout this is the width of the widest item and the height is
// the sum of all children combined with padding between each.
func (v vBoxLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
minSize := fyne.NewSize(0, 0)
addPadding := false
padding := v.paddingFunc()
for _, child := range objects {
if !child.Visible() || isVerticalSpacer(child) {
continue
}
childMin := child.MinSize()
minSize.Width = fyne.Max(childMin.Width, minSize.Width)
minSize.Height += childMin.Height
if addPadding {
minSize.Height += padding
}
addPadding = true
}
return minSize
}
// Declare conformity with Layout interface
var _ fyne.Layout = (*hBoxLayout)(nil)
type hBoxLayout struct {
paddingFunc func() float32
}
// Layout is called to pack all child objects into a specified size.
// For a VBoxLayout this will pack objects into a single column where each item
// is full width but the height is the minimum required.
// Any spacers added will pad the view, sharing the space if there are two or more.
func (g hBoxLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
spacers := 0
visibleObjects := 0
// Size taken up by visible objects
total := float32(0)
for _, child := range objects {
if !child.Visible() {
continue
}
if isHorizontalSpacer(child) {
spacers++
continue
}
visibleObjects++
total += child.MinSize().Width
}
padding := g.paddingFunc()
// Amount of space not taken up by visible objects and inter-object padding
extra := size.Width - total - (padding * float32(visibleObjects-1))
// Spacers split extra space equally
spacerSize := float32(0)
if spacers > 0 {
spacerSize = extra / float32(spacers)
}
x, y := float32(0), float32(0)
for _, child := range objects {
if !child.Visible() {
continue
}
if isHorizontalSpacer(child) {
x += spacerSize
continue
}
child.Move(fyne.NewPos(x, y))
width := child.MinSize().Width
x += padding + width
child.Resize(fyne.NewSize(width, size.Height))
}
}
// MinSize finds the smallest size that satisfies all the child objects.
// For a BoxLayout this is the width of the widest item and the height is
// the sum of all children combined with padding between each.
func (g hBoxLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
minSize := fyne.NewSize(0, 0)
addPadding := false
padding := g.paddingFunc()
for _, child := range objects {
if !child.Visible() || isHorizontalSpacer(child) {
continue
}
childMin := child.MinSize()
minSize.Height = fyne.Max(childMin.Height, minSize.Height)
minSize.Width += childMin.Width
if addPadding {
minSize.Width += padding
}
addPadding = true
}
return minSize
}
func isVerticalSpacer(obj fyne.CanvasObject) bool {
spacer, ok := obj.(SpacerObject)
return ok && spacer.ExpandVertical()
}
func isHorizontalSpacer(obj fyne.CanvasObject) bool {
spacer, ok := obj.(SpacerObject)
return ok && spacer.ExpandHorizontal()
}