-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbox_layout.go
175 lines (153 loc) · 3.98 KB
/
box_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
package main
import (
"fyne.io/fyne"
"fyne.io/fyne/layout"
"fyne.io/fyne/theme"
)
// Declare conformity with Layout interface
var _ fyne.Layout = (*boxLayout)(nil)
type boxLayout struct {
horizontal bool
sizes []int
}
// NewHBoxLayout returns a horizontal box layout for stacking a number of child
// canvas objects or widgets left to right.
func newHBoxLayout(sizes []int) fyne.Layout {
return &boxLayout{true, sizes}
}
// NewVBoxLayout returns a vertical box layout for stacking a number of child
// canvas objects or widgets top to bottom.
func newVBoxLayout(sizes []int) fyne.Layout {
return &boxLayout{false, sizes}
}
func isVerticalSpacer(obj fyne.CanvasObject) bool {
if spacer, ok := obj.(layout.SpacerObject); ok {
return spacer.ExpandVertical()
}
return false
}
func isHorizontalSpacer(obj fyne.CanvasObject) bool {
if spacer, ok := obj.(layout.SpacerObject); ok {
return spacer.ExpandHorizontal()
}
return false
}
func (g *boxLayout) isSpacer(obj fyne.CanvasObject) bool {
// invisible spacers don't impact layout
if !obj.Visible() {
return false
}
if g.horizontal {
return isHorizontalSpacer(obj)
}
return isVerticalSpacer(obj)
}
// Layout is called to pack all child objects into a specified size.
// For a VBoxLayout this will pack objects into a single column where each item
// is full width but the height is the minimum required.
// Any spacers added will pad the view, sharing the space if there are two or more.
func (g *boxLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
spacers := make([]fyne.CanvasObject, 0)
total := 0
for i, child := range objects {
if !child.Visible() {
continue
}
if g.isSpacer(child) {
spacers = append(spacers, child)
continue
}
if g.horizontal {
width := child.MinSize().Width
if i < len(g.sizes) {
width = fyne.Max(width, g.sizes[i])
}
total += width
} else {
height := child.MinSize().Height
if i < len(g.sizes) {
height = fyne.Max(height, g.sizes[i])
}
total += height
}
}
x, y := 0, 0
var extra int
if g.horizontal {
extra = size.Width - total - (theme.Padding() * (len(objects) - len(spacers) - 1))
} else {
extra = size.Height - total - (theme.Padding() * (len(objects) - len(spacers) - 1))
}
extraCell := 0
if len(spacers) > 0 {
extraCell = int(float64(extra) / float64(len(spacers)))
}
for i, child := range objects {
if !child.Visible() {
continue
}
width := child.MinSize().Width
height := child.MinSize().Height
if i < len(g.sizes) {
if g.horizontal {
width = fyne.Max(width, g.sizes[i])
} else {
height = fyne.Max(height, g.sizes[i])
}
}
if g.isSpacer(child) {
if g.horizontal {
x += extraCell
} else {
y += extraCell
}
continue
}
child.Move(fyne.NewPos(x, y))
if g.horizontal {
x += theme.Padding() + width
child.Resize(fyne.NewSize(width, size.Height))
} else {
y += theme.Padding() + height
child.Resize(fyne.NewSize(size.Width, height))
}
}
}
// MinSize finds the smallest size that satisfies all the child objects.
// For a BoxLayout this is the width of the widest item and the height is
// the sum of of all children combined with padding between each.
func (g *boxLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
minSize := fyne.NewSize(0, 0)
addPadding := false
for i, child := range objects {
if !child.Visible() {
continue
}
if g.isSpacer(child) {
continue
}
if g.horizontal {
minSize.Height = fyne.Max(child.MinSize().Height, minSize.Height)
width := child.MinSize().Width
if i < len(g.sizes) {
width = fyne.Max(width, g.sizes[i])
}
minSize.Width += width
if addPadding {
minSize.Width += theme.Padding()
}
} else {
minSize.Width = fyne.Max(child.MinSize().Width, minSize.Width)
height := child.MinSize().Height
if i < len(g.sizes) {
height = fyne.Max(height, g.sizes[i])
}
minSize.Height += height
if addPadding {
minSize.Height += theme.Padding()
}
}
addPadding = true
}
return minSize
}