-
Notifications
You must be signed in to change notification settings - Fork 1
/
hbox.go
346 lines (305 loc) · 9.01 KB
/
hbox.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
package goey
import (
"bitbucket.org/rj/goey/base"
)
var (
hboxKind = base.NewKind("bitbucket.org/rj/goey.HBox")
)
// HBox describes a layout widget that arranges its child widgets into a row.
// Children are positioned in order from the left towards the right. The main
// axis for alignment is therefore horizontal, with the cross axis for alignment is vertical.
//
// 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 HBox struct {
AlignMain MainAxisAlign // Control distribution of excess horizontal space when positioning children.
AlignCross CrossAxisAlign // Control distribution of excess vertical space when positioning children.
Children []base.Widget // Children.
}
// Kind returns the concrete type for use in the Widget interface.
// Users should not need to use this method directly.
func (*HBox) Kind() *base.Kind {
return &hboxKind
}
// Mount creates a horizontal layout for child widgets in the GUI.
// The newly created widget will be a child of the widget specified by parent.
func (w *HBox) 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 &hboxElement{
parent: parent,
children: c,
alignMain: w.AlignMain,
alignCross: w.AlignCross,
childrenInfo: ci,
totalFlex: totalFlex,
}, nil
}
type hboxElement struct {
parent base.Control
children []base.Element
alignMain MainAxisAlign
alignCross CrossAxisAlign
childrenInfo []boxElementInfo
totalWidth base.Length
totalFlex int
}
type boxElementInfo struct {
size base.Size
flex int
}
func (w *hboxElement) Close() {
base.CloseElements(w.children)
w.children = nil
w.childrenInfo = nil
}
func (*hboxElement) Kind() *base.Kind {
return &hboxKind
}
func (w *hboxElement) Layout(bc base.Constraints) base.Size {
if len(w.children) == 0 {
w.totalWidth = 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 := calculateHGap(nil, nil)
cbc.TightenWidth(cbc.Max.Width.Scale(1, count) - gap.Scale(count-1, count))
} else {
cbc.Min.Width = 0
cbc.Max.Width = base.Inf
}
if w.alignCross == Stretch {
if cbc.HasBoundedHeight() {
cbc = cbc.TightenHeight(cbc.Max.Height)
} else {
cbc = cbc.TightenHeight(w.MinIntrinsicHeight(base.Inf))
}
} else {
cbc = cbc.LoosenHeight()
}
width := base.Length(0)
height := 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() {
width += calculateHGap(previous, v)
} else {
width += calculateHGap(nil, nil)
}
}
previous = v
// Perform layout of the element. Track impact on width and height.
size := v.Layout(cbc)
w.childrenInfo[i].size = size
width += size.Width
height = max(height, size.Height)
}
w.totalWidth = width
// Need to adjust height to any widgets that have flex
if w.totalFlex > 0 {
extraWidth := base.Length(0)
if bc.HasBoundedWidth() && bc.Max.Width > w.totalWidth {
extraWidth = bc.Max.Width - w.totalWidth
} else if bc.Min.Width > w.totalWidth {
extraWidth = bc.Min.Width - w.totalWidth
}
if extraWidth > 0 {
for i, v := range w.childrenInfo {
if v.flex > 0 {
oldWidth := v.size.Width
fbc := cbc.TightenWidth(v.size.Width + extraWidth.Scale(v.flex, w.totalFlex))
size := w.children[i].Layout(fbc)
w.childrenInfo[i].size = size
w.totalWidth += size.Width - oldWidth
}
}
}
}
if w.alignCross == Stretch {
return bc.Constrain(base.Size{width, cbc.Min.Height})
}
return bc.Constrain(base.Size{width, height})
}
func (w *hboxElement) MinIntrinsicHeight(width base.Length) base.Length {
if len(w.children) == 0 {
return 0
}
if w.alignMain == Homogeneous {
width = guardInf(width, width.Scale(1, len(w.children)))
size := w.children[0].MinIntrinsicHeight(width)
for _, v := range w.children[1:] {
size = max(size, v.MinIntrinsicHeight(width))
}
return size
}
size := w.children[0].MinIntrinsicHeight(base.Inf)
for _, v := range w.children[1:] {
size = max(size, v.MinIntrinsicHeight(base.Inf))
}
return size
}
func (w *hboxElement) MinIntrinsicWidth(height base.Length) base.Length {
if len(w.children) == 0 {
return 0
}
size := w.children[0].MinIntrinsicWidth(height)
if w.alignMain.IsPacked() {
previous := w.children[0]
for _, v := range w.children[1:] {
// Add the preferred gap between this pair of widgets
size += calculateHGap(previous, v)
previous = v
// Find minimum size for this widget, and update
size += v.MinIntrinsicWidth(height)
}
return size
}
if w.alignMain == Homogeneous {
for _, v := range w.children[1:] {
size = max(size, v.MinIntrinsicWidth(height))
}
// Add a minimum gap between the controls.
size = size.Scale(len(w.children), 1) + calculateHGap(nil, nil).Scale(len(w.children)-1, 1)
return size
}
for _, v := range w.children[1:] {
size += v.MinIntrinsicWidth(height)
}
// Add a minimum gap between the controls.
if w.alignMain == SpaceBetween {
size += calculateHGap(nil, nil).Scale(len(w.children)-1, 1)
} else {
size += calculateHGap(nil, nil).Scale(len(w.children)+1, 1)
}
return size
}
func (w *hboxElement) SetBounds(bounds base.Rectangle) {
if len(w.children) == 0 {
return
}
if w.alignMain == Homogeneous {
gap := calculateHGap(nil, nil)
dx := bounds.Dx() + gap
count := len(w.children)
for i, v := range w.children {
x1 := bounds.Min.X + dx.Scale(i, count)
x2 := bounds.Min.X + dx.Scale(i+1, count) - gap
w.setBoundsForChild(i, v, x1, bounds.Min.Y, x2, bounds.Max.Y)
}
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.X += (bounds.Dx() - w.totalWidth) / 2
case MainEnd:
bounds.Min.X = bounds.Max.X - w.totalWidth
case SpaceAround:
extraGap = (bounds.Dx() - w.totalWidth).Scale(1, len(w.children)+1)
bounds.Min.X += extraGap
extraGap += calculateHGap(nil, nil)
case SpaceBetween:
if len(w.children) > 1 {
extraGap = (bounds.Dx() - w.totalWidth).Scale(1, len(w.children)-1)
extraGap += calculateHGap(nil, nil)
} else {
// There are no controls between which to put the extra space.
// The following essentially convert SpaceBetween to SpaceAround
bounds.Min.X += (bounds.Dx() - w.totalWidth) / 2
}
}
}
// Position all of the child controls.
posX := bounds.Min.X
previous := base.Element(nil)
for i, v := range w.children {
if w.alignMain.IsPacked() {
if i > 0 {
posX += calculateHGap(previous, v)
}
previous = v
}
dx := w.childrenInfo[i].size.Width
w.setBoundsForChild(i, v, posX, bounds.Min.Y, posX+dx, bounds.Max.Y)
posX += dx + extraGap
}
}
func (w *hboxElement) setBoundsForChild(i int, v base.Element, posX, posY, posX2, posY2 base.Length) {
dy := w.childrenInfo[i].size.Height
switch w.alignCross {
case CrossStart:
v.SetBounds(base.Rectangle{
base.Point{posX, posY},
base.Point{posX2, posY + dy},
})
case CrossCenter:
v.SetBounds(base.Rectangle{
base.Point{posX, posY + (posY2-posY-dy)/2},
base.Point{posX2, posY + (posY2-posY+dy)/2},
})
case CrossEnd:
v.SetBounds(base.Rectangle{
base.Point{posX, posY2 - dy},
base.Point{posX2, posY2},
})
case Stretch:
v.SetBounds(base.Rectangle{
base.Point{posX, posY},
base.Point{posX2, posY2},
})
}
}
func updateFlex(c []base.Element, alignMain MainAxisAlign, clientInfo []boxElementInfo) ([]boxElementInfo, int) {
if len(c) <= cap(clientInfo) {
clientInfo = clientInfo[:len(c)]
} else {
clientInfo = make([]boxElementInfo, len(c))
}
totalFlex := 0
for i, v := range c {
if elem, ok := v.(*expandElement); ok {
clientInfo[i].flex = elem.factor + 1
totalFlex += elem.factor + 1
}
}
if alignMain == Homogeneous {
totalFlex = 0
}
return clientInfo, totalFlex
}
func (w *hboxElement) updateProps(data *HBox) (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.totalWidth = 0
return err
}
func (w *hboxElement) UpdateProps(data base.Widget) error {
return w.updateProps(data.(*HBox))
}