-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
flex_layout.go
731 lines (711 loc) · 18.8 KB
/
flex_layout.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
// Copyright (c) 2021-2024 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"github.com/richardwilkes/toolbox/xmath"
"github.com/richardwilkes/unison/enums/align"
)
var _ Layout = &FlexLayout{}
// FlexLayout lays out the children of its Layoutable based on the FlexLayoutData assigned to each child.
type FlexLayout struct {
sizingCache map[*Panel]map[Size]*flexSizingCacheData
rows int
Columns int
HSpacing float32
VSpacing float32
HAlign align.Enum
VAlign align.Enum
EqualColumns bool
}
type flexSizingCacheData struct {
minSize Size
prefSize Size
maxSize Size
}
// FlexLayoutData is used to control how an object is laid out by the FlexLayout layout.
type FlexLayoutData struct {
cacheSize Size
minCacheSize Size
SizeHint Size
MinSize Size
HSpan int
VSpan int
HAlign align.Enum
VAlign align.Enum
HGrab bool
VGrab bool
}
// LayoutSizes implements the Layout interface.
func (f *FlexLayout) LayoutSizes(target *Panel, hint Size) (minSize, prefSize, maxSize Size) {
f.sizingCache = make(map[*Panel]map[Size]*flexSizingCacheData)
var insets Size
if b := target.Border(); b != nil {
insets = b.Insets().Size()
hint = hint.Sub(insets).Max(Size{})
}
prefSize = f.layout(target, Point{}, hint, false, false).Add(insets)
return f.layout(target, Point{}, hint, false, true).Add(insets), prefSize, MaxSize(prefSize)
}
// PerformLayout implements the Layout interface.
func (f *FlexLayout) PerformLayout(target *Panel) {
f.sizingCache = make(map[*Panel]map[Size]*flexSizingCacheData)
var insets Insets
if b := target.Border(); b != nil {
insets = b.Insets()
}
f.layout(target, Point{X: insets.Left, Y: insets.Top}, target.ContentRect(true).Size.Sub(insets.Size()), true, false)
}
func (f *FlexLayout) layout(target *Panel, location Point, hint Size, move, useMinimumSize bool) Size {
var totalSize Size
if f.Columns > 0 {
children := f.prepChildren(target, useMinimumSize)
if len(children) > 0 {
if f.HSpacing < 0 {
f.HSpacing = 0
}
if f.VSpacing < 0 {
f.VSpacing = 0
}
grid := f.buildGrid(children)
widths := f.adjustColumnWidths(hint.Width, grid)
f.wrap(hint.Width, grid, widths, useMinimumSize)
heights := f.adjustRowHeights(hint.Height, grid)
totalSize.Width += f.HSpacing * float32(f.Columns-1)
totalSize.Height += f.VSpacing * float32(f.rows-1)
for i := 0; i < f.Columns; i++ {
totalSize.Width += widths[i]
}
for i := 0; i < f.rows; i++ {
totalSize.Height += heights[i]
}
if move {
if totalSize.Width < hint.Width {
if f.HAlign == align.Middle {
location.X += xmath.Round((hint.Width - totalSize.Width) / 2)
} else if f.HAlign == align.End {
location.X += hint.Width - totalSize.Width
}
}
if totalSize.Height < hint.Height {
if f.VAlign == align.Middle {
location.Y += xmath.Round((hint.Height - totalSize.Height) / 2)
} else if f.VAlign == align.End {
location.Y += hint.Height - totalSize.Height
}
}
f.positionChildren(location, grid, widths, heights)
}
}
}
return totalSize
}
func (f *FlexLayout) sizingCacheData(panel *Panel, hint Size) *flexSizingCacheData {
m, ok := f.sizingCache[panel]
if !ok {
m = make(map[Size]*flexSizingCacheData)
f.sizingCache[panel] = m
}
var data *flexSizingCacheData
if data, ok = m[hint]; !ok {
var sizing flexSizingCacheData
sizing.minSize, sizing.prefSize, sizing.maxSize = panel.Sizes(hint)
data = &sizing
m[hint] = data
}
return data
}
func (f *FlexLayout) prepChildren(target *Panel, useMinimumSize bool) []*Panel {
var hint Size
children := target.Children()
for _, child := range children {
getDataFromTarget(child).computeCacheSize(f.sizingCacheData(child, hint), hint, useMinimumSize)
}
return children
}
func getDataFromTarget(target *Panel) *FlexLayoutData {
if data, ok := target.LayoutData().(*FlexLayoutData); ok {
return data
}
data := &FlexLayoutData{
HSpan: 1,
VSpan: 1,
VAlign: align.Middle,
}
target.layoutData = data
return data
}
func (f *FlexLayout) buildGrid(children []*Panel) [][]*Panel {
var grid [][]*Panel
var row, column int
f.rows = 0
for _, child := range children {
data := getDataFromTarget(child)
hSpan := max(1, min(data.HSpan, f.Columns))
vSpan := max(1, data.VSpan)
for {
lastRow := row + vSpan
for lastRow >= len(grid) {
grid = append(grid, make([]*Panel, f.Columns))
}
// noinspection GoNilness
for column < f.Columns && grid[row][column] != nil {
column++
}
endCount := column + hSpan
if endCount <= f.Columns {
index := column
// noinspection GoNilness
for index < endCount && grid[row][index] == nil {
index++
}
if index == endCount {
break
}
column = index
}
if column+hSpan >= f.Columns {
column = 0
row++
}
}
for j := 0; j < vSpan; j++ {
pos := row + j
for k := 0; k < hSpan; k++ {
// noinspection GoNilness
grid[pos][column+k] = child
}
}
f.rows = max(f.rows, row+vSpan)
column += hSpan
}
return grid
}
func (f *FlexLayout) adjustColumnWidths(width float32, grid [][]*Panel) []float32 {
availableWidth := width - f.HSpacing*float32(f.Columns-1)
expandCount := 0
widths := make([]float32, f.Columns)
minWidths := make([]float32, f.Columns)
expandColumn := make([]bool, f.Columns)
for j := 0; j < f.Columns; j++ {
for i := 0; i < f.rows; i++ {
data := f.getData(grid, i, j, true)
if data != nil {
hSpan := max(1, min(data.HSpan, f.Columns))
if hSpan == 1 {
w := data.cacheSize.Width
if widths[j] < w {
widths[j] = w
}
if data.HGrab {
if !expandColumn[j] {
expandCount++
}
expandColumn[j] = true
}
minimumWidth := data.minCacheSize.Width
if !data.HGrab {
if minimumWidth < 1 {
w = data.cacheSize.Width
} else {
w = minimumWidth
}
if minWidths[j] < w {
minWidths[j] = w
}
}
}
}
}
for i := 0; i < f.rows; i++ {
data := f.getData(grid, i, j, false)
if data != nil {
hSpan := max(1, min(data.HSpan, f.Columns))
if hSpan > 1 {
var spanWidth, spanMinWidth float32
spanExpandCount := 0
for k := 0; k < hSpan; k++ {
spanWidth += widths[j-k]
spanMinWidth += minWidths[j-k]
if expandColumn[j-k] {
spanExpandCount++
}
}
if data.HGrab && spanExpandCount == 0 {
expandCount++
expandColumn[j] = true
}
w := data.cacheSize.Width - spanWidth - float32(hSpan-1)*f.HSpacing
if w > 0 {
if f.EqualColumns {
equalWidth := xmath.Floor((w + spanWidth) / float32(hSpan))
for k := 0; k < hSpan; k++ {
if widths[j-k] < equalWidth {
widths[j-k] = equalWidth
}
}
} else {
f.apportionExtra(w, j, spanExpandCount, hSpan, expandColumn, widths)
}
}
minimumWidth := data.minCacheSize.Width
if !data.HGrab || minimumWidth != 0 {
if !data.HGrab || minimumWidth < 1 {
w = data.cacheSize.Width
} else {
w = minimumWidth
}
w -= spanMinWidth + float32(hSpan-1)*f.HSpacing
if w > 0 {
f.apportionExtra(w, j, spanExpandCount, hSpan, expandColumn, minWidths)
}
}
}
}
}
}
if f.EqualColumns {
var minColumnWidth, columnWidth float32
for i := 0; i < f.Columns; i++ {
if minColumnWidth < minWidths[i] {
minColumnWidth = minWidths[i]
}
if columnWidth < widths[i] {
columnWidth = widths[i]
}
}
if width > 0 && expandCount != 0 {
columnWidth = max(minColumnWidth, xmath.Floor(availableWidth/float32(f.Columns)))
}
for i := 0; i < f.Columns; i++ {
expandColumn[i] = expandCount > 0
widths[i] = columnWidth
}
}
// The following is still run even when EqualColumns is set, so that the overall width is consistent.
expandCount = 0
for i := 0; i < f.Columns; i++ {
if expandColumn[i] {
expandCount++
}
}
if width > 0 && expandCount > 0 {
var totalWidth float32
for i := 0; i < f.Columns; i++ {
totalWidth += widths[i]
}
c := expandCount
for xmath.Abs(totalWidth-availableWidth) > 0.01 {
delta := (availableWidth - totalWidth) / float32(c)
for j := 0; j < f.Columns; j++ {
if expandColumn[j] {
if widths[j]+delta > minWidths[j] {
widths[j] += delta
} else {
widths[j] = minWidths[j]
expandColumn[j] = false
c--
}
}
}
for j := 0; j < f.Columns; j++ {
for i := 0; i < f.rows; i++ {
data := f.getData(grid, i, j, false)
if data != nil {
hSpan := max(1, min(data.HSpan, f.Columns))
if hSpan > 1 {
minimumWidth := data.minCacheSize.Width
if !data.HGrab || minimumWidth != 0 {
var spanWidth float32
spanExpandCount := 0
for k := 0; k < hSpan; k++ {
spanWidth += widths[j-k]
if expandColumn[j-k] {
spanExpandCount++
}
}
var w float32
if !data.HGrab || minimumWidth < 1 {
w = data.cacheSize.Width
} else {
w = minimumWidth
}
w -= spanWidth + float32(hSpan-1)*f.HSpacing
if w > 0 {
f.apportionExtra(w, j, spanExpandCount, hSpan, expandColumn, widths)
}
}
}
}
}
}
if c == 0 {
break
}
totalWidth = 0
for i := 0; i < f.Columns; i++ {
totalWidth += widths[i]
}
}
}
return widths
}
func (f *FlexLayout) apportionExtra(extra float32, base, count, span int, expand []bool, values []float32) {
if count == 0 {
values[base] += extra
} else {
extraInt := int(xmath.Floor(extra))
delta := extraInt / count
remainder := extraInt - delta*count
for i := 0; i < span; i++ {
j := base - i
if expand[j] {
values[j] += float32(delta)
}
}
for remainder > 0 {
for i := 0; i < span; i++ {
j := base - i
if expand[j] {
values[j]++
remainder--
if remainder == 0 {
break
}
}
}
}
}
}
func (f *FlexLayout) getData(grid [][]*Panel, row, column int, first bool) *FlexLayoutData {
target := grid[row][column]
if target != nil {
data := getDataFromTarget(target)
hSpan := max(1, min(data.HSpan, f.Columns))
vSpan := max(1, data.VSpan)
var i, j int
if first {
i = row + vSpan - 1
j = column + hSpan - 1
} else {
i = row - vSpan + 1
j = column - hSpan + 1
}
if i >= 0 && i < f.rows {
if j >= 0 && j < f.Columns {
if target == grid[i][j] {
return data
}
}
}
}
return nil
}
func (f *FlexLayout) wrap(width float32, grid [][]*Panel, widths []float32, useMinimumSize bool) {
if width > 0 {
for j := 0; j < f.Columns; j++ {
for i := 0; i < f.rows; i++ {
data := f.getData(grid, i, j, false)
if data != nil {
if data.SizeHint.Height < 1 {
hSpan := max(1, min(data.HSpan, f.Columns))
var currentWidth float32
for k := 0; k < hSpan; k++ {
currentWidth += widths[j-k]
}
currentWidth += float32(hSpan-1) * f.HSpacing
if currentWidth != data.cacheSize.Width && data.HAlign == align.Fill || data.cacheSize.Width > currentWidth {
hint := Size{Width: max(data.minCacheSize.Width, currentWidth)}
data.computeCacheSize(f.sizingCacheData(grid[i][j], hint), hint, useMinimumSize)
minimumHeight := data.MinSize.Height
if data.VGrab && minimumHeight > 0 && data.cacheSize.Height < minimumHeight {
data.cacheSize.Height = minimumHeight
}
}
}
}
}
}
}
}
func (f *FlexLayout) adjustRowHeights(height float32, grid [][]*Panel) []float32 {
availableHeight := height - f.VSpacing*float32(f.rows-1)
expandCount := 0
heights := make([]float32, f.rows)
minHeights := make([]float32, f.rows)
expandRow := make([]bool, f.rows)
for i := 0; i < f.rows; i++ {
for j := 0; j < f.Columns; j++ {
data := f.getData(grid, i, j, true)
if data != nil {
vSpan := max(1, min(data.VSpan, f.rows))
if vSpan == 1 {
h := data.cacheSize.Height
if heights[i] < h {
heights[i] = h
}
if data.VGrab {
if !expandRow[i] {
expandCount++
}
expandRow[i] = true
}
minimumHeight := data.MinSize.Height
if !data.VGrab || minimumHeight != 0 {
if !data.VGrab || minimumHeight < 1 {
h = data.minCacheSize.Height
} else {
h = minimumHeight
}
if minHeights[i] < h {
minHeights[i] = h
}
}
}
}
}
for j := 0; j < f.Columns; j++ {
data := f.getData(grid, i, j, false)
if data != nil {
vSpan := max(1, min(data.VSpan, f.rows))
if vSpan > 1 {
var spanHeight, spanMinHeight float32
spanExpandCount := 0
for k := 0; k < vSpan; k++ {
spanHeight += heights[i-k]
spanMinHeight += minHeights[i-k]
if expandRow[i-k] {
spanExpandCount++
}
}
if data.VGrab && spanExpandCount == 0 {
expandCount++
expandRow[i] = true
}
h := data.cacheSize.Height - spanHeight - float32(vSpan-1)*f.VSpacing
if h > 0 {
if spanExpandCount == 0 {
heights[i] += h
} else {
delta := h / float32(spanExpandCount)
for k := 0; k < vSpan; k++ {
if expandRow[i-k] {
heights[i-k] += delta
}
}
}
}
minimumHeight := data.MinSize.Height
if !data.VGrab || minimumHeight != 0 {
if !data.VGrab || minimumHeight < 1 {
h = data.minCacheSize.Height
} else {
h = minimumHeight
}
h -= spanMinHeight + float32(vSpan-1)*f.VSpacing
if h > 0 {
f.apportionExtra(h, i, spanExpandCount, vSpan, expandRow, minHeights)
}
}
}
}
}
}
if height > 0 && expandCount > 0 {
var totalHeight float32
for i := 0; i < f.rows; i++ {
totalHeight += heights[i]
}
c := expandCount
delta := (availableHeight - totalHeight) / float32(c)
for xmath.Abs(totalHeight-availableHeight) > 0.01 {
for i := 0; i < f.rows; i++ {
if expandRow[i] {
if heights[i]+delta > minHeights[i] {
heights[i] += delta
} else {
heights[i] = minHeights[i]
expandRow[i] = false
c--
}
}
}
for i := 0; i < f.rows; i++ {
for j := 0; j < f.Columns; j++ {
data := f.getData(grid, i, j, false)
if data != nil {
vSpan := max(1, min(data.VSpan, f.rows))
if vSpan > 1 {
minimumHeight := data.MinSize.Height
if !data.VGrab || minimumHeight != 0 {
var spanHeight float32
spanExpandCount := 0
for k := 0; k < vSpan; k++ {
spanHeight += heights[i-k]
if expandRow[i-k] {
spanExpandCount++
}
}
var h float32
if !data.VGrab || minimumHeight < 1 {
h = data.minCacheSize.Height
} else {
h = minimumHeight
}
h -= spanHeight + float32(vSpan-1)*f.VSpacing
if h > 0 {
f.apportionExtra(h, i, spanExpandCount, vSpan, expandRow, heights)
}
}
}
}
}
}
if c == 0 {
break
}
totalHeight = 0
for i := 0; i < f.rows; i++ {
totalHeight += heights[i]
}
delta = (availableHeight - totalHeight) / float32(c)
}
}
return heights
}
func (f *FlexLayout) positionChildren(location Point, grid [][]*Panel, widths, heights []float32) {
gridY := location.Y
for i := 0; i < f.rows; i++ {
gridX := location.X
for j := 0; j < f.Columns; j++ {
data := f.getData(grid, i, j, true)
if data != nil {
hSpan := max(1, min(data.HSpan, f.Columns))
vSpan := max(1, data.VSpan)
var cellWidth, cellHeight float32
for k := 0; k < hSpan; k++ {
cellWidth += widths[j+k]
}
for k := 0; k < vSpan; k++ {
cellHeight += heights[i+k]
}
cellWidth += f.HSpacing * float32(hSpan-1)
childX := gridX
childWidth := min(data.cacheSize.Width, cellWidth)
switch data.HAlign {
case align.Middle:
childX += max(0, (cellWidth-childWidth)/2)
case align.End:
childX += max(0, cellWidth-childWidth)
case align.Fill:
childWidth = cellWidth
default:
}
cellHeight += f.VSpacing * float32(vSpan-1)
childY := gridY
childHeight := min(data.cacheSize.Height, cellHeight)
switch data.VAlign {
case align.Middle:
childY += max(0, (cellHeight-childHeight)/2)
case align.End:
childY += max(0, cellHeight-childHeight)
case align.Fill:
childHeight = cellHeight
default:
}
child := grid[i][j]
if child != nil {
child.SetFrameRect(Rect{Point: Point{X: childX, Y: childY}, Size: Size{Width: childWidth, Height: childHeight}})
}
}
gridX += widths[j] + f.HSpacing
}
gridY += heights[i] + f.VSpacing
}
}
func (f *FlexLayoutData) computeCacheSize(sizing *flexSizingCacheData, hint Size, useMinimumSize bool) {
f.cacheSize.Width = 0
f.cacheSize.Height = 0
f.minCacheSize.Width = 0
f.minCacheSize.Height = 0
if f.SizeHint.Width < 0 {
f.SizeHint.Width = 0
}
if f.SizeHint.Height < 0 {
f.SizeHint.Height = 0
}
if f.MinSize.Width < 0 {
f.MinSize.Width = 0
}
if f.MinSize.Height < 0 {
f.MinSize.Height = 0
}
if f.HSpan < 1 {
f.HSpan = 1
}
if f.VSpan < 1 {
f.VSpan = 1
}
if hint.Width > 0 || hint.Height > 0 {
if f.MinSize.Width > 0 {
f.minCacheSize.Width = f.MinSize.Width
} else {
f.minCacheSize.Width = sizing.minSize.Width
}
if hint.Width > 0 && hint.Width < f.minCacheSize.Width {
hint.Width = f.minCacheSize.Width
}
if hint.Width > 0 && hint.Width > sizing.maxSize.Width {
hint.Width = sizing.maxSize.Width
}
if f.MinSize.Height > 0 {
f.minCacheSize.Height = f.MinSize.Height
} else {
f.minCacheSize.Height = sizing.minSize.Height
}
if hint.Height > 0 && hint.Height < f.minCacheSize.Height {
hint.Height = f.minCacheSize.Height
}
if hint.Height > 0 && hint.Height > sizing.maxSize.Height {
hint.Height = sizing.maxSize.Height
}
}
if useMinimumSize {
f.cacheSize = sizing.minSize
if f.MinSize.Width > 0 {
f.minCacheSize.Width = f.MinSize.Width
} else {
f.minCacheSize.Width = sizing.minSize.Width
}
if f.MinSize.Height > 0 {
f.minCacheSize.Height = f.MinSize.Height
} else {
f.minCacheSize.Height = sizing.minSize.Height
}
} else {
f.cacheSize = sizing.prefSize
}
if hint.Width > 0 {
f.cacheSize.Width = hint.Width
}
if f.MinSize.Width > 0 && f.cacheSize.Width < f.MinSize.Width {
f.cacheSize.Width = f.MinSize.Width
}
if f.SizeHint.Width > 0 {
f.cacheSize.Width = f.SizeHint.Width
}
if hint.Height > 0 {
f.cacheSize.Height = hint.Height
}
if f.MinSize.Height > 0 && f.cacheSize.Height < f.MinSize.Height {
f.cacheSize.Height = f.MinSize.Height
}
if f.SizeHint.Height > 0 {
f.cacheSize.Height = f.SizeHint.Height
}
}