forked from lxn/walk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflowlayout.go
288 lines (218 loc) · 5.59 KB
/
flowlayout.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
// Copyright 2018 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"github.com/lxn/win"
)
type FlowLayout struct {
container Container
hwnd2StretchFactor map[win.HWND]int
margins Margins
spacing int
resetNeeded bool
}
type flowLayoutSection struct {
items []flowLayoutSectionItem
primarySpaceLeft int
secondaryMinSize int
}
type flowLayoutSectionItem struct {
widget Widget
minSize Size
}
func NewFlowLayout() *FlowLayout {
return new(FlowLayout)
}
func (l *FlowLayout) Container() Container {
return l.container
}
func (l *FlowLayout) SetContainer(value Container) {
if value != l.container {
if l.container != nil {
l.container.SetLayout(nil)
}
l.container = value
if value != nil && value.Layout() != Layout(l) {
value.SetLayout(l)
l.Update(true)
}
}
}
func (l *FlowLayout) Margins() Margins {
return l.margins
}
func (l *FlowLayout) SetMargins(value Margins) error {
if value.HNear < 0 || value.VNear < 0 || value.HFar < 0 || value.VFar < 0 {
return newError("margins must be positive")
}
l.margins = value
return nil
}
func (l *FlowLayout) Spacing() int {
return l.spacing
}
func (l *FlowLayout) SetSpacing(value int) error {
if value != l.spacing {
if value < 0 {
return newError("spacing cannot be negative")
}
l.spacing = value
l.Update(false)
}
return nil
}
func (l *FlowLayout) StretchFactor(widget Widget) int {
if factor, ok := l.hwnd2StretchFactor[widget.Handle()]; ok {
return factor
}
return 1
}
func (l *FlowLayout) SetStretchFactor(widget Widget, factor int) error {
if factor != l.StretchFactor(widget) {
if l.container == nil {
return newError("container required")
}
handle := widget.Handle()
if !l.container.Children().containsHandle(handle) {
return newError("unknown widget")
}
if factor < 1 {
return newError("factor must be >= 1")
}
l.hwnd2StretchFactor[handle] = factor
l.Update(false)
}
return nil
}
func (l *FlowLayout) cleanupStretchFactors() {
widgets := l.container.Children()
for handle, _ := range l.hwnd2StretchFactor {
if !widgets.containsHandle(handle) {
delete(l.hwnd2StretchFactor, handle)
}
}
}
func (l *FlowLayout) LayoutFlags() LayoutFlags {
return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert | GreedyHorz | GreedyVert
}
func (l *FlowLayout) MinSize() Size {
if l.container == nil {
return Size{}
}
children := l.container.Children()
var width, height int
for i := children.Len() - 1; i >= 0; i-- {
widget := children.At(i)
if !shouldLayoutWidget(widget) {
continue
}
minSize := minSizeEffective(widget)
width = maxi(minSize.Width, width)
height = maxi(minSize.Height, height)
}
return Size{width + l.margins.HNear + l.margins.HFar, height}
}
func (l *FlowLayout) minSizeForWidth(w int) Size {
if l.container == nil {
return Size{}
}
sections := l.sectionsForPrimarySize(w)
var width, height int
for i, section := range sections {
width = maxi(width, w-section.primarySpaceLeft)
if i > 0 {
height += l.spacing
}
height += section.secondaryMinSize
}
return Size{width + l.margins.HNear + l.margins.HFar, height}
}
func (l *FlowLayout) Update(reset bool) error {
if l.container == nil {
return nil
}
if reset {
l.resetNeeded = true
}
if l.container.Suspended() {
return nil
}
if !performingScheduledLayouts && scheduleLayout(l) {
return nil
}
if l.resetNeeded {
l.resetNeeded = false
l.cleanupStretchFactors()
}
ifContainerIsScrollViewDoCoolSpecialLayoutStuff(l)
bounds := l.container.ClientBounds()
sections := l.sectionsForPrimarySize(bounds.Width)
for i, section := range sections {
var widgets []Widget
for _, item := range section.items {
widgets = append(widgets, item.widget)
}
bounds.Height = section.secondaryMinSize
margins := l.margins
if i > 0 {
margins.VNear = 0
}
if i < len(sections)-1 {
margins.VFar = 0
}
if err := performBoxLayout(widgets, Horizontal, bounds, margins, l.spacing, l.hwnd2StretchFactor); err != nil {
return err
}
bounds.Y += bounds.Height + l.spacing
}
return nil
}
func (l *FlowLayout) sectionsForPrimarySize(primarySize int) []flowLayoutSection {
children := l.container.Children()
count := children.Len()
var sections []flowLayoutSection
section := flowLayoutSection{
primarySpaceLeft: primarySize - l.margins.HNear - l.margins.HFar,
}
addSection := func() {
sections = append(sections, section)
section.items = nil
section.primarySpaceLeft = primarySize - l.margins.HNear - l.margins.HFar
section.secondaryMinSize = 0
}
for i := 0; i < count; i++ {
var item flowLayoutSectionItem
item.widget = children.At(i)
if !shouldLayoutWidget(item.widget) {
continue
}
item.minSize = minSizeEffective(item.widget)
addItem := func() {
section.items = append(section.items, item)
if len(section.items) > 1 {
section.primarySpaceLeft -= l.spacing
}
section.primarySpaceLeft -= item.minSize.Width
section.secondaryMinSize = maxi(section.secondaryMinSize, item.minSize.Height)
}
if section.primarySpaceLeft < item.minSize.Width && len(section.items) == 0 {
addItem()
addSection()
} else if section.primarySpaceLeft < l.spacing+item.minSize.Width {
addSection()
addItem()
} else {
addItem()
}
}
if len(section.items) > 0 {
addSection()
}
if len(sections) > 0 {
sections[0].secondaryMinSize += l.margins.VNear
sections[len(sections)-1].secondaryMinSize += l.margins.VFar
}
return sections
}