-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbox.go
78 lines (67 loc) · 1.7 KB
/
vbox.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
package layoutx
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
)
// NewVBoxLayout NewVBoxLayout
func NewVBoxLayout(objects ...fyne.CanvasObject) fyne.CanvasObject {
return container.New(&VBoxLayout{}, objects...)
}
// VBoxLayout 自定义横向布局
type VBoxLayout struct{}
// MinSize MinSize
func (hb *VBoxLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
minSize := fyne.NewSize(0, 0)
addPadding := false
padding := theme.Padding()
for _, child := range objects {
if !child.Visible() || child.MinSize().Height == 0 {
continue
}
if m := fyne.Max(child.MinSize().Width, child.Size().Width); minSize.Width < m {
minSize.Width = m
}
minSize.Height += fyne.Max(child.MinSize().Height, child.Size().Height)
if addPadding {
minSize.Height += padding
}
addPadding = true
}
return minSize
}
// Layout Layout
func (hb *VBoxLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
spacers := 0
total := float32(0)
for _, child := range objects {
if !child.Visible() {
continue
}
if child.MinSize().Height == 0 {
spacers++
continue
}
total += fyne.Max(child.MinSize().Height, child.Size().Height)
}
padding := theme.Padding()
extra := size.Height - total - (padding * float32(len(objects)-spacers-1))
extraCell := float32(0)
if spacers > 0 {
extraCell = extra / float32(spacers)
}
x, y := float32(0), float32(0)
for _, child := range objects {
if !child.Visible() {
continue
}
if child.MinSize().Height == 0 {
y += extraCell
continue
}
child.Move(fyne.NewPos(x, y))
height := fyne.Max(child.MinSize().Height, child.Size().Height)
y += padding + height
child.Resize(fyne.NewSize(size.Width, height))
}
}