-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
flow_layout.go
206 lines (198 loc) · 5.45 KB
/
flow_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
// 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 (
"math"
"github.com/richardwilkes/unison/enums/align"
)
var _ Layout = &FlowLayout{}
// FlowLayout is a Layout that lays components out left to right, then top to bottom.
type FlowLayout struct {
HSpacing float32
VSpacing float32
}
// LayoutSizes implements Layout.
func (f *FlowLayout) LayoutSizes(target *Panel, hint Size) (minSize, prefSize, maxSize Size) {
var insets Insets
if b := target.Border(); b != nil {
insets = b.Insets()
}
if hint.Width < 1 {
hint.Width = math.MaxFloat32
}
if hint.Height < 1 {
hint.Height = math.MaxFloat32
}
width := hint.Width - insets.Width()
pt := Point{X: insets.Left, Y: insets.Top}
result := Size{Width: pt.Y, Height: pt.Y}
availWidth := width
availHeight := hint.Height - insets.Height()
var maxHeight float32
var largestChildMin Size
for _, child := range target.Children() {
minSize, prefSize, _ = child.Sizes(Size{})
if largestChildMin.Width < minSize.Width {
largestChildMin.Width = minSize.Width
}
if largestChildMin.Height < minSize.Height {
largestChildMin.Height = minSize.Height
}
if prefSize.Width > availWidth {
switch {
case minSize.Width <= availWidth:
prefSize.Width = availWidth
case pt.X == insets.Left:
prefSize.Width = minSize.Width
default:
pt.X = insets.Left
pt.Y += maxHeight + f.VSpacing
availWidth = width
availHeight -= maxHeight + f.VSpacing
maxHeight = 0
if prefSize.Width > availWidth {
if minSize.Width <= availWidth {
prefSize.Width = availWidth
} else {
prefSize.Width = minSize.Width
}
}
}
savedWidth := prefSize.Width
minSize, prefSize, _ = child.Sizes(Size{Width: prefSize.Width})
prefSize.Width = savedWidth
if prefSize.Height > availHeight {
if minSize.Height <= availHeight {
prefSize.Height = availHeight
} else {
prefSize.Height = minSize.Height
}
}
}
extent := pt.X + prefSize.Width
if result.Width < extent {
result.Width = extent
}
extent = pt.Y + prefSize.Height
if result.Height < extent {
result.Height = extent
}
if maxHeight < prefSize.Height {
maxHeight = prefSize.Height
}
availWidth -= prefSize.Width + f.HSpacing
if availWidth <= 0 {
pt.X = insets.Left
pt.Y += maxHeight + f.VSpacing
availWidth = width
availHeight -= maxHeight + f.VSpacing
maxHeight = 0
} else {
pt.X += prefSize.Width + f.HSpacing
}
}
result.Width += insets.Right
result.Height += insets.Bottom
largestChildMin.Width += insets.Width()
largestChildMin.Height += insets.Height()
return largestChildMin, result, MaxSize(result)
}
// PerformLayout implements Layout.
func (f *FlowLayout) PerformLayout(target *Panel) {
var insets Insets
if b := target.Border(); b != nil {
insets = b.Insets()
}
size := target.ContentRect(true).Size
width := size.Width - insets.Width()
pt := Point{X: insets.Left, Y: insets.Top}
availWidth := width
availHeight := size.Height - insets.Height()
var maxHeight float32
children := target.Children()
rects := make([]Rect, len(children))
start := 0
for i, child := range children {
minSize, prefSize, _ := child.Sizes(Size{})
if prefSize.Width > availWidth {
switch {
case minSize.Width <= availWidth:
prefSize.Width = availWidth
case pt.X == insets.Left:
prefSize.Width = minSize.Width
default:
pt.X = insets.Left
pt.Y += maxHeight + f.VSpacing
availWidth = width
availHeight -= maxHeight + f.VSpacing
if i > start {
f.applyRects(children[start:i], rects[start:i], maxHeight)
start = i
}
maxHeight = 0
if prefSize.Width > availWidth {
if minSize.Width <= availWidth {
prefSize.Width = availWidth
} else {
prefSize.Width = minSize.Width
}
}
}
savedWidth := prefSize.Width
minSize, prefSize, _ = child.Sizes(Size{Width: prefSize.Width})
prefSize.Width = savedWidth
if prefSize.Height > availHeight {
if minSize.Height <= availHeight {
prefSize.Height = availHeight
} else {
prefSize.Height = minSize.Height
}
}
}
rects[i] = Rect{Point: pt, Size: prefSize}
if maxHeight < prefSize.Height {
maxHeight = prefSize.Height
}
availWidth -= prefSize.Width + f.HSpacing
if availWidth <= 0 {
pt.X = insets.Left
pt.Y += maxHeight + f.VSpacing
availWidth = width
availHeight -= maxHeight + f.VSpacing
f.applyRects(children[start:i+1], rects[start:i+1], maxHeight)
start = i + 1
maxHeight = 0
} else {
pt.X += prefSize.Width + f.HSpacing
}
}
if start < len(children) {
f.applyRects(children[start:], rects[start:], maxHeight)
}
}
func (f *FlowLayout) applyRects(children []*Panel, rects []Rect, maxHeight float32) {
for i, child := range children {
vAlign, ok := child.LayoutData().(align.Enum)
if !ok {
vAlign = align.Start
}
switch vAlign {
case align.Middle:
if rects[i].Height < maxHeight {
rects[i].Y += (maxHeight - rects[i].Height) / 2
}
case align.End:
rects[i].Y += maxHeight - rects[i].Height
case align.Fill:
rects[i].Height = maxHeight
default: // same as align.Start
}
child.SetFrameRect(rects[i])
}
}