forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxlayout.go
132 lines (114 loc) · 3.29 KB
/
boxlayout.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
package layout
import (
"fyne.io/fyne"
"fyne.io/fyne/theme"
)
type boxLayout struct {
horizontal bool
}
func isVerticalSpacer(obj fyne.CanvasObject) bool {
if spacer, ok := obj.(SpacerObject); ok {
return spacer.ExpandVertical()
}
return false
}
func isHorizontalSpacer(obj fyne.CanvasObject) bool {
if spacer, ok := obj.(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 an VBoxLayout this will pack objects into a single column where each item
// is full width but the it's 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 _, child := range objects {
if g.isSpacer(child) {
spacers = append(spacers, child)
continue
}
if g.horizontal {
total += child.MinSize().Width
} else {
total += child.MinSize().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 _, child := range objects {
width := child.MinSize().Width
height := child.MinSize().Height
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 {
spacerCount := 0
minSize := fyne.NewSize(0, 0)
for _, child := range objects {
if g.isSpacer(child) {
spacerCount++
continue
}
if g.horizontal {
minSize = minSize.Add(fyne.NewSize(child.MinSize().Width, 0))
minSize.Height = fyne.Max(child.MinSize().Height, minSize.Height)
} else {
minSize = minSize.Add(fyne.NewSize(0, child.MinSize().Height))
minSize.Width = fyne.Max(child.MinSize().Width, minSize.Width)
}
}
if g.horizontal {
return minSize.Add(fyne.NewSize(theme.Padding()*(len(objects)-1-spacerCount), 0))
}
return minSize.Add(fyne.NewSize(0, theme.Padding()*(len(objects)-1-spacerCount)))
}
// NewHBoxLayout returns a horizontal box layout for stacking a number of child
// canvas objects or widgets left to right.
func NewHBoxLayout() fyne.Layout {
return &boxLayout{true}
}
// NewVBoxLayout returns a vertical box layout for stacking a number of child
// canvas objects or widgets top to bottom.
func NewVBoxLayout() fyne.Layout {
return &boxLayout{false}
}