-
Notifications
You must be signed in to change notification settings - Fork 1
/
vbox.go
355 lines (312 loc) · 10 KB
/
vbox.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package goey
import (
"bitbucket.org/rj/goey/base"
)
var (
vboxKind = base.NewKind("bitbucket.org/rj/goey.VBox")
)
// MainAxisAlign identifies the different types of alignment that are possible
// along the main axis for a vertical box or horizontal box layout.
type MainAxisAlign uint8
// Allowed values for alignment of the main axis in a vertical box (VBox) or
// horizontal box (HBox).
const (
MainStart MainAxisAlign = iota // Children will be packed together at the top or left of the box
MainCenter // Children will be packed together and centered in the box.
MainEnd // Children will be packed together at the bottom or right of the box
SpaceAround // Children will be spaced apart
SpaceBetween // Children will be spaced apart, but the first and last children will but the ends of the box.
Homogeneous // Children will be allocated equal space.
)
// IsPacked returns true if the main axis alignment is a one where children
// will be packed together.
func (a MainAxisAlign) IsPacked() bool {
return a <= MainEnd
}
// CrossAxisAlign identifies the different types of alignment that are possible
// along the cross axis for vertical box and horizontal box layouts.
type CrossAxisAlign uint8
// Allowed values for alignment of the cross axis in a vertical box (VBox) or
// horizontal box (HBox).
const (
Stretch CrossAxisAlign = iota // Children will be stretched so that the extend across box
CrossStart // Children will be aligned to the left or top of the box
CrossCenter // Children will be aligned in the center of the box
CrossEnd // Children will be aligned to the right or bottom of the box
)
// VBox describes a layout widget that arranges its child widgets into a column.
// Children are positioned in order from the top towards the bottom. The main
// axis for alignment is therefore vertical, with the cross axis for alignment is horizontal.
//
// The size of the box will try to set a width sufficient to contain all of its
// children. Extra space will be distributed according to the value of
// AlignMain. Subject to the box constraints during layout, the height should
// match the largest minimum height of the child widgets.
type VBox struct {
AlignMain MainAxisAlign
AlignCross CrossAxisAlign
Children []base.Widget
}
// Kind returns the concrete type for use in the Widget interface.
// Users should not need to use this method directly.
func (*VBox) Kind() *base.Kind {
return &vboxKind
}
// Mount creates a vertical layout for child widgets in the GUI.
// The newly created widget will be a child of the widget specified by parent.
func (w *VBox) Mount(parent base.Control) (base.Element, error) {
c := make([]base.Element, 0, len(w.Children))
// Mount all of the children
for _, v := range w.Children {
mountedChild, err := v.Mount(parent)
if err != nil {
base.CloseElements(c)
return nil, err
}
c = append(c, mountedChild)
}
// Record the flex factor for all children
ci, totalFlex := updateFlex(c, w.AlignMain, nil)
return &vboxElement{
parent: parent,
children: c,
alignMain: w.AlignMain,
alignCross: w.AlignCross,
childrenInfo: ci,
totalFlex: totalFlex,
}, nil
}
type vboxElement struct {
parent base.Control
children []base.Element
alignMain MainAxisAlign
alignCross CrossAxisAlign
childrenInfo []boxElementInfo
totalHeight base.Length
totalFlex int
}
func (w *vboxElement) Close() {
base.CloseElements(w.children)
w.children = nil
w.childrenInfo = nil
}
func (*vboxElement) Kind() *base.Kind {
return &vboxKind
}
func (w *vboxElement) Layout(bc base.Constraints) base.Size {
if len(w.children) == 0 {
w.totalHeight = 0
return bc.Constrain(base.Size{})
}
// Determine the constraints for layout of child elements.
cbc := bc
if w.alignMain == Homogeneous {
count := len(w.children)
gap := calculateVGap(nil, nil)
cbc.TightenHeight(cbc.Max.Height.Scale(1, count) - gap.Scale(count-1, count))
} else {
cbc.Min.Height = 0
cbc.Max.Height = base.Inf
}
if w.alignCross == Stretch {
if cbc.HasBoundedWidth() {
cbc = cbc.TightenWidth(cbc.Max.Width)
} else {
cbc = cbc.TightenWidth(w.MinIntrinsicWidth(base.Inf))
}
} else {
cbc = cbc.LoosenWidth()
}
height := base.Length(0)
width := base.Length(0)
previous := base.Element(nil)
for i, v := range w.children {
// Determine what gap needs to be inserted between the elements.
if i > 0 {
if w.alignMain.IsPacked() {
height += calculateVGap(previous, v)
} else {
height += calculateVGap(nil, nil)
}
}
previous = v
// Perform layout of the element. Track impact on width and height.
size := v.Layout(cbc)
w.childrenInfo[i].size = size
height += size.Height
width = max(width, size.Width)
}
w.totalHeight = height
// Need to adjust width to any widgets that have flex
if w.totalFlex > 0 {
extraHeight := base.Length(0)
if bc.HasBoundedHeight() && bc.Max.Height > w.totalHeight {
extraHeight = bc.Max.Height - w.totalHeight
} else if bc.Min.Height > w.totalHeight {
extraHeight = bc.Min.Height - w.totalHeight
}
if extraHeight > 0 {
for i, v := range w.childrenInfo {
if v.flex > 0 {
oldHeight := v.size.Height
fbc := cbc.TightenHeight(v.size.Height + extraHeight.Scale(v.flex, w.totalFlex))
size := w.children[i].Layout(fbc)
w.childrenInfo[i].size = size
w.totalHeight += size.Height - oldHeight
}
}
}
}
if w.alignCross == Stretch {
return bc.Constrain(base.Size{cbc.Min.Width, height})
}
return bc.Constrain(base.Size{width, height})
}
func (w *vboxElement) MinIntrinsicWidth(height base.Length) base.Length {
if len(w.children) == 0 {
return 0
}
if w.alignMain == Homogeneous {
height = guardInf(height, height.Scale(1, len(w.children)))
size := w.children[0].MinIntrinsicWidth(height)
for _, v := range w.children[1:] {
size = max(size, v.MinIntrinsicWidth(height))
}
return size
}
size := w.children[0].MinIntrinsicWidth(base.Inf)
for _, v := range w.children[1:] {
size = max(size, v.MinIntrinsicWidth(base.Inf))
}
return size
}
func (w *vboxElement) MinIntrinsicHeight(width base.Length) base.Length {
if len(w.children) == 0 {
return 0
}
size := w.children[0].MinIntrinsicHeight(width)
if w.alignMain.IsPacked() {
previous := w.children[0]
for _, v := range w.children[1:] {
// Add the preferred gap between this pair of widgets
size += calculateVGap(previous, v)
previous = v
// Find minimum size for this widget, and update
size += v.MinIntrinsicHeight(width)
}
return size
}
if w.alignMain == Homogeneous {
for _, v := range w.children[1:] {
size = max(size, v.MinIntrinsicHeight(width))
}
// Add a minimum gap between the controls.
size = size.Scale(len(w.children), 1) + calculateVGap(nil, nil).Scale(len(w.children)-1, 1)
return size
}
for _, v := range w.children[1:] {
size += v.MinIntrinsicHeight(width)
}
// Add a minimum gap between the controls.
if w.alignMain == SpaceBetween {
size += calculateVGap(nil, nil).Scale(len(w.children)-1, 1)
} else {
size += calculateVGap(nil, nil).Scale(len(w.children)+1, 1)
}
return size
}
func (w *vboxElement) SetBounds(bounds base.Rectangle) {
if len(w.children) == 0 {
return
}
if w.alignMain == Homogeneous {
gap := calculateVGap(nil, nil)
dy := bounds.Dy() + gap
count := len(w.children)
for i, v := range w.children {
y1 := bounds.Min.Y + dy.Scale(i, count)
y2 := bounds.Min.Y + dy.Scale(i+1, count) - gap
w.setBoundsForChild(i, v, bounds.Min.X, y1, bounds.Max.X, y2)
}
return
}
// Adjust the bounds so that the minimum Y handles vertical alignment
// of the controls. We also calculate 'extraGap' which will adjust
// spacing of the controls for non-packed alignments.
extraGap := base.Length(0)
if w.totalFlex == 0 {
switch w.alignMain {
case MainStart:
// Do nothing
case MainCenter:
bounds.Min.Y += (bounds.Dy() - w.totalHeight) / 2
case MainEnd:
bounds.Min.Y = bounds.Max.Y - w.totalHeight
case SpaceAround:
extraGap = (bounds.Dy() - w.totalHeight).Scale(1, len(w.children)+1)
bounds.Min.Y += extraGap
extraGap += calculateVGap(nil, nil)
case SpaceBetween:
if len(w.children) > 1 {
extraGap = (bounds.Dy() - w.totalHeight).Scale(1, len(w.children)-1)
extraGap += calculateVGap(nil, nil)
} else {
// There are no controls between which to put the extra space.
// The following essentially convert SpaceBetween to SpaceAround
bounds.Min.Y += (bounds.Dy() - w.totalHeight) / 2
}
}
}
// Position all of the child controls.
posY := bounds.Min.Y
previous := base.Element(nil)
for i, v := range w.children {
if w.alignMain.IsPacked() {
if i > 0 {
posY += calculateVGap(previous, v)
}
previous = v
}
dy := w.childrenInfo[i].size.Height
w.setBoundsForChild(i, v, bounds.Min.X, posY, bounds.Max.X, posY+dy)
posY += dy + extraGap
}
}
func (w *vboxElement) setBoundsForChild(i int, v base.Element, posX, posY, posX2, posY2 base.Length) {
dx := w.childrenInfo[i].size.Width
switch w.alignCross {
case CrossStart:
v.SetBounds(base.Rectangle{
base.Point{posX, posY},
base.Point{posX + dx, posY2},
})
case CrossCenter:
v.SetBounds(base.Rectangle{
base.Point{posX + (posX2-posX-dx)/2, posY},
base.Point{posX + (posX2-posX+dx)/2, posY2},
})
case CrossEnd:
v.SetBounds(base.Rectangle{
base.Point{posX2 - dx, posY},
base.Point{posX2, posY2},
})
case Stretch:
v.SetBounds(base.Rectangle{
base.Point{posX, posY},
base.Point{posX2, posY2},
})
}
}
func (w *vboxElement) updateProps(data *VBox) (err error) {
// Update properties
w.alignMain = data.AlignMain
w.alignCross = data.AlignCross
w.children, err = base.DiffChildren(w.parent, w.children, data.Children)
// Clear cached values
w.childrenInfo, w.totalFlex = updateFlex(w.children, w.alignMain, w.childrenInfo)
w.totalHeight = 0
return err
}
func (w *vboxElement) UpdateProps(data base.Widget) error {
return w.updateProps(data.(*VBox))
}